Java Cheat Sheet PDF for Quick Reference
Java Cheat Sheet PDF for Quick Reference
Catalog Resources
Email address
Java Cheat Sheet: I accept the
Reference Subscribe
Disclosure: [Link]
is supported by its
audience. When
you purchase
Object-Oriented Programming Language: based through links on our
site, we may earn
on the concepts of “objects”. an affiliate
Open Source: Readily available for development. commission.
[Link] Page 1 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Object-Oriented
Architectural-neutral
High Performance
Multi-Threaded
[Link] Page 2 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Distributed
1 byte = 8 bits
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0 8 byte
[Link] Page 3 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Data Type
String
Array
Class
Interface
Typecasting
It is a method of converting a variable of one data
type to another data type to process these
variables correctly.
Operators in Java
Java supports a rich set of operators that can be
classified as below :
[Link] Page 4 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Conditional operators ?:
Eclipse
NetBeans
[Link] Page 5 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
$ javac [file_name].java
$ java [file_name]
Variables in Java
Variables are the name of the memory location. It
is a container that holds the value while the java
program is executed. Variables are of three types
in Java :
[Link] Page 6 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class TestVariables
{
int data = 20; // instance variable
static int number = 10; //static variab
le
void someMethod()
{
int num = 30; //local variable
}
}
[Link] Page 7 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Reserved Words
Also known as keywords, particular words are
predefined in Java and cannot be used as variable
or object names. Some of the important keywords
are :
Keywords Usage
[Link] Page 8 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Methods in Java
The general form of method :
[Link] Page 9 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class TestIfElse
{
public static void main(String args[])
{
int percent = 75;
if(percent >= 75
{
[Link]("Passed");
}
else
[Link] Page 10 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
{
[Link]("Please attempt agai
n!");
}
}
}
2. Switch
class TestSwitch
{
public static void main(String args[])
{
int weather = 0;
switch(weather)
{
case 0 :
[Link]("Sunny");
break;
case 1 :
[Link]("Rainy");
break;
[Link] Page 11 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
case 2 :
[Link]("Cold");
break;
case 3 :
[Link]("Windy");
break;
default :
[Link]("Pleasant");
}
}
}
3. Loops in Java
For Loop
[Link] Page 12 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
++)
[Link]
n("*");
}
}
While Loop
Do While Loop
[Link] Page 13 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 14 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
3. Inheritance
[Link] Page 15 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class A
{
int i, j;
void showij() {
[Link]("i and j: " + i + "
" + j);
}
}
// Create a subclass by extending class
A.
class B extends A {
[Link] Page 16 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
int k;
void showk() {
[Link]("k: " + k);
}
void sum() {
[Link]("i+j+k: " + (i+j+
k));
}
}
class SimpleInheritance {
public static void main(String args[])
{
A objA = new A();
B objB = new B();
// The superclass may be used by itself
objA.i = 10;
objA.j = 20;
[Link]("Contents of objA:
");
[Link]();
[Link]();
objB.i = 7;
objB.j = 8;
[Link] Page 17 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
objB.k = 9;
[Link]("Contents of objB:
");
[Link]();
[Link]();
[Link]();
[Link]("Sum of i, j and k i
n objB:");
[Link]();
}
}
4. Polymorphism
[Link] Page 18 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class ParentMath
{
void area()
{
int a =2;
[Link]("Area of Square with
side 2 = %d %n", a * a);
[Link]();
}
}
class ChildMath extends ParentMath
{
void area()
[Link] Page 19 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
{
int a =4;
[Link]("Area of Square with
side 4= %d %n", a * a);
}
public static void main (String args[])
{
ChildMath obj = new ChildMath();
[Link]();
}
}
Number of parameters
[Link] Page 20 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class Shape
{
void area()
{
[Link]("Area of the followi
ng shapes are : ");
}
}
class Square extends Shape
{
void area(int length)
{
[Link] Page 21 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 22 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class InheritanceOverload
{
public static void main(String[] args)
{
int length = 5;
int breadth = 7;
Shape s = new Shape();
//object of child class square
Square sq = new Square();
//object of child class rectangle
Rectangle rec = new Rectangle();
//object of child class circle
Circle cir = new Circle();
//calling the area methods of all child
classes to get the area of different ob
jects
[Link]();
[Link](length);
[Link](length,breadth);
[Link](length);
}
}
Abstract Class
Superclass only defines a generalized form shared
by all of its subclasses, leaving it to each subclass
[Link] Page 23 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
abstract class A {
abstract void callme();
// concrete methods are still allowed i
n abstract classes
void callmetoo() {
[Link]("This is a concrete
method.");
}
}
class B extends A {
void callme() {
[Link]("B's implementation
of callme.");
}
}
class Abstract {
public static void main(String args[])
{
B b = new B();
[Link]();
[Link]();
}
}
[Link] Page 24 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Interfaces
A class’s interface can be fully abstracted from its
implementation using the “interface” keyword.
Thus, they are similar to class except that they
lack instance variables, and their methods are
declared without anybody.
interface Area
{
final static float pi = 3.14F;
float compute(float x , float y);
}
class Rectangle implements Area
{
[Link] Page 25 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 26 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Constructors in Java
A constructor initializes an object on creation.
Constructors can be :
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
[Link]("Constructing Box");
width = 10;
height = 10;
[Link] Page 27 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxVol {
public static void main(String args[])
{
// declare, allocate, and initialize Bo
x objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}
class Box {
double width;
[Link] Page 28 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
class BoxVolP {
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}
Arrays in Java
[Link] Page 29 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
`import [Link];`
`class SingleArray`
`{`
[Link] Page 30 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
`{`
`numbers[i] = [Link]();`
`}`
`[Link]("The elements in the
`[Link]();`
`[Link]("The sum of elements in
`for(int i=0;i<len;i++)`
`{`
`sum = sum + numbers[i];`
`}`
`[Link]("Sum of elements = " +
sum);`
`}`
`}``
`
[Link] Page 31 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class MatrixArray
{
public static void main(String args[])
{
int [][] m1 = {{1,2,1},{2,1,1},{1,1,2}};
int [][] m2 = {{2,2,2},{1,1,1},{2,1,2}};
int [][] sum = new int [3][3];
//printing matrix
[Link]("The given matrices are : ");
for(int a=0;a<[Link];a++)
{
for(int b=0;b<[Link];b++)
{
[Link](m1[a][b] + " ");
}
[Link]();
}
[Link]();
for(int a=0;a<[Link];a++)
{
for(int b=0;b<[Link];b++)
{
[Link] Page 32 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Strings in Java
Strings are a non-primitive data type that
[Link] Page 33 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Creating String
String Methods
[Link] Page 34 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 35 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class SortStrings {
static String arr[] = {
"Now", "the", "is", "time", "for", "al
[Link] Page 36 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 37 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class StringBufferTest {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hel
lo");
[Link]("buffer = " + sb);
[Link]("length = " + [Link]
gth());
[Link]("capacity = " + sb.c
apacity());
}
}
[Link] Page 38 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Multithreading
Multitasking: Process of executing multiple tasks
simultaneously to utilize the CPU.
Process-based multitasking.
(Multitasking)
Thread-based multitasking
(Multithreading)
[Link] Page 39 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Multitasking Multithreading
cost-effective expensive
[Link] Page 40 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Creating Thread
[Link] Page 41 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 42 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 43 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 44 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
}
}
class ThreadTest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
[Link] Page 45 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 46 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 47 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
ArrayIndexOutOfBoundException
caused by bad array
indexes
[Link] Page 48 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
StringIndexOutOfBoundException
caused when a program
attempts to access a non-
existent character
position in a string.
Checked Exceptions
Unchecked Exceptions
[Link] Page 49 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class Error
{
public static void main(String args[])
{
int a [] = ;
int b = 5;
try
{
int x = a[2]/b-a[1];
}
catch(ArithmeticException e)
{
[Link]("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexError");
[Link] Page 50 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
}
catch(ArrayStoreException e)
{
[Link]("Wrong data type");
}
int y = a[1]/a[0];
[Link]("y = " + y);
}
}
Finally
finally
{
int y = a[1]/a[0];
[Link]("y = " + y);
}
import [Link];
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class TestMyException
{
public static void main(String args[])
{
int x = 5 , y = 1000;
try
{
float z = (float) x / (float) y ;
if(z < 0.01)
{
throw new MyException("Number is too sm
all");
[Link] Page 52 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
}
}
catch (MyException e)
{
[Link]("Caught my exception
");
[Link]([Link]());
}
finally
{
[Link]("I am always here");
}
}
}
[Link] Page 53 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Creating files
Updating files
Manipulation of data
Streams
Java uses the concept of streams to represent an
ordered sequence of data, a path along which data
flows. Thus, it has a source and a destination.
[Link] Page 54 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Stream Classes
They are contained in the [Link] package.
[Link] Page 55 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Method Description
[Link] Page 56 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Method Description
Reading/Writing Bytes
[Link] Page 57 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link].*;
class WriteBytes
{
public static void main(String args[])
{
bytes cities [] = {'C','A','L','I','
F','O','R','N','I','A', '\n', 'V','E','
G','A','S','\n','R','E','N','O','\n'};
//Create output file stream
FileOutputStream outfile = null;
try
{
//connect the outfile stream to "city.t
xt"
outfile = new FileOutputStream("[Link]
t");
//Write data to the stream
[Link](cities);
[Link]();
[Link] Page 58 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
}
catch(IOException ioe)
{
[Link](ioe);
[Link](-1);
}
}
}
import [Link].*;
class ReadBytes
{
public static void main(String args[])
{
//Create an input file stream
FileInputStream infile = null;
int b;
try
{
//connect the infile stream to required
file
infile = new FileInputStream(args [ 0
[Link] Page 59 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
]);
//Read and display
while( (b = [Link] ( ) ) !=-1)
{
[Link]((char) b );
}
[Link]();
}
catch(IOException ioe)
{
[Link](ioe);
[Link](-1);
}
}
}
[Link] Page 60 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Writes characters
Reading/Writing Characters
import [Link].*;
class CopyCharacters
{
public static void main (String args[])
{
//Declare and create input and output f
iles
[Link] Page 61 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 62 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link]();
[Link]();
}
catch (IOException e)
{}
}
}
}
Java Collections
The collections framework is contained in [Link]
package defines a set of interfaces and their
implementations to manipulate collections, which
serve as containers for a group of objects.
Interfaces
Interface Description
[Link] Page 63 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Classes
[Link] Page 64 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Class Interface
AbstractCollection Collection
AbstarctList List
Abstract Queue
AbstractSequentialList List
LinkedList List
AbstractSet Set
EnumSet Set
HashSet Set
PriorityQueue Queue
TreeSet Set
[Link] Page 65 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link].*;
class Num
{
public static void main(String args[])
{
ArrayList num = new ArrayList ();
[Link](9);
[Link](12);
[Link](10);
[Link](16);
[Link](6);
[Link](8);
[Link](56);
//printing array list
[Link]("Elements : ");
[Link]((s) -> [Link](
s));
//getting size
[Link]("Size of array list
is: ");
[Link]();
//retrieving specific element
[Link] Page 66 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 67 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
HashSet Implementation
import [Link].*;
class HashSetExample
{
public static void main(String args[])
{
HashSet hs = new HashSet();
[Link]("D");
[Link]("W");
[Link]("G");
[Link]("L");
[Link] Page 68 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link]("Y");
[Link]("The elements availa
ble in the hash set are :" + hs);
}
}
import [Link].*;
class TreeSetExample
{
public static void main(String args[])
{
TreeSet ts = new TreeSet();
[Link]("D");
[Link]("W");
[Link]("G");
[Link]("L");
[Link]("Y");
[Link]("The elements availa
ble in the tree set are :" + ts);
}
}
import [Link].*;
[Link] Page 69 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
class VectorExample
{
public static void main(String args[])
{
Vector fruits = new Vector ();
[Link]("Apple");
[Link]("Orange");
[Link]("Grapes");
[Link]("Pineapple");
Iterator it = [Link]();
while ([Link]())
{
[Link]([Link]);
}
}
}
import [Link].*;
public class StackExample
{
public static void main (String args[])
{
Stack st = new Stack ();
[Link]("Java");
[Link]("Classes");
[Link]("Objects");
[Link] Page 70 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link]("Multithreading");
[Link]("Programming");
[Link]("The elements in the
Stack : " + st);
[Link]("The elements at the
top of Stack : " + [Link]());
[Link]("The elements popped
out of the Stack : " + [Link]());
[Link]("The elements in the
Stack after pop of the element : " + s
t);
[Link]("The result of searc
h : " + [Link] ("r e"));
}
}
import [Link].*;
public class HashTableExample
{
public static void main (String args[])
{
Hashtable ht = new Hashtable();
[Link]("Item 1","Apple");
[Link]("Item 2","Orange");
[Link]("Item 3","Grapes");
[Link]("Item 4","Pine");
[Link] Page 71 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link]("Item 5","Kiwi");
Enumeration e = [Link]();
while([Link]())
{
String str = (String) [Link]();
[Link]([Link](str));
}
}
}
Memory management is :
[Link] Page 72 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 73 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link];
class Circle
{
public static void main (String args
[])
{
double r,dia,peri,area ;
[Link]("Enter the radius of
circle : ");
[Link] Page 74 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link];
class EvenOdd
{
public static void main (String args[])
{
int x,y;
Scanner s = new Scanner ([Link]);
[Link]("Enter the values x
, y : ");
[Link] Page 75 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
x = [Link]();
y = [Link]();
[Link](" **** EVEN NUMBERS
BETWEEN GIVEN RANGE ARE **** >> ");
int count = x;
while(count <=y)
{
if(count % 2 == 0)
{
[Link](count);
}
count ++;
}
}
}
import [Link];
class Prime
{
public static void main (String args[])
{
double num;
int n;
[Link] Page 76 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 77 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link];
class Palindrome
{
public static void main(String args[])
{
int num,reverse=0,mode;
Scanner s = new Scanner([Link]);
[Link]("Enter a number to c
heck for Palindrome: ");
num = [Link]();
int number = num;
while(num!=0)
{
//[Link](" number entering
= "+num);
mode = num % 10;
//[Link](" mode = "+mode);
reverse =(reverse * 10 )+ mode;
//[Link](" reverse = "+reve
rse);
num = num/10;
[Link] Page 78 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Pattern printing
*
* *
* * *
* * * *
* * * * *
[Link] Page 79 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
import [Link];
class TriStars
{
public static void main(String args[])
{
for(int i=0;i<=5;i++)
{
for(int j=0;j<i;j++)
{
[Link](" * ");
}
[Link]();
}
[Link]();
}
}
Summary
[Link] Page 80 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 81 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Constructor in java
Explore More
search...
[Link] Page 82 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Learn More
HTML Doctype
How to Convert a Declaration | Docs HTML Text Color |
List to a String in With Examples Docs With Examples
Python (join,
Comprehensions) HTML Programming HTML Programming
Skills Web Skills Web
Python Development Development
[Link] Page 83 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 84 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
[Link] Page 85 of 87
Java Cheat Sheet: Download PDF for Quick Reference 1/10/26, 12:55
Python Editor
[Link] Page 86 of 87