Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Core Java Cheat Sheet

Java Programming Java Variables Iterative Statements


Java is a high level, general purpose programming language that {public|private} [static] type name [= expression|value]; // for loop
for (condition) {expression}
produces software for multiple platforms. It was developed by James // for each loop
Gosling in 1991 and released by Sun Microsystems in 1996 and is Java Methods for (int i: someArray) {}
currently owned by Oracle. // while loop
{public|private} [static] {type | void} name(arg1, ..., while (condition) {expression}
Primitive Data Types argN ){statements} // do while loop
do {expression} while(condition)
Types Size Min Max Data Type Conversion
Fibonacci Series
byte 8 -128 127
// Widening (byte<short<int<long<float<double) for (i = 1; i <= n; ++i)
char 16 216-1 All Unicode characters int i = 10; //int--> long {
16 215-1 From +32,767 to -32,768 long l = i; //automatic type conversion System.out.print(t1 + " + ");
short // Narrowing int sum = t1 + t2; t1 = t2;
31
int 32 2 -1 From +2,147,483,647 to -2,147,483,648 double d = 10.02; t2 = sum;
63
long l = (long)d; //explicit type casting }
long 64 2 -1 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 // Numeric values to String
float -149
(2-2-23)·2127 String str = String.valueOf(value);
32 2 // String to Numeric values Pyramid Pattern
double 64 (2-2-52)·21023 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324 int i = Integer.parseInt(str);
double d = Double.parseDouble(str); k = 2*n - 2;
boolean 1 -- -- for(i=0; i<n; i++)
{
Java Operators User Input for(j=0; j<k; j++){System.out.print(" ");}
k = k - 1;
// Using BufferReader for(j=0; j<=i; j++ ){System.out.print("* ");}
BufferedReader reader = new BufferedReader(new System.out.println();
InputStreamReader(System.in)); }
String name = reader.readLine();
// Using Scanner
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int a = in.nextInt();
// Using Console
String name = System.console().readLine();

Basic Java Program

public class Demo


{
public static void main(String[] args)
{
System.out.println("Hello from edureka!");
}
}
Core Java Cheat Sheet
Decisive Statements Arrays in Java
//if statement
if (condition) {expression} 1-Dimensional Multiplying Two Matrices
//if-else statement
if (condition) {expression} else {expression} //Initializing for (i = 0; i < row1; i++)
//switch statement type[] varName= new type[size]; { for (j = 0; j < col2; j++)
switch (var) { case 1: expression; break; default: // Declaring { for (k = 0; k < row2; k++)
expression; break; } type[] varName= new type[]{values1, value2,...}; { sum = sum + first[i][k]*second[k][j]; }
multiply[i][j] = sum;
Prime Number Array with Random Variables sum=0; }}

if (n < 2)
{
double[] arr = new double[n];
for (int i=0; i<n; i++)
Java Strings
return false; {a[i] = Math.random();}
} // Creating String using literal
For (int i=2; i <= n/i; i++) String str1 = “Welcome”;
{ Maximum Value in Array // Creating String using new keyword
if (n%i == 0) return false; String str2 = new String(”Edureka”);
} double max = 0;
return true; for (int i=0; i<arr.length(); i++) String Methods
{ if(a[i] > max) max = a[i]; }
Factorial of a Number str1==str2 //compare the address;
Reversing an Array String newStr = str1.equals(str2); //compares the values
int factorial(int n) String newStr = str1.equalsIgnoreCase() //
{ newStr = str1.length() //calculates length
for(int i=0; i<(arr.length())/2; i++)
if (n == 0) newStr = str1.charAt(i) //extract i'th character
{ double temp = a[i];
{return 1;} newStr = str1.toUpperCase() //returns string in ALL CAPS
a[i] = a[n-1-i];
else newStr = str1.toLowerCase() //returns string in ALL LOWERCASE
a[n-1-i] = temp; }
{ newStr = str1.replace(oldVal, newVal) //search and replace
return(n * factorial(n-1)); newStr = str1.trim() //trims surrounding whitespace
} Multidimensional Arrays newStr = str1.contains("value"); //Check for the values
} newStr = str1.toCharArray(); //Convert into character array
// Initializing newStr = str1.IsEmpty(); //Check for empty String
newStr = str1.endsWith(); //Checks if string ends with the
datatype[][] varName = new dataType[row][col];
// Declaring given suffix
datatype[][] varName = {{value1, value2....},{value1,
value2....}..};

Transposing a Matrix

for(i = 0; i < row; i++)


{ for(j = 0; j < column; j++)
{ System.out.print(array[i][j]+" "); }
System.out.println(" ");
}

You might also like