Java Complete Cheatsheet for Beginners
CyberSamir Team
May 2025
Produced by CyberSamir Team
A quick reference guide for beginners learning Java programming
1
1 Java Basics 5 Control Flow
Java is a high-level, object-oriented programming language 5.1 If-Else
known for its portability and robustness. It runs on the 1 if ( age >= 18) {
Java Virtual Machine (JVM). 2 System . out . println ( " Adult " ) ;
3 } else {
4 System . out . println ( " Minor " ) ;
5 }
1.1 First Program
1 public class HelloWorld {
2 public static void main ( String [] args ) { 5.2 Switch
3 System . out . println ( " Hello , World ! " ) ;
4 } 1 int day = 2;
5 } 2 switch ( day ) {
3 case 1: System . out . println ( " Monday " ) ; break ;
4 case 2: System . out . println ( " Tuesday " ) ; break ;
5 default : System . out . println ( " Invalid " ) ;
1.2 Comments 6 }
1 // Single - line comment
2 /* Multi - line comment */
3 /* * D o c u m e n t a t i o n comment */ 5.3 Loops
1 // For loop
2 for ( int i = 0; i < 5; i ++) {
3 System . out . println ( i ) ;
2 Data Types 4 }
5 // While loop
6 int i = 0;
2.1 Primitive Types 7 while ( i < 5) {
8 System . out . println ( i ) ;
• byte: 8-bit integer (-128 to 127) 9 i ++;
10 }
• short: 16-bit integer 11 // Do - while loop
12 do {
• int: 32-bit integer 13 System . out . println ( i ) ;
14 i ++;
15 } while ( i < 5) ;
• long: 64-bit integer (add L suffix)
• float: 32-bit float (add F suffix)
6 Arrays
• double: 64-bit float
1 int [] numbers = {1 , 2 , 3 , 4 , 5};
• boolean: true or false 2 int [] arr = new int [5]; // Empty array
3 arr [0] = 10; // Assign value
• char: 16-bit Unicode character (e.g., ’A’) 4 System . out . println ( numbers . length ) ; // 5
2.2 Non-Primitive Types
7 Methods
• String: e.g., "Hello"
1 public int add ( int a , int b ) {
2 return a + b ;
• Arrays: e.g., int[] numbers 3 }
4 // Calling method
• Classes and Objects 5 int sum = add (5 , 3) ; // 8
3 Variables
8 Classes and Objects
1 int age = 25; // D e c l a r a t i o n and i n i t i a l i z a t i o n
2 String name = " Samir " ; 1 public class Car {
3 final double PI = 3.14159; // C on st an t 2 String model ;
3 int year ;
4
5 public Car ( String model , int year ) {
6 this . model = model ;
4 Operators 7 this . year = year ;
8 }
9
4.1 Arithmetic 10 public void display () {
11 System . out . println ( model + " " + year ) ;
1 + - * / % ( modulus ) 12 }
2 int a = 10 , b = 3; 13 }
3 int sum = a + b ; // 13 14 // C r e a t i n g object
4 int mod = a % b ; // 1 15 Car myCar = new Car ( " Toyota " , 2020) ;
16 myCar . display () ; // Toyota 2020
4.2 Comparison
9 Inheritance
1 == != > < >= <=
2 boolean isEqual = ( a == b ) ; // false
1 public class Vehicle {
2 String brand ;
3 }
4 public class Car extends Vehicle {
4.3 Logical 5 int wheels = 4;
6 }
1 && ( AND ) || ( OR ) ! ( NOT ) 7 Car car = new Car () ;
2 boolean result = ( a > 5 && b < 5) ; // true 8 car . brand = " Honda " ;
2
10 Exception Handling 4 String name = scanner . nextLine () ;
5 System . out . println ( " Hello , " + name ) ;
1 try { 6 scanner . close () ;
2 int [] arr = {1 , 2};
3 System . out . println ( arr [5]) ;
4 } catch ( A r r a y I n d e x O u t O f B o u n d s E x c e p t i o n e ) {
5 System . out . println ( " Error : " + e ) ; 13 Collections
6 } finally {
7 System . out . println ( " This always runs " ) ;
8 } 1 import java . util . ArrayList ;
2 ArrayList < String > list = new ArrayList < >() ;
3 list . add ( " Apple " ) ; // Add item
4 list . get (0) ; // " Apple "
5 list . remove (0) ; // Remove item
11 String Methods
1 String str = " Hello " ;
2 str . length () ; // 5
3 str . toUpperCase () ; // " HELLO " 14 Tips for Beginners
4 str . substring (1 , 3) ; // " el "
5 str . contains ( " lo " ) ; // true • Practice coding daily on platforms like LeetCode.
• Use an IDE like IntelliJ IDEA or Eclipse.
12 Input/Output • Read the official Java documentation: https://
1 import java . util . Scanner ; docs.oracle.com/en/java/
2 Scanner scanner = new Scanner ( System . in ) ;
3 System . out . print ( " Enter name : " ) ; • Join communities like Stack Overflow.