Tutorialspoint

ES6 Binary Representation

console.log(0b001) 
console.log(0b010) 
console.log(0b011) 
console.log(0b100)

SortedMap Interface in Java Programming

import java.util.*;
public class TreeMapDemo {

   public static void main(String args[]) {
   
      // Create a hash map
      TreeMap tm = new TreeMap();
      
      // Put elements to the map
      tm.put("Zara", new Double(3434.34));
      tm.put("Mahnaz", new Double(123.22));
      tm.put("Ayan", new Double(1378.00));
      tm.put("Daisy", new Double(99.22));
      tm.put("Qadir", new Double(-19.08));
      
      // Get a set of the entries
      Set set = tm.entrySet();
      
      // Get an iterator
      Iterator i = set.iterator();
      
      // Display elements
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }
      System.out.println();
      
      // Deposit 1000 into Zara's account
      double balance = ((Double)tm.get("Zara")).doubleValue();
      tm.put("Zara", new Double(balance + 1000));
      System.out.println("Zara's new balance: " + tm.get("Zara"));
   }
}

Set Interface in Java Programming

import java.util.*;
public class SetDemo {

  public static void main(String args[]) { 
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<Integer>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);

         TreeSet sortedSet = new TreeSet<Integer>(set);
         System.out.println("The sorted list is:");
         System.out.println(sortedSet);

         System.out.println("The First element of the set is: "+ (Integer)sortedSet.first());
         System.out.println("The last element of the set is: "+ (Integer)sortedSet.last());
      }
      catch(Exception e) {}
   }
} 

Private and final methods in Java Programming

public class PrivateFinalMethods {
   private void print() {
      System.out.println("in parent print");
   }
   public static void main(String[] args) {
   
      PrivateFinalMethods obj = new PrivateFinalMethodsChild();
      obj.print();
      PrivateFinalMethodsChild obj1 = new PrivateFinalMethodsChild();
      obj1.print();
   }
}
class PrivateFinalMethodsChild extends PrivateFinalMethods {
   public void print(){
      System.out.println("in child print method");
   }
}

ES6 valueOf()

var num = new Number(10); 
console.log(num.valueOf());

Private Constructors and Singleton Classes in Java Programming

public class SingletonClassDriver {
   public static void main(String[] args) {
   
      SingletonClass obj = SingletonClass.getObject();
      SingletonClass obj1 = SingletonClass.getObject();
      System.out.println(obj);
      System.out.println(obj1);
   }
}
class SingletonClass {
   public static SingletonClass obj = null;
      private SingletonClass() {
   }
   public static SingletonClass getObject() {
      if (obj == null) {
         obj = new SingletonClass();
      }
      return obj;
   }
}

ES6 toString()

var num = new Number(10); 
console.log(num.toString()); 
console.log(num.toString(2)); 
console.log(num.toString(8));

Print a 2 D Array or Matrix in Java Programming

public class Print2DArray {
   public static void main(String[] args) {
   
      final int[][] matrix = {
         { 1, 2, 3 },
         { 4, 5, 6 },
         { 7, 8, 9 }
      };
      for (int i = 0; i < matrix.length; i++) { //this equals to the row in our matrix.
         for (int j = 0; j < matrix[i].length; j++) { //this equals to the column in each row.
            System.out.print(matrix[i][j] + " ");
         }
         System.out.println(); //change line on console as row comes to end in the matrix.
      }
   }
}

ES6 toPrecision()

var num = new Number(7.123456); 
console.log(num.toPrecision()); 
console.log(num.toPrecision(1)); 
console.log(num.toPrecision(2));

Null Pointer Exception in Java Programming

public class Tester {
   public static void main(String[] args) {
      Object ref = null;
      ref.toString(); // this will throw a NullPointerException
   }
}

Advertisements
Loading...