About Question enthuware.ocajp.i.v7.2.999 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick

About Question enthuware.ocajp.i.v7.2.999 :

Post by The_Nick »

I would like to add a clarification.

basically intern of course is useful only for String object as the String literals are automatically interned.
A part from that, the use of intern to be meaningful must be done at the same time the object it's created.
example:

Code: Select all

String str1 = "Indeed";
String str = new String("Indeed").intern();
System.out.println(str1==str);
If it had been done something like str.intern(); after the creation of str it of course would be too late as the different object would have been already created.

Let's pass this OCA exam.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.999 :

Post by admin »

That is incorrect. A new String object is created anyway when you do new String("Indeed"). However, when you call intern on it, the JVM finds out the interned String object containing the same value and assigns to str.
The reference to the newly created String object is lost.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 238 guests