Core Java Concepts Cheat Sheet by 😸CattoDoesCode
public class Main {
public static void main(String[] args) {
// write your code here
}
}
System.out.println("Hello World");
// This is a sample comment.
/* This is a sample
multi-line comment. */
Data Type | Size | Description | Range |
---|---|---|---|
byte |
8-bit | signed two's complement integer | -128 to 127 |
short |
16-bit | signed two's complement integer | -32,768 to 32,767 |
int |
32-bit | signed two's complement integer | -231 to 231-1 (i.e., -2147483648 to 2147483647 ) |
long |
64-bit | two's complement | -263 to 263-1 (i.e., -9223372036854775808 to `9223372036854775807 |
float |
32-bit | IEEE 754 floating point | Negative range: -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 |
double |
64-bit | IEEE 754 floating point | Negative range: -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308 |
boolean |
1-bit | has only two possible values | true or false |
char |
16-bit | single 16-bit Unicode character | '\u0000' (or 0) to '\uffff' (or 65,535 inclusive) |
String
in Java is not a primitive data type but an object that are backed by char array.
<DataType> <variableName>;
sample code:
int numVariable; // variable name is in camelCase
char my_variable; // variable name is in snake_case
Data Type | Default Value |
---|---|
byte |
0 |
short |
0 |
int |
0 |
long |
0L |
float |
0.0f |
double |
0.0d |
char |
'\u0000' |
boolean |
false |
String (or any object) |
null |
<variableName> = <value or expression>;
sample code:
numVariable = 100;
my_variable = 'x'; // single quotation marks ('') for char and double quotation marks for string("")
String myString = "Java";
Method | Description |
---|---|
lenth() |
returns the length of characters inside the string |
toUpperCase |
converts a string into upper case |
toLowerCase |
converts a string into lower case |
indexOf("locate") |
returns the index (the position) of the first occurrence of a specified text in a string (including whitespace) |
for more string methods: https://www.w3schools.com/java/java_ref_string.asp
sample code:
public class Main {
public static void main(String[] args) {
String vowels = "AEIOU";
System.out.println("length of vowels: " + vowels.length());
}
}
output:
length of vowels: 5
syntax:
if (condition1) {
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
sample code:
public class Main {
public static void main(String[] args) {
int x = 100;
int y = 50;
if (x < y) {
System.out.println("x is less than y");
}
else if (x == y) {
System.out.println("x is equal to y");
}
else {
System.out.println("x is greater than y");
}
}
}
output:
x is greater than y
syntax:
variable = (condition) ? expressionTrue : expressionFalse
sample code:
public class Main {
public static void main(String[] args) {
int x = 100;
int y = 50;
String result = (x < y) ? "x is less than y" : "x is greater than y";
System.out.println(result);
}
}
output:
x is greater than y
syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The
break
keyword terminates a switch statement or a loop statement.
sample code:
public class Main {
public static void main(String[] args) {
int choice = 3;
switch(choice) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
}
}
}
public class Main {
public static void main(String[] args) {
int choice = 3;
switch (choice) {
case 1 -> System.out.println("one");
case 2 -> System.out.println("two");
case 3 -> System.out.println("three");
}
}
}
output:
three
syntax:
for ((initialization); (condition); (increment)) {
// code block to be executed
}
sample code:
public class Main {
public static void main(String[] args) {
for (int i = 0; i <= 10; i+=1) {
System.out.println(i);
}
}
}
output:
0
1
2
3
4
5
6
7
8
9
10
syntax:
while(condition) {
// code block to be executed
}
sample code:
public class Main {
public static void main(String[] args) {
int i = 0;
while(i <= 10) {
System.out.println(i);
i++;
}
}
}
output:
0
1
2
3
4
5
6
7
8
9
10
syntax:
do {
// code block to be executed
}
while (condition);
sample code:
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}
while(i <= 10);
}
}
output:
0
1
2
3
4
5
6
7
8
9
10
syntax:
<dataType>[] <variable> = <value>;
sample code:
String[] ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
public class Main {
public static void main(String[] args) {
int[] tens = {10, 20, 30, 40, 50, 60, 70, 80, 100};
System.out.println(tens[2]);
}
}
output:
30
public class Main {
public static void main(String[] args) {
int[] tens = {10, 20, 30, 40, 50, 60, 70, 80, 100};
tens[8] = 90;
System.out.println(tens[8]);
}
}
output:
90
public class Main {
public static void main(String[] args) {
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (String d : days) {
System.out.println(d);
}
}
}
output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
static <void> <methodName>() {
// code to be executed
}
keywords:
static
means that the method belongs to the Main class and not an object of the Main class.void
means that this method does not have a return value.
or
static <dataType> <methodName>() {
// code to be executed
return <value or expression>
}
If you want the method to return a value, you can use a primitive data type (such as
int
,char
, etc.) instead ofvoid
, and use thereturn
keyword inside the method:
sample code:
public class Main {
static void hello_world(){
System.out.println("Hello, world!");
}
public static void main(String[] args) {
hello_world();
}
}
output:
Hello, world!
sample code:
public class Main {
static String greet_user(String user){
return "Hello, " + user;
}
public static void main(String[] args) {
System.out.println(greet_user("Bob"));
}
}
output:
Hello, Bob
- Java Documentation | docs.oracle.com
- mathcenter.oxford.emory.edu
- w3schools.com
- javatpoint.com
- geeksforgeeks.org