toString() ================== class Employee { private int salary; // default, int is zero public String toString() { return this.salary + ""; } } class Main { public static void main(String[] args) { Employee e1 = new Employee(); System.out.println(e1); } } == vs .equals ============== public class Main { public static void main(String[] args) { String s1 = "testing"; String s2 = "test"; s2 = s2 + "ing"; System.out.println(s1==s2); //false System.out.println(s1.equals(s2)); //true String s3 = s1; System.out.println(s1==s3); //true System.out.println(s1.equals(s3)); //true } } .length or array ================ public class Main { public static void main(String[] args) { int[] arr = new int[3]; System.out.println(arr.length); arr.length=2; } } static method, non-static field =============================== class Day { private int year, month, day; static public boolean isLeapYear(int y) { if (y % 400 == 0) return true; else if (y % 100 == 0) return false; else if (y % 4 == 0) return true; else return false; } } public class Main { public static void main(String[] args) { boolean result = Day.isLeapYear(2023); System.out.println(result); } } //System.out.println(this.year); //System.out.println(year); exchange salary/object ================ class Employee { private int salary; public Employee(int s) { salary = s; } public String toString() { return this.salary+""; } public static void swapSalary(Employee e1, Employee e2) { int temp = e1.salary; e1.salary = e2.salary; e2.salary = temp; } public static void swapEmployee(Employee e1, Employee e2) { Employee temp = e1; e1 = e2; e2 = temp; } } public class Main { public static void main(String[] args) { Employee a = new Employee(100); Employee b = new Employee(200); Employee.swapEmployee(a, b); System.out.println(a + ", " + b); // Output: 100,200 Employee.swapSalary(a, b); System.out.println(a + ", " + b); // Output: 200,100 // swapEmployee's e1 copies Main's a. // => Changing e1 doesn't mean change a. // swapSalary's e1 and Main's a SEES the same object. // => So, salary swapped. ^_^ } }