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.
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.
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 String
s "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 String
s or any kind of object really unless you know exactly what you are doing and why you are doing it this way.
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
The question is primarily opinion-based, because of the two used Strings "a"
and "b"
.
So what are "a"
and "b"
?
String Literals
.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.