Ways to convert Integer into hex, oct, binary…

Java has different ways to do this easily: Integer i = new Integer(54); System.out.println(Integer.toBinaryString(i)); System.out.println(Integer.toHexString(i)); System.out.println(Integer.toString(i, 8)); System.out.println(Integer.toString(i, 2)); System.out.println(i.toOctalString(i)); System.out.println(i.toBinaryString(i));  

Read More

JVM will cache Integer values for numbers between -128 and 127

public class CompareDemo {     public static void main(String[] args) {         Integer i1 = 7;         Integer i2 = 7;          System.out.println("Integer: " + (i1==i2));         //Integer: true         Double d1 = 7d;         Double d2 = 7d;          System.out.println("Double: " + (d1==d2));         //Double: false…

Read More

Polymorphie bei Variablen und Methoden

Dieses Beispiel zeigt , dass bei der Polymorphie die Regeln für Variablen anders sind als für Methoden. Bei Variablen wird immer die deklarierte Variable angesprochen, während bei Methoden die implementierte Methode verwendet wird: class Alpha { int over = 1; String info() {return "A"; }; } class Beta extends Alpha { int over = 2; String info() {return "B";…

Read More

Widening & Boxing

Rules :  1. Primitive Widening > Boxing > Varargs.  2. Widening and Boxing (WB) not allowed.  3. Boxing and Widening (BW) allowed.  4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.  5. Widening between wrapper classes not allowed   

Read More

Threads Notes

  –> Objective 1: Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable.     A thread can be defined and instantiated in one of two ways: case 1. by extending java.lang.Thread (or)    class TestThread extends java.lang.Thread{ public void run(){ //the job that needs to be run goes here….

Read More