0% found this document useful (0 votes)
7 views12 pages

Java Cheat Sheet for Beginners

This document is a Java cheat sheet designed for beginners, covering basic syntax, data types, control structures, and essential programming concepts. It includes examples of variables, loops, conditionals, arrays, and the Java Collections Framework. The cheat sheet serves as a quick reference for compiling and running Java programs, as well as manipulating strings and arrays.

Uploaded by

manthrisowmya1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Java Cheat Sheet for Beginners

This document is a Java cheat sheet designed for beginners, covering basic syntax, data types, control structures, and essential programming concepts. It includes examples of variables, loops, conditionals, arrays, and the Java Collections Framework. The cheat sheet serves as a quick reference for compiling and running Java programs, as well as manipulating strings and arrays.

Uploaded by

manthrisowmya1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

[Link] Stars 6.8k

Java
This cheat sheet is a crash course for Java beginners and help review the basic syntax of the Java language.

India to Dubai India to Thailand

from India from India

from Rs14,838 Search from Rs12,110 Search


*

# Getting Started
[Link]

public class Hello {


// main method
public static void main(String[] args)
{
// Output: Hello, world!
[Link]("Hello, world!");
}
}

Compiling and running

$ javac [Link]
$ java Hello
Hello, world!

Variables

int num = 5;
float floatNum = 5.99f;
char letter = 'D';

[Link] 1/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference
boolean bool = true;
String site "quickref me";
Primitive Data Types

Data Type Size Default Range

byte 1 byte 0 -128 to 127

short 2 byte 0 -215 to 215-1

int 4 byte 0 -231 to 231-1

long 8 byte 0 -263 to 263-1

float 4 byte 0.0f N/A

double 8 byte 0.0d N/A

char 2 byte \u0000 0 to 65535

boolean N/A false true / false

Strings

String first = "John";


String last = "Doe";
String name = first + " " + last;
[Link](name);

See: Strings

Loops

String word = "QuickRef";


for (char c: [Link]()) {
[Link](c + "-");
}
// Outputs: Q-u-i-c-k-R-e-f-

See: Loops

Arrays

char[] chars = new char[10];


chars[0] = 'a'
chars[1] = 'b'

String[] letters = {"A", "B", "C"};


int[] mylist = {100, 200};
boolean[] answers = {true, false};

See: Arrays

Swap

int a = 1;
int b = 2;
[Link](a + " " + b); // 1 2

int temp = a;
a = b;
b = temp;
[Link](a + " " + b); // 2 1

[Link] 2/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference
Type Casting

// Widening
// byte<short<int<long<float<double
int i = 10;
long l = i; // 10

// Narrowing
double d = 10.02;
long l = (long)d; // 10

[Link](10); // "10"
[Link]("10"); // 10
[Link]("10"); // 10.0

Conditionals

int j = 10;

if (j == 10) {
[Link]("I get printed");
} else if (j > 10) {
[Link]("I don't");
} else {
[Link]("I also don't");
}

See: Conditionals

User Input

Scanner in = new Scanner([Link]);


String str = [Link]();
[Link](str);

int num = [Link]();


[Link](num);

# Java Strings
Basic

String str1 = "value";


String str2 = new String("value");
String str3 = [Link](123);

Concatenation

String s = 3 + "str" + 3; // 3str3


String s = 3 + 3 + "str"; // 6str
String s = "3" + 3 + "str"; // 33str
String s = "3" + "3" + "23"; // 3323
String s = "" + 3 + 3 + "23"; // 3323
String s = 3 + 3 + 23; // 29

StringBuilder

StringBuilder sb = new StringBuilder(10);

[Link] 3/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| | | | | | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9

[Link]("QuickRef");

┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | R | e | f | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9

[Link](5, 9);

┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9

[Link](0, "My ");

┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9

[Link]("!");

┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | ! |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9

Comparison

String s1 = new String("QuickRef");


String s2 = new String("QuickRef");

s1 == s2 // false
[Link](s2) // true

"AB".equalsIgnoreCase("ab") // true

Manipulation

String str = "Abcd";

[Link](); // ABCD
[Link](); // abcd
[Link]("#"); // Abcd#
[Link]("b", "-"); // A-cd

" abc ".trim(); // abc


"ab".toCharArray(); // {'a', 'b'}

Information

String str = "abcd";

[Link](2); // c
[Link]("a") // 0

[Link] 4/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference
[Link]("z") // -1
[Link](); // 4
[Link](); // abcd
[Link](2); // cd
[Link](2,3); // c
[Link]("c"); // true
[Link]("d"); // true
[Link]("a"); // true
[Link](); // false

Immutable

String str = "hello";


[Link]("world");

// Outputs: hello
[Link](str);

String str = "hello";


String concat = [Link]("world");

// Outputs: helloworld
[Link](concat);

Once created cannot be modified, any modification creates a new String

# Java Arrays
Declare

int[] a1;
int[] a2 = {1, 2, 3};
int[] a3 = new int[]{1, 2, 3};

int[] a4 = new int[3];


a4[0] = 1;
a4[2] = 2;
a4[3] = 3;

Modify

int[] a = {1, 2, 3};


[Link](a[0]); // 1

a[0] = 9;
[Link](a[0]); // 9

[Link]([Link]); // 3

Loop (Read & Modify)

int[] arr = {1, 2, 3};


for (int i=0; i < [Link]; i++) {
arr[i] = arr[i] * 2;
[Link](arr[i] + " ");
}
// Outputs: 2 4 6

Loop (Read)

[Link] 5/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

String[] arr = {"a", "b", "c"};


for (int a: arr) {
[Link](a + " ");
}
// Outputs: a b c

Multidimensional Arrays

int[][] matrix = { {1, 2, 3}, {4, 5} };

int x = matrix[1][0]; // 4
// [[1, 2, 3], [4, 5]]
[Link](matrix)

for (int i = 0; i < [Link]; ++i) {


for(int j = 0; j < a[i].length; ++j) {
[Link](a[i][j]);
}
}
// Outputs: 1 2 3 4 5 6 7

Sort

char[] chars = {'b', 'a', 'c'};


[Link](chars);

// [a, b, c]
[Link](chars);

# Java Conditionals
Operators

+ - * /

% = ++ --

== != > >=

< <=

&& || ?:

instanceof

~ << >> >>>

& ^ |

If else

int k = 15;
if (k > 20) {
[Link](1);
} else if (k > 10) {

[Link] 6/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference
[Link](2);
} else {
[Link](3);
}

Switch

int month = 3;
String str;
switch (month) {
case 1:
str = "January";
break;
case 2:
str = "February";
break;
case 3:
str = "March";
break;
default:
str = "Some other month";
break;
}

// Outputs: Result March


[Link]("Result " + str);

Ternary operator

int a = 10;
int b = 20;
int max = (a > b) ? a : b;

// Outputs: 20
[Link](max);

# Java Loops
For Loop

for (int i = 0; i < 10; i++) {


[Link](i);
}
// Outputs: 0123456789

for (int i = 0,j = 0; i < 3; i++,j--) {


[Link](j + "|" + i + " ");
}
// Outputs: 0|0 -1|1 -2|2

Enhanced For Loop

int[] numbers = {1,2,3,4,5};

for (int number: numbers) {


[Link](number);
}
// Outputs: 12345

[Link] 7/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

U dt l d ' Li t'
While Loop

int count = 0;

while (count < 5) {


[Link](count);
count++;
}
// Outputs: 01234

Do While Loop

int count = 0;

do {
[Link](count);
count++;
} while (count < 5);
// Outputs: 01234

Continue Statement

for (int i = 0; i < 5; i++) {


if (i == 3) {
continue;
}
[Link](i);
}
// Outputs: 01245

Break Statement

for (int i = 0; i < 5; i++) {


[Link](i);
if (i == 3) {
break;
}
}
// Outputs: 0123

# Java Collections Framework


Java Collections

Collection Interface Ordered Sorted Thread safe Duplicate Nullable

ArrayList List Y N N Y Y

Vector List Y N Y Y Y

LinkedList List, Deque Y N N Y Y

CopyOnWriteArrayList List Y N Y Y Y

HashSet Set N N N N One null

LinkedHashSet Set Y N N N One null

TreeSet Set Y Y N N N

[Link] 8/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

Collection Interface Ordered Sorted Thread safe Duplicate Nullable

CopyOnWriteArraySet Set Y N Y N One null

ConcurrentSkipListSet Set Y Y Y N N

HashMap Map N N N N (key) One null (key)

HashTable Map N N Y N (key) N (key)

LinkedHashMap Map Y N N N (key) One null (key)

TreeMap Map Y Y N N (key) N (key)

ConcurrentHashMap Map N N Y N (key) N

ConcurrentSkipListMap Map Y Y Y N (key) N

ArrayDeque Deque Y N N Y N

PriorityQueue Queue Y N N Y N

ConcurrentLinkedQueue Queue Y N Y Y N

ConcurrentLinkedDeque Deque Y N Y Y N

ArrayBlockingQueue Queue Y N Y Y N

LinkedBlockingDeque Deque Y N Y Y N

ArrayList

List<Integer> nums = new ArrayList<>();

// Adding
[Link](2);
[Link](5);
[Link](8);

// Retrieving
[Link]([Link](0));

// Indexed for loop iteration


for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}

[Link]([Link]() - 1);
[Link](0); // VERY slow

for (Integer value : nums) {


[Link](value);
}

HashMap

Map<Integer, String> m = new HashMap<>();


[Link](5, "Five");
[Link](8, "Eight");
[Link](6, "Six");
[Link](4, "Four");
[Link](2, "Two");

// Retrieving
[Link]([Link](6));

[Link] 9/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference
// Lambda forEach
[Link]((key, value) -> {
String msg = key + ": " + value;
[Link](msg);
});

HashSet

Set<String> set = new HashSet<>();


if ([Link]()) {
[Link]("Empty!");
}

[Link]("dog");
[Link]("cat");
[Link]("mouse");
[Link]("snake");
[Link]("bear");

if ([Link]("cat")) {
[Link]("Contains cat");
}

[Link]("cat");
for (String element : set) {
[Link](element);
}

ArrayDeque

Deque<String> a = new ArrayDeque<>();

// Using add()
[Link]("Dog");

// Using addFirst()
[Link]("Cat");

// Using addLast()
[Link]("Horse");

// [Cat, Dog, Horse]


[Link](a);

// Access element
[Link]([Link]());

// Remove element
[Link]([Link]());

# Misc
Access Modifiers

Modifier Class Package Subclass World

public Y Y Y Y

protected Y Y Y N

[Link] 10/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

Modifier Class Package Subclass World

difi Y Y N N
Regular expressions

String text = "I am learning Java";


// Removing All Whitespace
[Link]("\\s+", "");

// Splitting a String
[Link]("\\|");
[Link]([Link]("|"));

See: Regex in java

Comment

// I am a single line comment!

/*
And I am a
multi-line comment!
*/

/**
* This
* is
* documentation
* comment
*/

Keywords

abstract continue for new switch assert

default goto package synchronized boolean do

if private this break double implements

protected throw byte else import public

throws case enum instanceof return transient

catch extends int short try char

final interface static void class finally

long strictfp volatile const float native

super while

Math methods

[Link](a,b) Maximum of a and b

[Link](a,b) Minimum of a and b

[Link](a) Absolute value a

[Link](a) Square-root of a

[Link](a,b) Power of b

[Link](a) Closest integer

[Link] 11/13
09/01/2025, 23:59 Java Cheat Sheet & Quick Reference

[Link](ang) Sine of ang

[Link](ang) Cosine of ang

[Link](ang) Tangent of ang

[Link](ang) Inverse sine of ang

[Link](a) Natural logarithm of a

[Link](rad) Angle rad in degrees

Try/Catch/Finally

try {
// something
} catch (Exception e) {
[Link]();
} finally {
[Link]("always printed");
}

Related Cheatsheet

C# Cheatsheet
Quick Reference

Recent Cheatsheet
[Link] 12/13

You might also like