0% found this document useful (0 votes)
124 views

Java Cheat Sheet

The document provides an overview of basic Java concepts for beginners, including how to write a simple Hello World program, data types, operators, control flow statements like if/else and loops, functions, and classes. It explains concepts like declaring and initializing variables, arrays, and how to compile and run a Java program. The cheat sheet covers many fundamental Java building blocks to help newcomers learn the basics of the Java programming language.

Uploaded by

Sourav Debnath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Java Cheat Sheet

The document provides an overview of basic Java concepts for beginners, including how to write a simple Hello World program, data types, operators, control flow statements like if/else and loops, functions, and classes. It explains concepts like declaring and initializing variables, arrays, and how to compile and run a Java program. The cheat sheet covers many fundamental Java building blocks to help newcomers learn the basics of the Java programming language.

Uploaded by

Sourav Debnath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Java CheatSheet

For Beginners

HelloWorld.java File Name

Class Name
Main Method
public class HelloWorld {

public static void main(String[] args) {

//Print "Hello World " in the terminal window


System.out.println("Hello World")

}
} 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

Type java HelloWorld to


HelloWorld.Class
computer-language version of your
execute your program program

"Hello World" JVM

Output
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"


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
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 && || !
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 System.out.print(String s) print s


void System.out.println(String s) print s
Followed by new line

void System.out.println() print a new line


if and if else statement
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)
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.

Loop Continuation condition

int power = 1;
while ( power <= n/2)
{
power = 2*power;
}

Brackets are option when body BODY


is a single statement
For Loop

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

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];
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;
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


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;
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
Functions
Classes

You might also like