import java.util.Date; public class Example { public static void main(String[] args) throws Exception { Date date = new Date(); System.out.printf("Two-digit Year = %ty\n",date); } }
public class Example { public static void main(String[] args) { String s = "OneAndOnly"; if(s.startsWith("one")) { System.out.println("Begins with the specified word!"); } else { System.out.println("Does not begin with the specified word!"); } } }
public class Demo { public void func() { int num; System.out.println("The number is : " + num); } public static void main(String args[]) { Demo obj = new Demo(); obj.func(); } }
上面的程序包含一个局部变量num. 导致错误“变量num可能尚未初始化”
上面程序的正确版本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Demo { public void func() { int num = 50; System.out.println("The number is : " + num); } public static void main(String args[]) { Demo obj = new Demo(); obj.func(); } }
import java.lang.*; public class ThreadPriorityDemo extends Thread { public static void main(String[]args) { ThreadPriorityDemo thread1 = new ThreadPriorityDemo(); ThreadPriorityDemo thread2 = new ThreadPriorityDemo(); ThreadPriorityDemo thread3 = new ThreadPriorityDemo(); System.out.println("Default thread priority of thread1: " + thread1.getPriority()); System.out.println("Default thread priority of thread2: " + thread2.getPriority()); System.out.println("Default thread priority of thread3: " + thread3.getPriority()); thread1.setPriority(8); thread2.setPriority(3); thread3.setPriority(6); System.out.println("New thread priority of thread1: " + thread1.getPriority()); System.out.println("New thread priority of thread2: " + thread2.getPriority()); System.out.println("New thread priority of thread3: " + thread3.getPriority()); } }
**上面程序的输出如下:**
Default thread priority of thread1: 5 Default thread priority of thread2: 5 Default thread priority of thread3: 5 New thread priority of thread1: 8 New thread priority of thread2: 3 New thread priority of thread3: 6
class A { int x = 26; static int y = 15; } public class B extends A { void display() { System.out.println(super.x); System.out.println(super.y); } public static void main(String[] args) { B obj = new B(); obj.display(); } }
public class Demo { int x = 25; static int y = 12; void display() { this.x = 250; System.out.println(x); this.y = 120; System.out.println(y); } public static void main(String[] args) { Demo obj = new Demo(); obj.display(); } }
import static java.lang.Thread.currentThread; import java.util.concurrent.TimeUnit; public class Demo { public static void main(String args[]) throws InterruptedException { Server ser = new Server(); Thread thread = new Thread(ser, "T1"); thread.start(); System.out.println(currentThread().getName() + " is stopping Server thread"); ser.stop(); TimeUnit.MILLISECONDS.sleep(200); System.out.println(currentThread().getName() + " is finished now"); } } class Server implements Runnable { private volatile boolean exit = false; public void run() { while(!exit) { System.out.println("The Server is running"); } System.out.println("The Server is now stopped"); } public void stop() { exit = true; } }
**上面程序的输出如下:**
main is stopping Server thread The Server is running The Server is now stopped main is finished now
class Parent { private void display() { System.out.println("Super class"); } } public class Example extends Parent { //using the Override annotation @Override void display() // trying to override display() { System.out.println("Sub class"); } public static void main(String[] args) { Parent obj = new Example(); obj.display(); } }
**输出如下:**
$javac Example.java Example.java:11: error: method does not override or implement a method from a supertype @Override ^ Example.java:19: error: display() has private access in Parent obj.display(); ^ 2 errors
import java.util.*; public class Example { public static void main(String args[]) { List<Integer> list = new ArrayList<Integer>(); for(int i=1;i<=10;i++) // adding 1 to 10 in the integer ArrayList { list.add(i); } // printing even elements in list using lambda expression list.forEach(arg -> { if (arg%2 == 0) System.out.println(arg); }); } }
public class Example { public static void main(String args[]) { String s1 = "Same string"; String s2 = "same string"; int a1= s1.compareTo(s2); int a2=s1.compareToIgnoreCase(s2); if(a1<0) System.out.println("String s2 is greater"); else if(a1>0) System.out.println("String s1 is greater"); else System.out.println("String s1 is equal to String s2"); if(a2==0) System.out.println("After Ignoring the case, s1 and s2 are equal"); else System.out.println("Even after ignoring the case, s1 and s2 are not equal"); } }
**输出如下:**
$javac Example.java $java Example String s2 is greater After Ignoring the case, s1 and s2 are equal
public class Example { public static void main(String x) { System.out.println(x+" World"); } public static void main(String a, int b) { System.out.println(a+","+b); } public static void main(String []args) { System.out.println("Hello from public static void main(String []args)!"); main("Hello"); main("Hello",2); } }
**输出如下:**
$javac Example.java $java -Xmx128M -Xms16M Example Hello from public static void main(String []args)! Hello World Hello,2
Before generics type casting was required. List l = new ArrayList(); l.add("apple"); String str = (String) list.get(0); // type casting After generics type casting was no longer needed. List<String> l = new ArrayList<String>(); list.add("apple"); String str = list.get(0); // no type casting
public class Demo { public static void main(String[] args) { System.out.println("class loader for this class: " + Demo.class.getClassLoader()); System.out.println("class loader for DNSNameService: " + sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader()); System.out.println("class loader for HashMap: " + java.util.HashMap.class.getClassLoader()); } }
上面程序的输出如下:
class loader for this class: sun.misc.Launcher$AppClassLoader@4e0e2f2a class loader for DNSNameService: sun.misc.Launcher$ExtClassLoader@5c647e05 class loader for HashMap: null
import java.util.Calendar; public class Example { public static void main(String[] args) { Calendar c = Calendar.getInstance(); System.out.println(c.getTime().toString()); System.out.println("Day = " + c.get(Calendar.DAY\_OF\_WEEK)); } }
public class Demo { public static void main(String args[]) { int num = 2413, rev = 0; System.out.println("The number is " + num); while(num != 0) { rev = rev \* 10; rev = rev + num % 10; num = num / 10; } System.out.println("Reverse of the above number is " + rev); } }
上面程序的输出如下:
The number is 2413 Reverse of the above number is 3142
public class Demo { public static void main(String[] args) { int num1 = 2147483647; int num2 = 1; System.out.println("Number 1: " + num1); System.out.println("Number 2: " + num2); long sum = (long)num1 + (long)num2; if (sum > Integer.MAX_VALUE) { throw new ArithmeticException("Overflow occurred!"); } System.out.println("The sum of two numbers is: " + (int)sum); } }
**上面程序的输出如下:**
Number 1: 2147483647 Number 2: 1 Exception in thread "main" java.lang.ArithmeticException: Overflow occurred! at Demo.main(Demo.java:14)