import java.util.Scanner; // switch supports strings public class StringSwitch { public static void main(String[] args) { Scanner s= new Scanner(System.in); System.out.print("Enter the name of programming language you use : "); String lang = s.nextLine(); // read a line from keyboard switch(lang) { case "java" : System.out.println("Java Programmer"); break; case "c#" : System.out.println(".Net Programmer"); break; case "c++" : System.out.println("C++ Programmer"); break; default: System.out.println("Programmer"); break; } } }
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
Map<String, List<String>> anagrams = new HashMap<>();
FileOutputStream fos = new FileOutputStream(path); try { // code to process fos } catch(Exception ex) { System.out.println(ex); } finally { fos.close(); }
try (FileOutputStream fos = new FileOutputStream(path)) { // code to process fos } catch(Exception ex) { System.out.println(ex); }
int n = 0b10000000; System.out.println(n); // prints 128 int n2 = 121_31_23_232; // integer literal with underscores int n3 = 0xff_dd; // hexa literal with underscores System.out.println(n2); System.out.println(n3);
try { int v = Integer.parseInt(num); int result = 100 / v; System.out.println(result); } catch(NumberFormatException | ArithmeticException ex ) { // multi-catch System.out.println("Not a valid number or zero"); }
class Ex1 extends Exception { } class Ex2 extends Exception { } public class RethrowException { public void m1(int v) throws Ex1 , Ex2 { try { if (v < 0) { throw new Ex1(); } else if ( v > 100 ) { throw new Ex2(); } // process } catch (Exception ex) { throw ex; // unreported exception java.lang.Exception; must be caught or declared to be thrown } } }