Core Java Cheat Sheet
Core Java Cheat Sheet
Core Java Cheat Sheet
Let’s start off by learning the primitive data types that Java offers:
Data Type Size Range
byte 8 -128..127
short 16 -32,768..32,767
Java Operators
Logical &&, ||
Relational <, >, <=, >=,==, !=
Shift <<, >>, >>>
Ternary ?:
Unary ++x, –x, x++, x–, +x, –x, !, ~
Java Variables
Variables in Java refers to the name of the reserved memory area. You
need variables to store any value for the computational or reference
purpose.
1. Local Variables
2. Instance Variables
3. Static Variables
{public | private} [static] type name [= expression | value];
Java Methods
A method is a set of code that is grouped together to perform a
specific operation. A method is completed in two steps:
1. Method Initialization
2. Method Invocation
Data Conversion
The process of changing a value from one data type to another type is
known as data type conversion. Data Type conversion is of two types:
User Input
Java provides three ways to take an input from the user/ console:
2. Methods
3. Variables
public class Demo{
public static void main(String[] args)
{ System.out.println("Hello from edureka!");}
}
Iterative statements are used when you need to repeat a set of statements
until the condition for termination is not met.
// for loop
for (condition) {expression}// for each loop
for (int i: someArray) {} // while loopwhile (condition)
{expression} // do while loopdo {expression} while(condition)
Decisive Statements
if (n < 2) { return false; } for (int i=2; i <= n/i; i++) {if (n%i
== 0) return false;}return true;
int factorial(int n)
{ if (n == 0)
{return 1;} else
{return(n * factorial(n-1));} }
Java Arrays
Single Dimensional (1-D)
Reversing an array.
for(int i=0; i<(arr.length())/2; i++)
{ double temp = a[i];
a[i] = a[n-1-i];
a[n-1-i] = temp;
}
Multi Dimensional (2-D)
Transposing a matrix.
for(i = 0; i < row; i++){ for(j = 0; j < column; j++)
{ System.out.print(array[i][j]+" "); } System.out.println(" ");}
1. Using a literal
2. Using ‘new’ keyword
String str1 = “Welcome”; // Using literal
String str2 = new String(”Edureka”); // Using new keyword
Few of the most important and frequently used String methods are
listed below:
str1==str2 //compares address;
String newStr = str1.equals(str2); //compares the valuesString
newStr = str1.equalsIgnoreCase() //compares the values ignoring
the casenewStr = str1.length() //calculates length
newStr = str1.charAt(i) //extract i'th character
newStr = str1.toUpperCase() //returns string in ALL CAPS
newStr = str1.toLowerCase() //returns string in ALL
LOWERvCASEnewStr = str1.replace(oldVal, newVal) //search and
replace
newStr = str1.trim() //trims surrounding whitespace
newStr = str1.contains("value"); //check for the values
newStr = str1.toCharArray(); // convert String to character type
array
newStr = str1.IsEmpty(); //Check for empty String
newStr = str1.endsWith(); //Checks if string ends with the given
suffix