Java_cheatsheet
Java_cheatsheet
</>CodeWithHarry
Java Cheatsheet
"Java Cheatsheet for java developers"
By CodeWithHarry Updated: 5 April 2025
Basics
Boilerplate
class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Showing Output
class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Taking Input
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(name);
}
}
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println(x);
}
}
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
float x = sc.nextFloat();
https://www.codewithharry.com/blogpost/java-cheatsheet 2/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
System.out.println(x);
}
}
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
System.out.println(x);
}
}
The eight primitives defined in Java are int , byte , short , long , float , double ,
boolean , and char . These aren't considered objects and represent raw values.
byte
byte is a primitive data type that only takes up 8 bits of memory.
class HelloWorld {
public static void main(String args[]) {
byte age = 18;
System.out.println(age);
}
}
long
long is another primitive data type related to integers. long takes up 64 bits of memory.
class HelloWorld {
https://www.codewithharry.com/blogpost/java-cheatsheet 3/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
float
We represent basic fractional numbers in Java using the float type. This is a single-
precision decimal number, which means if we get past six decimal points, this number
becomes less precise and more of an estimate.
class HelloWorld {
public static void main(String args[]) {
float price = 100.05f;
System.out.println(price);
}
}
char
char is a 16-bit integer representing a Unicode-encoded character.
class HelloWorld {
public static void main(String args[]) {
char letter = 'A';
System.out.println(letter);
}
}
int
int holds a wide range of non-fractional number values.
class HelloWorld {
public static void main(String args[]) {
int var1 = 256;
System.out.println(var1);
https://www.codewithharry.com/blogpost/java-cheatsheet 4/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
}
}
short
If we want to save memory and byte is too small, we can use short .
class HelloWorld {
public static void main(String args[]) {
short var2 = 5666;
System.out.println(var2);
}
}
Comments
A comment is the code that is not executed by the compiler, and the programmer uses it to
keep track of the code.
Multi-line comment
/* It's a
multi-line
comment
*/
Constants
Constants are like a variable, except that their value never changes during program execution.
https://www.codewithharry.com/blogpost/java-cheatsheet 5/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Arithmetic Expressions
Addition
Subtraction
It can be used to subtract two numbers.
Multiplication
https://www.codewithharry.com/blogpost/java-cheatsheet 6/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Division
It can be used to divide two numbers.
Modulo Remainder
Augmented Operators
Addition assignment
https://www.codewithharry.com/blogpost/java-cheatsheet 7/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
var += 10;
System.out.println(var);
}
}
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
https://www.codewithharry.com/blogpost/java-cheatsheet 8/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when
used inside a string literal.
Tab
It gives a tab space.
Backslash
It adds a backslash.
Single quote
https://www.codewithharry.com/blogpost/java-cheatsheet 9/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Question mark
It adds a question mark.
Carriage return
Double quote
https://www.codewithharry.com/blogpost/java-cheatsheet 10/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Type Casting
class HelloWorld {
public static void main(String args[]) {
int x = 45;
double var_name = x;
System.out.println(var_name);
}
}
class HelloWorld {
public static void main(String args[]) {
double x = 40005;
int var_name = (int) x;
System.out.println(var_name);
}
}
if Statement
if (condition) {
// block of code to be executed if the condition is true
https://www.codewithharry.com/blogpost/java-cheatsheet 11/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
if-else Statement
if (condition) {
// If condition is True then this block will get executed
} else {
// If condition is False then this block will get executed
}
if else-if Statement
if (condition1) {
// Codes
} else if(condition2) {
// Codes
} else if (condition3) {
// Codes
} else {
// Codes
}
Ternary Operator
It is shorthand for an if-else statement.
Syntax
Example
https://www.codewithharry.com/blogpost/java-cheatsheet 12/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
y = (x == 1) ? 61 : 90;
System.out.println("Value of y is: " + y);
y = (x == 20) ? 61 : 90;
System.out.println("Value of y is: " + y);
}
}
Switch Statements
It allows a variable to be tested for equality against a list of values (cases).
class SwitchExample {
public static void main(String args[]) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
https://www.codewithharry.com/blogpost/java-cheatsheet 13/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines repeatedly and
can be controlled as per conditions added by the coder.
while Loop
It iterates the block of code as long as a specified condition is True.
for Loop
for loop is used to run a block of code several times.
class HelloWorld {
public static void main(String args[]) {
for(int i = 1; i < 100; i++) {
System.out.println(i);
}
}
}
for-each Loop
}
}
}
do-while Loop
It is an exit-controlled loop. It is very similar to the while loop with one difference, i.e., the body
of the do-while loop is executed at least once even if the condition is False.
Break statement
break keyword inside the loop is used to terminate the loop.
class HelloWorld {
public static void main(String args[]) {
for(int i = 1; i < 100; i++) {
System.out.println(i);
if(i == 50)
break;
}
}
}
Continue statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting
https://www.codewithharry.com/blogpost/java-cheatsheet 15/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
class HelloWorld {
public static void main(String args[]) {
for(int i = 1; i < 100; i++) {
System.out.println(i);
if(i == 50)
continue;
}
}
}
Arrays
Declaring an array
Declaration of an array.
Defining an array
Defining an array.
Accessing an array
https://www.codewithharry.com/blogpost/java-cheatsheet 16/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Changing an element
Array length
}
}
}
Multi-dimensional Arrays
Arrays can be 1-D, 2-D, or multi-dimensional.
Methods
Methods are used to divide an extensive program into smaller pieces. It can be called multiple
times to provide reusability to the program.
Declaration
Declaration of a method.
returnType methodName(parameters) {
// statements
}
Calling a method
Calling a method.
methodName(arguments);
https://www.codewithharry.com/blogpost/java-cheatsheet 18/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Example
import java.util.Scanner;
public class EvenOdd {
public static void main (String args[]) {
// creating Scanner class object
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number: ");
// reading value from the user
int num = scan.nextInt();
// method calling
findEvenOdd(num);
}
}
Method Overloading
Method overloading means having multiple methods with the same name, but different
parameters.
class Calculate {
void sum(int x, int y) {
System.out.println("Sum is: " + (x + y));
}
void sum(float x, float y) {
System.out.println("Sum is: " + (x + y));
}
public static void main(String[] args) {
Calculate calc = new Calculate();
calc.sum(5, 4); // sum(int x, int y) is called.
calc.sum(1.2f, 5.6f); // sum(float x, float y) is called.
}
https://www.codewithharry.com/blogpost/java-cheatsheet 19/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
Recursion
Recursion is when a function calls a copy of itself to work on a minor problem. The function
that calls itself is known as the Recursive function.
void recurse() {
recurse();
}
Strings
String Length
Returns the length of the string.
https://www.codewithharry.com/blogpost/java-cheatsheet 20/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
toLowerCase()
indexOf()
Returns the index of a specified character from the string.
concat()
Used to concatenate two strings.
}
}
Math Class
Tags
Share
java cheatsheet
Main Learn
Home Courses
Contact Tutorials
Work With Us Notes
My Gear
Legal Social
Terms GitHub
Privacy Twitter (X)
Refund YouTube
Facebook
https://www.codewithharry.com/blogpost/java-cheatsheet 22/23
4/18/25, 11:22 AM Java Cheatsheet | Blog | CodeWithHarry
https://www.codewithharry.com/blogpost/java-cheatsheet 23/23