codingknowledge_
harish
Java CheatSheet
For Beginners
[Link] File Name
Class Name
Main Method
public class HelloWorld {
public static void main(String[] args) {
//Print "Hello World " in the terminal window
[Link]("Hello World")
}
} Statement
codingknowledge_
harish
Editing, compiling, and executing.
Editor [Link]
Yo
ur
Use any text editor to
Pr
create program Fil
og
e
ra
m
Compiler
Type javac
[Link] to
compile your program
[Link]
Type java HelloWorld to computer-language version of your
execute your program program
"Hello World" JVM
Output
codingknowledge_
harish
Data Types and Examples
Type Set of Values Common Operators
Int Integers +-*/%
double floating point numbers +-*/
boolean boolean value && || !
char characters
String sequence of characters +
Int : 74, 99, 21478521
double : 3.5, 2.55, 6.0222e8
boolean : true , flase
char : 'A' , 'D , 'F ' , '1' , '%' , '/n'
String : "AB" , "Hello"
codingknowledge_
harish
Declaration and assignment statements
File Name
Int a, b ; Declaration statement
a = 10;
Variable Name
b= 20;
Initial initialization
int c = a+b
Statement
assignment statements
codingknowledge_
harish
Integers
Values Integers between -2^31 and +2^31-1
Examples 1234, 24, 5, 200000
Operations sign add subtract multiply divide remainder
operators +- + - * / %
Floating-point numbers.
Values Real Numbers (Specified by IEEE 754 Standard))
Examples 3.14159, 2.0, 1.4142556568, 6.022e23
Operations add subtract multiply divide
operators + - * /
Floating-point numbers.
Values true or flase
literals True / Flase
Operations and or not
operators && || !
codingknowledge_
harish
Comparison operators.
op Meaning True False
== equal 2 == 2 2 == 5
!= not equal 3 != 2 2!=2
< less then 2 < 13 2<1
<= less then or equal 3<=4 3 <= 2
> greater thenl 14 > 5 5 > 14
>= greater then or equal 3>=2 2 >= 3
Printing
void [Link](String s) print s
void [Link](String s) print s
Followed by new line
print a new line
void [Link]()
codingknowledge_
harish
if and if else statement
codingknowledge_
harish
Nested if-else statement.
// 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)
[Link]("x is greater than y and z"); //
statement1.
}
else
[Link]("x is less than or equal to y"); //
statement2.
codingknowledge_
harish
if-else if Ladder Statements in Java
if(condition)
statement1;
else if(condition)
statement2;
else if(condition)
statement3;
...
else
statement4;
[Link]
codingknowledge_
harish
while loop.
Loop Continuation condition
int power = 1;
while ( power <= n/2)
{
power = 2*power;
}
Brackets are option when body BODY
is a single statement
codingknowledge_
harish
For Loop
Loop Continuation
declare and initialize a
condition
loop control value
Increment
int power = 1;
for ( int i = 0 ; i<=n ; i++)
{
[Link]( i + " " + power)
power = 2*power
}
Body
comment “java” and get it’s
complete pdf in your Dm 🔥
codingknowledge_
harish
Loops.
codingknowledge_
harish
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;
}
codingknowledge_
harish
Arrays
Single Dimensional Array in Java
Syntax to Declare an Array in Java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Instantiation of an Array in Java
arrayRefVar=new datatype[size];
codingknowledge_
harish
Arrays
Single Dimensional Array in Java
int a[]=new int[5];//declaration and
instantiation
a[0]=27;//initialization
a[1]=25; a[2]=75;
a[3]=47; a[4]=59;
codingknowledge_
harish
Arrays
Multidimensional Array in Java
Syntax to Declare Multidimensional Array in Java
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
Example to instantiate Multidimensional Array in Java
int[][] arr=new int[3][3];//3 row and 3 column
codingknowledge_
harish
Arrays
Multidimensional Array in Java
Example to initialize Multidimensional Array in Java
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;
codingknowledge_
harish
Functions
Return Type Method name Argument Value
public static double funtionName(int n)
{
Argument Type
double sum =0.0;
for (int i = 1 ; i<=n ; i++)
sum += 1.0 / i;
return sum; Signature
Return Statement
}
Local Variable
codingknowledge_
harish
Functions
codingknowledge_
harish
Classes