Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Core Java Cheatsheet: A Quick Reference Guide to Java Fundamentals, Cheat Sheet of Java Programming

In this cheat sheet there is a clear overview on Java Programming

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

jeny
jeny 🇺🇸

4.6

(14)

251 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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

Partial preview of the text

Download Core Java Cheatsheet: A Quick Reference Guide to Java Fundamentals and more Cheat Sheet Java Programming in PDF only on Docsity!

CORE JAVA CHEATSHEET Learn JAVA from experts at https://www.edureka.co

Java Programming^ Iterative Statements

Java is a high level, general purpose programming language that produces software for multiple platforms. It was developed by James Gosling in 1991 and released by Sun Microsystems in 1996 and is currently owned by Oracle.

Arrays In Java

User - Defined Exceptions // for loop for (condition) {expression} // for each loop for (int i: someArray) {} // while loop while (condition) {expression} // do while loop do {expression} while(condition)

Primitive Data Types

Type Size Range byte 8 - 128.. short 16 - 32,768..32, int 32 - 2,147,483,648.. 2,147,483, long 64 9,223,372,036,854,775,808.. 9,223.. float 32 3.4e-0.38.. 3.4e+0. double 64 1.7e-308.. 1.7e+ char 16 Complete Unicode Character Set Boolean 1 True, False

Java Operators

Type Operators Arithmetic +, – , *,? , % Assignment =, +=, - =, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= Bitwise ^, &, | Logical &&, || Relational <, >, <=, >=,==, != Shift <<, >>, >>> Ternary? : Unary ++x, – x, x++, x–, +x, – x, !, ~ {public|private} [static] type name [= expression|value];

Java Variables

{public|private} [static] {type | void} name(arg1, ..., argN ){statements}

Java Methods

Data Type Conversion

// Widening (byte<short<int<long<float<double) int i = 10; //int--> long long l = i; //automatic type conversion // Narrowing double d = 10.02; long l = (long)d; //explicit type casting // Numeric values to String String str = String.valueOf(value); // String to Numeric values int i = Integer.parseInt(str); double d = Double.parseDouble(str);

User Input

// Using BufferReader BufferedReader reader = new BufferedReader(new 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!"); } } Save Compile Execute className.java javac className java className for (i = 1; i <= n; ++i) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; }

Fibonacci series

k = 2n - 2; for(i=0; i<n; i++) { for(j=0; j<k; j++){System.out.print(" ");} k = k - 1; for(j=0; j<=i; j++ ){System.out.print(" ");} System.out.println(); }

Pyramid Pattern

Decisive Statements

//if statement if (condition) {expression} //if-else statement if (condition) {expression} else {expression} //switch statement switch (var) { case 1: expression; break; default: expression; break; } if (n < 2) { return false; } for (int i=2; i <= n/i; i++) { if (n%i == 0) return false; } return true;

Prime Number

// Creating String using literal String str1 = “Welcome”; // Creating String using new keyword String str2 = new String(”Edureka”); str1==str2 //compare the address; String newStr = str1.equals(str2); //compares the values String newStr = str1.equalsIgnoreCase() // newStr = 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 LOWERCASE newStr = 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 into character array newStr = str1.IsEmpty(); //Check for empty String newStr = str1.endsWith(); //Checks if string ends with the given suffix

1 - Dimensional

// Initializing type[] varName= new type[size]; // Declaring type[] varName= new type[]{values1, value2,...}; double[] arr = new double[n]; for (int i=0; i<n; i++) {a[i] = Math.random();}

Array with Random Variables

double max = 0; for (int i=0; i<arr.length(); i++) { if(a[i] > max) max = a[i]; }

Maximum value in 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; }

Reversing an Array

// Initializing datatype[][] varName = new dataType[row][col]; // Declaring datatype[][] varName = {{value1, value2....},{value1, value2....}..};

Multi – Dimensional Arrays

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

Transposing A Matrix

for (i = 0; i < row1; i++) { for (j = 0; j < col2; j++) { for (k = 0; k < row2; k++) { sum = sum + first[i][k]*second[k][j]; } multiply[i][j] = sum; sum = 0; } }

Multiplying two Matrices

Java Strings

int factorial(int n) { if (n == 0) {return 1;} else { return(n * factorial(n-1)); } }

Factorial of a Number

String Methods