15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
</>CodeWithHarry
Start Learning Tutorials
Java Cheatsheet
"Java Cheatsheet for java developers"
By CodeWithHarry Updated: April 5, 2025
Basics
Boilerplate
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World");
}
}
public → Access modifier (needed for JVM to call main ).
static → Allows JVM to call without creating an object.
void → Method returns nothing.
String[] args → Command-line arguments.
[Link] 1/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
Showing Output
[Link]("Hello"); // prints without newline
[Link](" World"); // prints with newline
[Link]("Age: %d", 20); // formatted output
Taking Input (Scanner)
import [Link];
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String name = [Link](); // String
int age = [Link](); // int
float f = [Link](); // float
double d = [Link](); // double
boolean b = [Link](); // boolean
char c = [Link]().charAt(0); // char (first char of input)
[Link]("Name: " + name);
}
}
Primitive Types
Eight primitives: byte , short , int , long , float , double , boolean , char .
byte (8-bit, -128 to 127)
short (16-bit, -32,768 to 32,767)
[Link] 2/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
int (32-bit, default for integers)
long (64-bit, append L for literals)
float (32-bit, append f )
double (64-bit, default for decimals)
boolean ( true or false )
char (16-bit Unicode)
Comments
// Single line
/* Multi-line */
/** Javadoc comment */
Constants
final double PI = 3.14159;
static final int MAX = 100;
Arithmetic Operators
+ - * / % ++ --
Division between integers truncates result.
% gives remainder.
[Link] 3/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
Assignment Operators
=, +=, -=, *=, /=, %=
Comparison Operators
==, !=, >, <, >=, <=
Logical Operators
&& (AND), || (OR), ! (NOT)
Escape Sequences
\n → newline
\t → tab
\\ → backslash
\' → single quote
\" → double quote
\r → carriage return
\b → backspace
( \? is not valid in Java; use "?" literally.)
Type Casting
[Link] 4/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
Widening (automatic)
int x = 45;
double y = x; // OK
Narrowing (manual)
double d = 45.9;
int n = (int) d; // truncates decimal
Control Flow
if / else if / else
if (x > 0) { ... }
else if (x == 0) { ... }
else { ... }
Ternary
String result = (x > 0) ? "Positive" : "Negative";
switch (Java 7+ supports Strings)
switch(day) {
case 1: [Link]("Mon"); break;
...
default: [Link]("Invalid");
}
[Link] 5/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
Loops
while
while (i < 5) {
i++;
}
do-while
do {
i++;
} while (i < 5);
for
for (int i = 0; i < 5; i++) { ... }
for-each
for (int n : arr) { ... }
break / continue
break → exits loop
continue → skips iteration
Arrays
[Link] 6/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
int[] nums = new int[5];
String[] names = {"Harry", "Rohan"};
[Link]([Link]);
Multi-dimensional
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
[Link](matrix[1][2]); // 6
Methods
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
[Link](add(5, 10));
}
Method Overloading
void sum(int a, int b) { ... }
void sum(double a, double b) { ... }
Recursion
int fact(int n) {
if (n == 0) return 1;
return n * fact(n - 1);
[Link] 7/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
Strings
Strings are objects of [Link] .
String s = "Hello";
int len = [Link]();
[Link]();
[Link]();
[Link]("l");
[Link]("He");
[Link]("Hello");
[Link]("hello");
[Link](0, 3); // "Hel"
[Link]("H", "J");
Math Class
[Link](5, 10);
[Link](5, 10);
[Link](16);
[Link](2, 3);
[Link](-10);
[Link](); // [0.0, 1.0)
Download Java CheatSheet
[Link] 8/9
15/01/2026, 00:20 Java Cheatsheet | Blog | CodeWithHarry
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
Made with ❤️ and ☕ in India
[Link] 9/9