Core Java Cheat Sheet
Core Java Cheat Sheet
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