The Java Program: Literal.java

  1 
  2 class Literal {
  3 
  4    public static void main (String[] args) {
  5 
  6       final String s1 = new String ("hello world1");  // Expensive
  7       final String s2 = "hello world2";               // Cheap
  8       System.out.println (s1);
  9       System.out.println (s2);
 10 
 11       String s3 = "";    // Wasteful object allocation
 12       if (Math.cos (0.0) > 0.0) {
 13          s3 = "positive";
 14       } else {
 15          s3 = "non-positive";
 16       }
 17       
 18       final String s4;
 19       if (Math.cos (0.0) > 0.0) {
 20          s4 = "positive";
 21       } else {
 22          s4 = "non-positive";
 23       }
 24 
 25       final String s5 = (Math.cos (0.0) > 0.0) ? "positive" : "non-positive";
 26    }
 27 }