3

How many objects will be created for this code?

class Main {
  int num;
  public static void gacemarks(Main m)
  {
    m.num += 10;
  }
  public static void main(String[] args) {
    Main m1 = new Main();
    Main m2 = m1;
    Main m3 = new Main();
    m2.num = 60;
    gacemarks(m2);
    System.out.println(m2);
  }
}

The answer is 2. But I got 3. m1 will be created, m2 refers to the same object m3 is created newly and after the call, the m object is generated.

6
  • I know its a duplicate question. But i was not able to understand this code. Commented Apr 1, 2019 at 13:39
  • 2
    You are confusing object references (m1,m2,m3,m) with the objects they refer to. Objects are only created when a new statement (or clone() method, but that's another story) is called. Commented Apr 1, 2019 at 13:41
  • m1 and m2 refer to one Main object. m3 refers to another Main object. That's 2 Main objects total. Commented Apr 1, 2019 at 13:41
  • Try System.out.println(m2.num) after gacemarks(m2);. You should get 70 then because m is a reference to the object that is refered to by m2. (I assume that System.out.println(m2) doesn't show that because you didn't override toString()). Commented Apr 1, 2019 at 13:47
  • It did return the hex code !!! @Thomas Commented Apr 1, 2019 at 14:45

3 Answers 3

3

In the context of your code, the only two objects I see being explicitly created are the lines in which the new operator appears:

Main m1 = new Main();
Main m3 = new Main();

Here is a breakdown of which is happening in each line:

Main m1 = new Main();     // create new Main object 'm1'
Main m2 = m1;             // assign 'm2' to reference 'm1' (no new object)
Main m3 = new Main();     // create new Main object 'm3'
m2.num = 60;              // assign a field in 'm2' (no new object)
gacemarks(m2);            // repeatedly increment the 'm2.num' field (no new object)
System.out.println(m2);   // print 'm2' (no new object)
Sign up to request clarification or add additional context in comments.

4 Comments

So, the call to the method does not create a new object, instead, it works on the same object reference being passed.
@NemalyPraveen Yes, that's right. At least, when you reference the field num, you are incrementing the field of the object you passed in as a parameter.
@NemalyPraveen you might have heard that Java is call by value and this might be what's confusing you. That's true but there's a catch: when you pass a reference to an object, e.g. in gacemarks(m2);, the value is the reference itself, i.e. m is a copy of m2 but they both are just references (similar to pointers in C++) to the same object.
Yup, @Thomas these can be referred to as object references being passed by value.
2

The answer is 2

That's right. Let's count together:

m1 will be created

Right, that's your object number one.

m2 refers to the same object

Right again, no new objects added; the count remains one.

m3 is created newly

That's your number two.

and after the call, the m object is generated.

There's no m object in the code: when you pass m2 to other methods, no copy is created. Hence the final count remains two.

3 Comments

So, by code you are referring to the main method ??
@NemalyPraveen no he's refering to all your code. m is a reference to the object that has been passed so it doesn't refer to a new object.
@NemalyPraveen Yes, that's where all the action is happening. No code is executed unless main invokes it, directly or indirectly.
2

Although there are two Main objects created directly in the code (with new operator) one can argue that System.out.println(m2); creates an additional String object due to toString() being called.

5 Comments

Good catch +1. Maybe this should be an interview question.
@TimBiegeleisen another one could be String[] args array created when calling main(). It's hard to tell what the question is really about.
Actually, you're opening a Pandora's box, because you don't know how many objects are created there. It depends on the implementation. There is a string created in Integer.toHexString(), and then it's concatenated to the other stuff like class name (which may or may not create an object) to create another string.
Well I understand "The answer is 2. But I got 3." as "The correct answer is 2. But I got 3, which is wrong." - so I'd doubt all those other objects, including the string, are taken into account. :)
I guess it usually refers to the objects being created by the user. Not the ones that are generated automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.