1
MyTest[] dates = new MyTest[3];
dates[0] = new MyTest("a");
dates[1] = new MyTest("b");

I think five objects are created.
The teacher thought that there were three.
I wonder how many objects are created.

ric
  • 627
  • 1
  • 12
  • 23
kRaon
  • 15
  • 1
  • If you count the array `dates` as an object, then yes, three object will be created. If you do not count `dates`, then two objects are created (the one referenced by `dates[0]` and `dates[1]`). – Turing85 Dec 13 '17 at 11:51
  • 1
    I assume he 's referring to "a", "b", new MyTest[3], new MyTest("a") and new MyTest("b") – Stultuske Dec 13 '17 at 11:51

3 Answers3

2

Well here goes the third answer to this question. Long story short: We cannot give you a definitive answer since it depends on what and how you count.

Long story long: There are at lest two objects created, namely the ones created with the explicit call to MyTest's constructor:

dates[0] = new MyTest("a");
dates[1] = new MyTest("b");

Whether or not you count MyTest[] dates = new MyTest[3]; depends on whether or not you would define an array as an object. If you do count an array as an object, then at least three objects are created.

Since Arrays are objects in Java, you count

MyTest[] dates = new MyTest[3];

as well, so we have at least three objects. Thanks to @Eran for the hint.

There are actually two more objects used, namely the Strings "a" and "b", that are passed as arguments to the constructor of MyTest. Now Java uses an optimization called constant string pooling. Whenever the compiler encounters a String that is written in " it will create a String of that value. If a String-constant with the value does already exist, it is reused. This is why

String a = "Hello";
String b = "Hello";
String c = "Hell" + "o";
System.out.println(a == b);
System.out.println(b == c);

will produce the output1

true
false

So if you count those String-constants, two more objects are created.

Thus, the answer is either 3 or 5 objects are created, depending on how you count.


1 Never ever ever use == to compare Strings or any kind of object really unless you know exactly what you are doing and why you are doing it this way.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • @Turing85 why do you say either 3 or 5? there is only one answer to the question and its 5 (2 of which are constants). – nafas Dec 13 '17 at 12:30
  • @nafas if this the sole code in the whole program, then yes, five objects are created. If, for whatever reason, the `String` constants `"a"` and `"b"` were created before, then only trhee objects get created. – Turing85 Dec 13 '17 at 18:50
1

Atleast 3 or as more as 5 is the correct answer: This is what I think the class is like.

class MyTest(){
   /*Constructor that takes an argument.*/
   MyTest(String test){
     //do something here.
   }
}

MyTest[] dates = new MyTest[3]; //Created as array object of size 3 but it holds null references so far.

dates[0] = new MyTest("a"); //Creates a new object using the constructor and assign to index 0 of data[]

dates[1] = new MyTest("b"); //Creates a new object using the constructor and assign to index 1 of data[]

The string passed to the constructor could be a new object or they can already exist in the string pool. So the object can range from anything from 3-5.

String strObject = new String("Java");
and
String strLiteral = "Java";

Both expression gives you String object, but there is subtle difference between them. When you create String object using new() operator, it always create a new object in heap memory. On the other hand, if you create object using String literal syntax e.g. "Java", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

Read more: Reference

Some insight on array being objects in Java

Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22
  • Please look at @Eran's comment to [this answer](https://stackoverflow.com/a/47792386/4216641). You should explain why you are not counting the Strings `"a"` and `"b"`, which are passed to `MyTest`'s constructor. – Turing85 Dec 13 '17 at 12:06
  • Updated the answer to reflect that. – Sadiq Ali Dec 13 '17 at 12:10
0

The question is primarily opinion-based, because of the two used Strings "a" and "b".

So what are "a" and "b"?

Are String Literals Objects?

String literals are references to instances of class String.

And the Java Language Specification only says

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

Which indicates that the String Literals are internal constants and must be created before the certain part of the code is executed.


But the question is How many Objects are created when this code is executed and here you can argue, because the String Literals must be created before the shown code is executed.