⬅ Previous TopicGo Cheat Sheet
Next Topic ⮕JavaScript Cheat Sheet
Primitive Types
int age = 25;
double price = 9.99;
char grade = 'A';
boolean isActive = true;
int age = 25;
double price = 9.99;
char grade = 'A';
boolean isActive = true;
int number = 10;
final double PI = 3.14159; // constant
int sum = 5 + 3;
int product = 5 * 2;
boolean isEqual = (a == b);
boolean result = (x > 5) && (y < 10);
int i = (int) 3.14; // explicit
float f = 10; // implicit
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Hello " + name);
if (x > 0) {
System.out.println("Positive");
} else {
System.out.println("Non-positive");
}
switch (day) {
case 1: System.out.println("Mon"); break;
}
for (int i = 0; i < 5; i++) {}
while (x > 0) {}
do {} while (x > 0);
for (int i = 0; i < 10; i++) {
if (i == 5) continue;
if (i == 8) break;
}
class Dog {
String name;
void bark() {
System.out.println("Woof");
}
}
Dog d = new Dog();
d.name = "Rex";
Dog(String name) {
this.name = name;
}
Dog(String name, int age) { ... }
private int id;
public void setId(int id) { this.id = id; }
class Animal {}
class Cat extends Animal {
@Override void speak() {}
}
abstract class Shape { abstract void draw(); }
interface Drawable { void draw(); }
static int count;
final int MAX = 100;
class Outer { class Inner {} }
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Cleanup");
}
void risky() throws IOException {}
throw new IllegalArgumentException("Bad input");
int[] nums = {1, 2, 3};
ArrayList list = new ArrayList<>();
LinkedList ll = new LinkedList<>();
HashMap map = new HashMap<>();
HashSet set = new HashSet<>();
Stack stack = new Stack<>();
Queue queue = new LinkedList<>();
Collections.sort(list);
String s = "hello";
s.length();
s.toUpperCase();
s.substring(1,3);
StringBuilder sb = new StringBuilder("hi");
sb.append(" there");
String a = "test";
String b = new String("test");
a == b // false
a.equals(b) // true
enum Color { RED, GREEN, BLUE; }
class Box { T value; }
Runnable r = () -> System.out.println("Hello");
List list = Arrays.asList("a", "b", "c");
list.stream().filter(x -> x.equals("a"));
@FunctionalInterface
interface MyFunc { void run(); }
@interface MyAnnotation {}
import java.util.*;
void printAll(String... args) {}
class MyThread extends Thread {
public void run() {
System.out.println("Running");
}
}
Thread t = new MyThread();
t.start();
Runnable r = () -> {};
new Thread(r).start();
synchronized void syncMethod() {}
Thread.sleep(1000);
t.join();
wait(); notify();
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.obj"));
oos.writeObject(obj);
// java.util: Collections, Date, Scanner
// java.lang: Math, String, Object (auto-imported)
// java.io: File, BufferedReader, InputStream
// JVM: Executes bytecode
// JRE: JVM + standard libraries
// JDK: JRE + development tools
public static void main(String[] args) {
System.out.println("Hello Java");
}
for (String arg : args) {
System.out.println(arg);
}
System.gc();
// Access JavaDoc: use javadoc tool or IDE shortcut
Comments