2

Might be its stupid question!!

Suppose

 public class A{

            Long weight = 1200L;  
            Integer i = 10; 
            int z = 20;
            A a;  } 


   public class B extends A{

   public static void main(String[] args) { 

              B b = new B(); 
              B c = new B();
              B d = new B(); 
      }

   }

I want to know how many objects are going to be created? what i know , 3 objects corresponding to b , c , d and Long , Integer ,int for each reference. Moreover , each object corresponding to b , c ,d has instance variable "a".

Is this correct? If so , does it mean if i have an instance variables with wrapper class or any pre-defined class , i own object wrt defined class (in mycase Integer and Long)?

Thanks in advance

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
user2985842
  • 437
  • 9
  • 24
  • 1
    By the way, an `int` variable doesn't reference an object. – Dawood ibn Kareem Feb 06 '14 at 19:19
  • int is like A...normal instance variable – user2985842 Feb 06 '14 at 19:21
  • 1
    I missed that main was creating instances of B. 9 objects. – Hot Licks Feb 06 '14 at 19:25
  • An object is created when you use the object creator keyword `new`, so in this case you have 3 objects. As for `Long` and `Integer` they are just wrapper classes for `int` and `long` respectively. Unless you create an integer like this `Integer i = new Integer(10);`, `i` just holds a primitive variable. – Omoro Feb 06 '14 at 19:28
  • 2
    @Omoro: an Integer is an Integer. It can't be an int. A variable of type Integer references an Object, or doesn't reference anything (null). – JB Nizet Feb 06 '14 at 19:30
  • @JB Nizet indeed, you are right. – Omoro Feb 06 '14 at 19:34

2 Answers2

4

You are correct about the count of Bs being created. I think the hard part of the question is designed to check if you understand what happens when

  1. A field of a primitive type is defined (field z)
  2. A field of an object type is defined and initialized (fields weight and i)
  3. A field of an object type is defined but not initialized (field a)
  4. A field is initialized with an object of a wrapped primitive between -128 and +127

The answer is that an object is created only in the second case. Fields of primitive types are not objects, and uninitialized fields are nulls.

Note that weight and i are primitives in object wrappers. Numbers between -128 and +127 are special because of interning.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • What about the three instances of B themselves? – Hot Licks Feb 06 '14 at 19:25
  • @HotLicks Oops, I forgot to write about it. OP got it right, though. Thanks! – Sergey Kalinichenko Feb 06 '14 at 19:27
  • could you explain more A field of an object type is defined but not initialized (field a) A field is initialized with an object of a wrapped primitive between -128 and +128 – user2985842 Feb 06 '14 at 19:29
  • 1
    @user2985842 The first `B` will create an object for `B`, for `weight`, and for `i`. At this point `i` will be interned. We've got three objects so far. The second `B` will create a `B` again, and it will also create `weight`. However, `i` will be pulled from the collection of interned `Integer`s, so that's an extra two objects, not three. The last `B` will also create two more objects - for the same reason. So that's the total of 7 objects. Note that if you change the definition to `int i = 200;`, object count will be 9, not 7. – Sergey Kalinichenko Feb 06 '14 at 19:32
  • @DavidWallace You're right, it's +127, not +128. I fixed the answer, thanks! By the way, when you see an obvious error like that, feel free to edit - I like good edits :-) Thanks! – Sergey Kalinichenko Feb 06 '14 at 19:47
  • This is a really good question. It's made me think a lot. Unfortunately, all this thinking is dangerous. I don't agree with this answer any longer, and I have removed my upvote. Sorry, dasblinkenlight. Integer caching is not the same as String interning. I will post an answer of my own shortly. – Dawood ibn Kareem Feb 06 '14 at 19:59
  • @DavidWallace In the implementation of `Integer` and its `IntegerCache` static inner class the array of interned `Integer`s is created in the static constructor. The static constructor is invoked when the class gets used for the first time. Assuming that no other code exists in the system in addition to OPs, the call to create all the 256 `Integer`s is performed at the time when the `i` of the first object `B` is initialized. That's why I think it is fair to attribute the creation of `Integer(10)` to OP's code. – Sergey Kalinichenko Feb 06 '14 at 20:21
  • Hmm, by that argument, the answer to this question is 263 - the 256 Integers, the Integer[], and the six other objects. But then, you'd have to count anything else created by the class loader. Not to mention the class loader itself. Where do we stop? – Dawood ibn Kareem Feb 06 '14 at 20:24
0

Java maintains an internal cache of 256 Integer objects, one for every value from -128 to 127. When an int value within this range is autoboxed, an Integer object from this cache is used - that is, a new Integer is not created.

Therefore, the values assigned to the variables b.i, c.i and d.i by this code are all references to the objects in the internal cache - new Integer objects are not created by this code at all.

When this code runs, three Long objects are created, for the variables b.weight, c.weight and d.weight. Three objects of class B are created, for the variables b, c and d. But this code itself does not create objects other than these six.

The answer to this question is six.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110