The following Java cheat sheet will help review the basic syntax of the language.
The entry point of a Java program is the main()
function. It has the following signature:
public static void main(String args[]) {
// Code goes here...
}
class HelloWorld { public static void main(String args[]) { byte b = 1; System.out.println("Byte: " + b); short s = 50; System.out.println("short: " + s); int i = 255; System.out.println("Integer: " + i); long l = 1500; System.out.println("Long: " + l); float f = 250.6f; System.out.println("Float: " + f); double d = 134567.89; System.out.println("Double: " + d); char c = 'k'; System.out.println("Char: " + c); boolean flag = false; System.out.println("Boolean: " + flag); } }
Type | Minimum Value | Maximum Value |
---|---|---|
byte |
||
short |
||
int |
||
long |
1. String
String
is the most commonly used data type in Java. Some frequently used methods of this class are demonstrated below:
class HelloWorld { public static void main( String args[] ) { String str = new String(" Hello World! "); // Remove leading and trailing whitespaces: str = str.trim(); // Get index of a character: int i = str.indexOf('W'); // Get length of string: int len = str.length(); // Replace each occurrence of a character: String s = str.replace("!", " :)"); // Convert to lowercase: s = s.toLowerCase(); // Opposite to toUpperCase() System.out.println("Original: " + str + "\nLength: " + len + "\nChanged: " + s); } }
2. Arrays
Arrays can be defined and referenced using the following syntax:
In Java, Arrays can be easily converted into a
String
using the statictoString()
method in theArrays
class.
import java.util.*; class HelloWorld { public static void main( String args[] ) { // Declare an int array of size 10: int[] seq = new int[10]; // Populate the array: for(int i = 0; i < seq.length; ++i) seq[i] = i; // Print the array: System.out.println("int array: " + Arrays.toString(seq)); // Declare and fill a char array: char[] s = {'a', 'b', 'c', 'd'}; // Print the char array: System.out.println(s); } }
1. If-else
class HelloWorld { public static void main(String args[]) { int i = 0; if(i < 0) System.out.println("Negative int"); else if (i > 0) System.out.println("Positive int"); else System.out.println("int = 0"); } }
2. Switch statements
class HelloWorld { public static void main(String args[]) { int i = 0; switch(i){ case 0: System.out.println("int = 0"); break; default: System.out.println("int != 0"); } } }
// Interface: interface Workable{ public void powerOn(); public void powerOff(); } // Abstract class: abstract class Machine implements Workable{ private boolean state; protected String name; // Constructor: Machine(){ state = false; setName(); } // Abstract class: public abstract void setName(); // Implemented interface's functions: @Override public void powerOn(){ System.out.println("Powering on " + name); state = true; } @Override public void powerOff(){ System.out.println("Powering off " + name); state = false; } } // Concrete class: class Computer extends Machine{ @Override public void setName(){ name = "PC"; } // Entry point: public static void main(String args[]) { Computer pc = new Computer(); pc.powerOn(); pc.powerOff(); } }
class HelloWorld { public static void main( String args[] ) { // while loop: System.out.print("while loop: "); int i = 0; while(i < 10){ System.out.print(i + " "); ++i; } // for loop: System.out.print("\nfor loop: "); for(; i < 20; ++i) System.out.print(i + " "); // for-each loop: System.out.print("\nfor-each loop: "); char[] arr = {'s','e','n','t','e','n','c','e'}; for(char c: arr) System.out.print(c); } }
RELATED TAGS
View all Courses