CORE JAVA CHEATSHEET Learn JAVA from experts at https://www.edureka.
co
Java Programming Iterative Statements Arrays In Java
Java is a high level, general purpose programming // for loop 1 - Dimensional language that produces software for multiple platforms. It for (condition) {expression} was developed by James Gosling in 1991 and released by // Initializing Sun Microsystems in 1996 and is currently owned by // for each loop type[] varName= new type[size]; Oracle. for (int i: someArray) {} User -Defined Exceptions // Declaring // while loop type[] varName= new type[]{values1, value2,...}; Primitive Data Types while (condition) {expression} Array with Random Variables Type Size Range // do while loop byte 8 -128..127 do {expression} while(condition) double[] arr = new double[n]; short 16 -32,768..32,767 for (int i=0; i<n; i++) int 32 -2,147,483,648.. 2,147,483,647 {a[i] = Math.random();} Fibonacci series long 64 9,223,372,036,854,775,808.. 9,223.. float 32 3.4e-0.38.. 3.4e+0.38 for (i = 1; i <= n; ++i) Maximum value in an Array double 64 1.7e-308.. 1.7e+308 { double max = 0; char 16 Complete Unicode Character Set System.out.print(t1 + " + "); for (int i=0; i<arr.length(); i++) Boolean 1 True, False int sum = t1 + t2; t1 = t2; { if(a[i] > max) max = a[i]; } t2 = sum; } Java Operators Reversing an Array for(int i=0; i<(arr.length())/2; i++) Type Operators Pyramid Pattern { double temp = a[i]; Arithmetic +, – , *, ? , % a[i] = a[n-1-i]; k = 2*n - 2; a[n-1-i] = temp; } Assignment =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= for(i=0; i<n; i++) Bitwise ^, &, | { Logical &&, || for(j=0; j<k; j++){System.out.print(" ");} Multi – Dimensional Arrays Relational <, >, <=, >=,==, != k = k - 1; // Initializing for(j=0; j<=i; j++ ){System.out.print("* ");} datatype[][] varName = new dataType[row][col]; Shift <<, >>, >>> System.out.println(); // Declaring Ternary ?: } datatype[][] varName = {{value1, value2....},{value1, Unary ++x, –x, x++, x–, +x, –x, !, ~ value2....}..};
Java Variables Decisive Statements Transposing A Matrix
for(i = 0; i < row; i++) {public|private} [static] type name [= expression|value]; //if statement { for(j = 0; j < column; j++) if (condition) {expression} { System.out.print(array[i][j]+" "); } Java Methods System.out.println(" "); //if-else statement } {public|private} [static] {type | void} name(arg1, ..., if (condition) {expression} else {expression} argN ){statements} Multiplying two Matrices //switch statement switch (var) { case 1: expression; break; for (i = 0; i < row1; i++) Data Type Conversion default: expression; break; } { for (j = 0; j < col2; j++) { for (k = 0; k < row2; k++) // Widening (byte<short<int<long<float<double) { sum = sum + first[i][k]*second[k][j]; } int i = 10; //int--> long Prime Number multiply[i][j] = sum; long l = i; //automatic type conversion if (n < 2) sum = 0; } } // Narrowing { double d = 10.02; return false; long l = (long)d; //explicit type casting // Numeric values to String } Java Strings for (int i=2; i <= n/i; i++) String str = String.valueOf(value); { // String to Numeric values if (n%i == 0) return false; // Creating String using literal int i = Integer.parseInt(str); } String str1 = “Welcome”; double d = Double.parseDouble(str); return true; // Creating String using new keyword String str2 = new String(”Edureka”); User Input Factorial of a Number // Using BufferReader int factorial(int n) { String Methods BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); if (n == 0) String name = reader.readLine(); {return 1;} str1==str2 //compare the address; // Using Scanner else String newStr = str1.equals(str2); //compares the values Scanner in = new Scanner(System.in); { String s = in.nextLine(); return(n * factorial(n-1)); String newStr = str1.equalsIgnoreCase() // int a = in.nextInt(); } newStr = str1.length() //calculates length // Using Console } newStr = str1.charAt(i) //extract i'th character String name = System.console().readLine(); newStr = str1.toUpperCase() //returns string in ALL CAPS newStr = str1.toLowerCase() //returns string in ALL LOWERCASE Basic Java Program newStr = str1.replace(oldVal, newVal) //search and replace
public class Demo newStr = str1.trim() //trims surrounding whitespace
Save className.java { newStr = str1.contains("value"); //Check for the values public static void main(String[] args) newStr = str1.toCharArray(); //Convert into character array { Compile javac className System.out.println("Hello from edureka!"); newStr = str1.IsEmpty(); //Check for empty String } newStr = str1.endsWith(); //Checks if string ends with the given suffix } Execute java className