Java cheatSheet
Java cheatSheet
Note: Java program file name should exactly match with class name and should be
same as any predefined classes like String, Array.
Data Types
Primitive Data Types
i) Integer Types:
1) byte (8 bits); Example: byte a =10;
2) short (16 bits); Example: short a =1000;
3) Integer (32 bits); Example: int i = 10000;
4) long (64 bits); Example: long l =100000000000L;
ii) Relational types (Numbers with decimal places):
1) float (32 bits); Example: float f = 1.23f or (float) 1.23;
2) double (64 bits); Example: double d =123.4567890;
ii) Characters:
1) Character (16 bits); Example: char c =’Z’ iv) Conditional
2) Boolean (1 bit); Example: boolean b = true;
Non-Primitive Data Types
Non-primitive or Reference data types in Java are Objects, Class, Interface, String and
Arrays.
Example: String str = “Java World”;
Variables in Java
a) Local variable: a) Instance variable:
Local variable is Instance variables are declared inside the
declared within methods or blocks. class but outside the body of the method.
It is called instance variable because its
value is instance specific and is not
shared among instances.
Example:
public class Test{
int i1=10; //instance variable
public void testMethod(){
int i3=30; //local variable
}};
Operator
Arithmetic Operators: Relational Operators:
1) Addition + (for Addition, String 1) == comparison (equals)
concatenation)
2) ! = Not equal
2) Subtraction – (for Subtraction)
3) > greater than
3) Multiplication *
4) >=greaterthan equal to
4) Division /
5) < lessthan
5) Modules %
6) <=lessthan equal to
6) Increment ++
7) Decrement —
Assignment Operators: Logical Operators:
1) Assignment Operator: = 1) Logical Not Operator: - !
2) Add and Assign: += 2) Logical And Operator: && executes
the loop when all the given conditions are
3) Subtract and assign: -= true.
4) Multiple and assign: *= 3) Logical Or Operator:|| ->executes the
loop with any one condition to be true.
Control Statements
Looping Statements
for loop
syntax : for(initialization ;condition; increment/decrement) {
// code to be executed }
while loop
while(condition){ // code to be executed }
for –each loop do-while loop
for (type var : array) {
do
// code to be executed} {code to be executed} while(condition)
Jump Statement
Break: Continue:
When Condition satisfied then stops the When Condition satisfied then skips the
execution of the loop and continues with current iteration and the next block of code
the next block of code
Nested If If-else-if
If(condition) {if(condition){ code to be If(condition)
executed }} {code to be executed };
else if{code to be executed};
else if{code to be executed};
else{Code to be Executed};
Switch case
Switch(expression)
{ case exp1:
Statement 1;
break;
case exp2:
Statement 2;
break;
default
default statement;
break;
}
Java Objects
Objects –represents the state and behavior of the class variable and methods
Syntax to call class variables and methods:
ClassName obj=new ClassName();
Java Execution starts with main method:
Main method signature is as public static void main(String [ ] args)
Sample code:
public class LearnVariables{
{
int b = 15;
public static void main(String[] args) {
LearnVariables var= new LearnVariables();
System.out.println(var.b);
}
}
Access Specifiers
- explain the scope/visibility of class variables and methods of a Java Program
Public: accessible to all class (Global)
Private: accessible only to the current class object
Protected: accessible to the current class, subclasses and the packages
Default: accessible only to class and the package
*Packages: includes the group of classes/subclasses