Posts

Showing posts with the label output

Java Puzzle : Integer Initialization

Here is a simple program which instantiate Integer object using primitive int values with all possible ways, new operator, AutoBoxing , Integer.valueOf() . 1: package com.test; 2:   3: public class IntegerPuzzle { 4: public static void main(String[] args) { 5: Integer a = new Integer(100); 6: Integer b = new Integer(100); 7: Integer c = 100; 8: Integer d = 100; 9: Integer e = Integer.valueOf(100); 10: Integer f = 150; 11: Integer g = 150; 12: 13: System.out.println( "a = b ? " + (a==b)); 14: System.out.println( "c = d ? " + (c==d)); 15: System.out.println( "d = e ? " + (d==e)); 16: System.out.println( "a = c ? " + (a==c)); 17: System.out.println( "f = g ? " + (f==g)); 18: } 19: } What would be the output of  this program ?

Understanding Observer Designing Pattern by Example

Image
  “The Observer pattern define a one to many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.” With the observer pattern, the source is the object that contain the state and control it. The Observer on the other hand use the state, even if they don’t own it. The observer pattern provides an object design where the subject and observer are loosely coupled. Here is the Class Diagram explaining Benefit of loosely coupling The only thing the subject knows about an Observer is that it implements a certain interface We can add new Observer at any time We never need to modify subject to add more observer. We can use subject or observer independently of each other. Change in either observer or Source will not affect the other. Here is an example : This example explains Observer pattern, here source generates random characters and observer, Java Swing component listen this eve...

Java Puzzle – Magic of Auto-boxing

  Auto-boxing is introduced in JDK 5.0.  it converts primitive data variable to corresponding wrapper classes and vice versa without any explicit efforts. For example int to Integer and Integer to int . Here is the one program using auto-boxing feature 1: class AutoBoxingTest 2: { 3: public static void main(String[] args) 4: { 5: boolean b1= true , b2= true ; 6: System.out.println(" boolean Result = "+ compare(b1,b2)+" \n "); 7: System.out.println(" Int Result = "+ compare(10,10)+" \n "); 8: System.out.println(" String Result = "+ compare(" ABC "," ABCE ")+" \n "); 9: System.out.println(" float Result = "+ compare(10.00f,10f)+" \n "); 10: System.out.println(" Mixed Result = "+ compare(10l,10l)+" \n "); 11: System.out.println(" Mixed[long,int...