Java Cheat Sheet
Java Cheat Sheet
For Beginners
Class Name
Main Method
public class HelloWorld {
}
} Statement
Editing, compiling, and executing.
Editor HelloWorld.java
Yo
ur
Pr
Use any text editor to
og
Fil
create program
e
ra
m
Compiler
Type javac
HelloWorld.java to
compile your program
Output
Data Types and Examples
char characters
File Name
a = 10;
Variable Name
b= 20;
Initial initialization
int c = a+b
Statement
assignment statements
Integers
Floating-point numbers.
Floating-point numbers.
== equal 2 == 2 2 == 5
Printing
// Outer if statement.
if(condition)
{
// Inner if statement defined in outer if else statement.
if(condition)
statement1;
}
// Else part of outer if statement.
else {
statement2;
}
For example:
if (x > y)
{
if (y > z)
System.out.println("x is greater than y and z"); //
statement1.
}
else
System.out.println("x is less than or equal to y"); //
statement2.
if-else if Ladder Statements in Java
if(condition)
statement1;
else if(condition)
statement2;
else if(condition)
statement3;
...
else
statement4;
https://www.scientecheasy.com/
while loop.
int power = 1;
while ( power <= n/2)
{
power = 2*power;
}
Loop Continuation
declare and initialize a
condition
loop control value
Increment
int power = 1;
for ( int i = 0 ; i<=n ; i++)
{
System.out.println( i + " " + power)
power = 2*power
}
Body
Loops.
Switch statement.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not
matched;
}
Arrays
arrayRefVar=new datatype[size];
Arrays
a[0]=27;//initialization
a[1]=25;
a[2]=75;
a[3]=47;
a[4]=59;
Arrays
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Functions
Local Variable
Functions
Classes