Why does Arrays.asList(…).toArray().getClass() give different results in JDK 8 and 9? [duplicate]
up vote
32
down vote
favorite
This question already has an answer here:
array cast Java 8 vs Java 9
2 answers
Why does the following condition return true
with JDK 8, whereas it returns false
with JDK 9?
String.class == Arrays.asList("a", "b").toArray().getClass()
java java-8 java-9
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 min ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
32
down vote
favorite
This question already has an answer here:
array cast Java 8 vs Java 9
2 answers
Why does the following condition return true
with JDK 8, whereas it returns false
with JDK 9?
String.class == Arrays.asList("a", "b").toArray().getClass()
java java-8 java-9
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 min ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
32
down vote
favorite
up vote
32
down vote
favorite
This question already has an answer here:
array cast Java 8 vs Java 9
2 answers
Why does the following condition return true
with JDK 8, whereas it returns false
with JDK 9?
String.class == Arrays.asList("a", "b").toArray().getClass()
java java-8 java-9
This question already has an answer here:
array cast Java 8 vs Java 9
2 answers
Why does the following condition return true
with JDK 8, whereas it returns false
with JDK 9?
String.class == Arrays.asList("a", "b").toArray().getClass()
This question already has an answer here:
array cast Java 8 vs Java 9
2 answers
java java-8 java-9
java java-8 java-9
edited 8 mins ago
Michael
18.2k73268
18.2k73268
asked 12 hours ago
Felix
33617
33617
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 min ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 min ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
40
down vote
The List
type returned by asList
is Arrays$ArrayList
. The toArray
method in JDK 8 on that class is:
@Override
public Object toArray() {
return a.clone();
}
But in JDK 9+ it is:
@Override
public Object toArray() {
return Arrays.copyOf(a, a.length, Object.class);
}
In both cases a String
is passed to asList
, but in the JDK 8 case it is cloned, which retains its array type (String
), and in JDK 9+ it is copied using Arrays.copyOf
with the explicit new array type of Object
.
This difference means that in JDK 8 Arrays.asList("a", "b").toArray().getClass()
returns String
and in JDK 9+ it returns Object
, so in JDK 9+ your expression will evaluate to false
.
The reason for this change comes from JDK-6260652 with the motivation:
The Collection documentation claims that
collection.toArray()
is "identical in function" to
collection.toArray(new Object[0]);
However, the implementation of
Arrays.asList
does not follow this: If created with an array of a subtype (e.g.String
), itstoArray()
will return an array of the same type (because it useclone()
) instead of anObject
.
If one later tries to store non-Strings (or whatever) in that array, an
ArrayStoreException
is thrown.
So this change was made to fix the previous behaviour.
If this is a problem for you, the related release note offers this as a work-around:
If this problem occurs, rewrite the code to use the one-arg form
toArray(T)
, and provide an instance of the desired array type. This will also eliminate the need for a cast.
String array = list.toArray(new String[0]);
It’s so funny that theArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…
– Holger
17 mins ago
add a comment |
up vote
9
down vote
I would say that this was a bug in JDK 8 and before that has been fixed.
List<T>.toArray()
was always declared as returning Object
(see JavaDoc) - that it did in effect return String
in a special case was a mistake.
1
That's incorrect. The method has always returnedObject
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.
– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
add a comment |
up vote
0
down vote
There are couple of reasons here
1) It is a bug in JDK 8 where should return java.lang.Object
but it is not and it is fixed in JDK 9+
@Override
public Object toArray() {
return a.clone();
}
2) ==
operator is always used for reference comparison, so java.lang.String
or java.lang.Object
are objects which are singleton and immutable loaded at the time of JVM starts
System.out.println(String.class.hashCode()); //2018699554
System.out.println(Arrays.asList("a", "b").toArray().getClass().hashCode()); //2018699554
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrongjava.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann
– Deadpool
9 hours ago
2
Nothing in the question is comparing any strings; it's comparing twojava.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.
– Boann
9 hours ago
sorry you are right, my intention is to explain==
opeartor will always comapre references andjava.lang.Object
orjava.lang.String
objects are singleton and immutable and load at JVM starts @Boann
– Deadpool
9 hours ago
That==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the twoClass
objects were being compared via.equals
. Or simply runningSystem.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.
– Boann
9 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
40
down vote
The List
type returned by asList
is Arrays$ArrayList
. The toArray
method in JDK 8 on that class is:
@Override
public Object toArray() {
return a.clone();
}
But in JDK 9+ it is:
@Override
public Object toArray() {
return Arrays.copyOf(a, a.length, Object.class);
}
In both cases a String
is passed to asList
, but in the JDK 8 case it is cloned, which retains its array type (String
), and in JDK 9+ it is copied using Arrays.copyOf
with the explicit new array type of Object
.
This difference means that in JDK 8 Arrays.asList("a", "b").toArray().getClass()
returns String
and in JDK 9+ it returns Object
, so in JDK 9+ your expression will evaluate to false
.
The reason for this change comes from JDK-6260652 with the motivation:
The Collection documentation claims that
collection.toArray()
is "identical in function" to
collection.toArray(new Object[0]);
However, the implementation of
Arrays.asList
does not follow this: If created with an array of a subtype (e.g.String
), itstoArray()
will return an array of the same type (because it useclone()
) instead of anObject
.
If one later tries to store non-Strings (or whatever) in that array, an
ArrayStoreException
is thrown.
So this change was made to fix the previous behaviour.
If this is a problem for you, the related release note offers this as a work-around:
If this problem occurs, rewrite the code to use the one-arg form
toArray(T)
, and provide an instance of the desired array type. This will also eliminate the need for a cast.
String array = list.toArray(new String[0]);
It’s so funny that theArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…
– Holger
17 mins ago
add a comment |
up vote
40
down vote
The List
type returned by asList
is Arrays$ArrayList
. The toArray
method in JDK 8 on that class is:
@Override
public Object toArray() {
return a.clone();
}
But in JDK 9+ it is:
@Override
public Object toArray() {
return Arrays.copyOf(a, a.length, Object.class);
}
In both cases a String
is passed to asList
, but in the JDK 8 case it is cloned, which retains its array type (String
), and in JDK 9+ it is copied using Arrays.copyOf
with the explicit new array type of Object
.
This difference means that in JDK 8 Arrays.asList("a", "b").toArray().getClass()
returns String
and in JDK 9+ it returns Object
, so in JDK 9+ your expression will evaluate to false
.
The reason for this change comes from JDK-6260652 with the motivation:
The Collection documentation claims that
collection.toArray()
is "identical in function" to
collection.toArray(new Object[0]);
However, the implementation of
Arrays.asList
does not follow this: If created with an array of a subtype (e.g.String
), itstoArray()
will return an array of the same type (because it useclone()
) instead of anObject
.
If one later tries to store non-Strings (or whatever) in that array, an
ArrayStoreException
is thrown.
So this change was made to fix the previous behaviour.
If this is a problem for you, the related release note offers this as a work-around:
If this problem occurs, rewrite the code to use the one-arg form
toArray(T)
, and provide an instance of the desired array type. This will also eliminate the need for a cast.
String array = list.toArray(new String[0]);
It’s so funny that theArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…
– Holger
17 mins ago
add a comment |
up vote
40
down vote
up vote
40
down vote
The List
type returned by asList
is Arrays$ArrayList
. The toArray
method in JDK 8 on that class is:
@Override
public Object toArray() {
return a.clone();
}
But in JDK 9+ it is:
@Override
public Object toArray() {
return Arrays.copyOf(a, a.length, Object.class);
}
In both cases a String
is passed to asList
, but in the JDK 8 case it is cloned, which retains its array type (String
), and in JDK 9+ it is copied using Arrays.copyOf
with the explicit new array type of Object
.
This difference means that in JDK 8 Arrays.asList("a", "b").toArray().getClass()
returns String
and in JDK 9+ it returns Object
, so in JDK 9+ your expression will evaluate to false
.
The reason for this change comes from JDK-6260652 with the motivation:
The Collection documentation claims that
collection.toArray()
is "identical in function" to
collection.toArray(new Object[0]);
However, the implementation of
Arrays.asList
does not follow this: If created with an array of a subtype (e.g.String
), itstoArray()
will return an array of the same type (because it useclone()
) instead of anObject
.
If one later tries to store non-Strings (or whatever) in that array, an
ArrayStoreException
is thrown.
So this change was made to fix the previous behaviour.
If this is a problem for you, the related release note offers this as a work-around:
If this problem occurs, rewrite the code to use the one-arg form
toArray(T)
, and provide an instance of the desired array type. This will also eliminate the need for a cast.
String array = list.toArray(new String[0]);
The List
type returned by asList
is Arrays$ArrayList
. The toArray
method in JDK 8 on that class is:
@Override
public Object toArray() {
return a.clone();
}
But in JDK 9+ it is:
@Override
public Object toArray() {
return Arrays.copyOf(a, a.length, Object.class);
}
In both cases a String
is passed to asList
, but in the JDK 8 case it is cloned, which retains its array type (String
), and in JDK 9+ it is copied using Arrays.copyOf
with the explicit new array type of Object
.
This difference means that in JDK 8 Arrays.asList("a", "b").toArray().getClass()
returns String
and in JDK 9+ it returns Object
, so in JDK 9+ your expression will evaluate to false
.
The reason for this change comes from JDK-6260652 with the motivation:
The Collection documentation claims that
collection.toArray()
is "identical in function" to
collection.toArray(new Object[0]);
However, the implementation of
Arrays.asList
does not follow this: If created with an array of a subtype (e.g.String
), itstoArray()
will return an array of the same type (because it useclone()
) instead of anObject
.
If one later tries to store non-Strings (or whatever) in that array, an
ArrayStoreException
is thrown.
So this change was made to fix the previous behaviour.
If this is a problem for you, the related release note offers this as a work-around:
If this problem occurs, rewrite the code to use the one-arg form
toArray(T)
, and provide an instance of the desired array type. This will also eliminate the need for a cast.
String array = list.toArray(new String[0]);
edited 7 mins ago
Michael
18.2k73268
18.2k73268
answered 12 hours ago
Jorn Vernee
19.6k33256
19.6k33256
It’s so funny that theArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…
– Holger
17 mins ago
add a comment |
It’s so funny that theArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…
– Holger
17 mins ago
It’s so funny that the
ArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…– Holger
17 mins ago
It’s so funny that the
ArrayList(Collection)
constructor contains a protection against this misbehavior with reference to this bug report (it’s from 2005) for a long time now, whereas no-one came to the idea of fixing the misbehavior in the first place, apparently…– Holger
17 mins ago
add a comment |
up vote
9
down vote
I would say that this was a bug in JDK 8 and before that has been fixed.
List<T>.toArray()
was always declared as returning Object
(see JavaDoc) - that it did in effect return String
in a special case was a mistake.
1
That's incorrect. The method has always returnedObject
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.
– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
add a comment |
up vote
9
down vote
I would say that this was a bug in JDK 8 and before that has been fixed.
List<T>.toArray()
was always declared as returning Object
(see JavaDoc) - that it did in effect return String
in a special case was a mistake.
1
That's incorrect. The method has always returnedObject
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.
– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
add a comment |
up vote
9
down vote
up vote
9
down vote
I would say that this was a bug in JDK 8 and before that has been fixed.
List<T>.toArray()
was always declared as returning Object
(see JavaDoc) - that it did in effect return String
in a special case was a mistake.
I would say that this was a bug in JDK 8 and before that has been fixed.
List<T>.toArray()
was always declared as returning Object
(see JavaDoc) - that it did in effect return String
in a special case was a mistake.
answered 12 hours ago
Thomas Kläger
5,8992717
5,8992717
1
That's incorrect. The method has always returnedObject
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.
– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
add a comment |
1
That's incorrect. The method has always returnedObject
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.
– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
1
1
That's incorrect. The method has always returned
Object
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.– Konrad Rudolph
46 mins ago
That's incorrect. The method has always returned
Object
(just look at its return type declaration). It never violated its declared type. Instead it violated its documented runtime behaviour. The difference is a nuance, but an important one.– Konrad Rudolph
46 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
The JDK 9 release note on this change is here: oracle.com/technetwork/java/javase/…
– Alan Bateman
33 mins ago
add a comment |
up vote
0
down vote
There are couple of reasons here
1) It is a bug in JDK 8 where should return java.lang.Object
but it is not and it is fixed in JDK 9+
@Override
public Object toArray() {
return a.clone();
}
2) ==
operator is always used for reference comparison, so java.lang.String
or java.lang.Object
are objects which are singleton and immutable loaded at the time of JVM starts
System.out.println(String.class.hashCode()); //2018699554
System.out.println(Arrays.asList("a", "b").toArray().getClass().hashCode()); //2018699554
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrongjava.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann
– Deadpool
9 hours ago
2
Nothing in the question is comparing any strings; it's comparing twojava.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.
– Boann
9 hours ago
sorry you are right, my intention is to explain==
opeartor will always comapre references andjava.lang.Object
orjava.lang.String
objects are singleton and immutable and load at JVM starts @Boann
– Deadpool
9 hours ago
That==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the twoClass
objects were being compared via.equals
. Or simply runningSystem.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.
– Boann
9 hours ago
add a comment |
up vote
0
down vote
There are couple of reasons here
1) It is a bug in JDK 8 where should return java.lang.Object
but it is not and it is fixed in JDK 9+
@Override
public Object toArray() {
return a.clone();
}
2) ==
operator is always used for reference comparison, so java.lang.String
or java.lang.Object
are objects which are singleton and immutable loaded at the time of JVM starts
System.out.println(String.class.hashCode()); //2018699554
System.out.println(Arrays.asList("a", "b").toArray().getClass().hashCode()); //2018699554
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrongjava.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann
– Deadpool
9 hours ago
2
Nothing in the question is comparing any strings; it's comparing twojava.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.
– Boann
9 hours ago
sorry you are right, my intention is to explain==
opeartor will always comapre references andjava.lang.Object
orjava.lang.String
objects are singleton and immutable and load at JVM starts @Boann
– Deadpool
9 hours ago
That==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the twoClass
objects were being compared via.equals
. Or simply runningSystem.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.
– Boann
9 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
There are couple of reasons here
1) It is a bug in JDK 8 where should return java.lang.Object
but it is not and it is fixed in JDK 9+
@Override
public Object toArray() {
return a.clone();
}
2) ==
operator is always used for reference comparison, so java.lang.String
or java.lang.Object
are objects which are singleton and immutable loaded at the time of JVM starts
System.out.println(String.class.hashCode()); //2018699554
System.out.println(Arrays.asList("a", "b").toArray().getClass().hashCode()); //2018699554
There are couple of reasons here
1) It is a bug in JDK 8 where should return java.lang.Object
but it is not and it is fixed in JDK 9+
@Override
public Object toArray() {
return a.clone();
}
2) ==
operator is always used for reference comparison, so java.lang.String
or java.lang.Object
are objects which are singleton and immutable loaded at the time of JVM starts
System.out.println(String.class.hashCode()); //2018699554
System.out.println(Arrays.asList("a", "b").toArray().getClass().hashCode()); //2018699554
edited 6 mins ago
Michael
18.2k73268
18.2k73268
answered 10 hours ago
Deadpool
3,2582324
3,2582324
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrongjava.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann
– Deadpool
9 hours ago
2
Nothing in the question is comparing any strings; it's comparing twojava.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.
– Boann
9 hours ago
sorry you are right, my intention is to explain==
opeartor will always comapre references andjava.lang.Object
orjava.lang.String
objects are singleton and immutable and load at JVM starts @Boann
– Deadpool
9 hours ago
That==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the twoClass
objects were being compared via.equals
. Or simply runningSystem.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.
– Boann
9 hours ago
add a comment |
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrongjava.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann
– Deadpool
9 hours ago
2
Nothing in the question is comparing any strings; it's comparing twojava.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.
– Boann
9 hours ago
sorry you are right, my intention is to explain==
opeartor will always comapre references andjava.lang.Object
orjava.lang.String
objects are singleton and immutable and load at JVM starts @Boann
– Deadpool
9 hours ago
That==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the twoClass
objects were being compared via.equals
. Or simply runningSystem.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.
– Boann
9 hours ago
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
String pools have nothing to do with what's going on here.
– Boann
9 hours ago
is that wrong
java.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann– Deadpool
9 hours ago
is that wrong
java.lang.String
is string object that created while JVM loading in object heap memory or String pool, is that wrong? String constants are stored in pool, objects are stored in heap @Boann– Deadpool
9 hours ago
2
2
Nothing in the question is comparing any strings; it's comparing two
java.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.– Boann
9 hours ago
Nothing in the question is comparing any strings; it's comparing two
java.lang.Class
objects. Its behavior would be the same even if there were no such thing as the string constant pool.– Boann
9 hours ago
sorry you are right, my intention is to explain
==
opeartor will always comapre references and java.lang.Object
or java.lang.String
objects are singleton and immutable and load at JVM starts @Boann– Deadpool
9 hours ago
sorry you are right, my intention is to explain
==
opeartor will always comapre references and java.lang.Object
or java.lang.String
objects are singleton and immutable and load at JVM starts @Boann– Deadpool
9 hours ago
That
==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the two Class
objects were being compared via .equals
. Or simply running System.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.– Boann
9 hours ago
That
==
compares object references is true, but misses the point of the question. The behavior of the code in the question would be exactly the same if the two Class
objects were being compared via .equals
. Or simply running System.out.println(Arrays.asList(1, 2, 3).toArray());
also illustrates the behavior change between JDK 8 and JDK 9+.– Boann
9 hours ago
add a comment |