Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help needed on intern(), compile time Vs run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help needed on intern(), compile time Vs run time" Watch "Help needed on intern(), compile time Vs run time" New topic
Author

Help needed on intern(), compile time Vs run time

Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
Dear Friends
#1 I am facing a few problems with the intern() method of the String class. When exactly is it invoked? Can you please guide me or direct me to a online resource or a book which explains this.
#2 About compile time and run time activities - again, please guide me to a resource (online, book, etc.)
Thanx
regards
Sanjeev
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
About intern() method: IMHO the best way to find out when it is invoked is to look at the code for String class. It is invoked by NO String method. My conclusion: intern() is only invoked when JVM deals with String literals, like String s = "abc"; - intern() will be invoked. But after String s = new String("abc"); s will not be interned.


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
I agree with Mapraputa Is.
Actually most of the methods of String which return String i.e.
toString()
substring(0)
concat("")
toLowerCase()
trim()
...
and even
replace('x','x')
are cleverly written so that if the String which is about to be returned is same as the one the method was invoked on, then the
String object on which the method was invoked is returned.
I was living with the Idea that this happens because of intern method(). Must thank Mapraputa for pointing it out.

[This message has been edited by mohit joshi (edited September 30, 2000).]
[This message has been edited by mohit joshi (edited September 30, 2000).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Sanjeev,
The following is from JDK 1.3 for String.intern()

All literal strings and string-valued constant expressions are interned. String literals are defined in �3.10.5 of
the Java Language Specification

The only info I've found on runtime vs compile-time activities is in the JLS §11 on Exceptions.
Hope that helps
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ying Xing
Greenhorn

Joined: Sep 23, 2000
Posts: 24
Hi Sanjeev Verma,
You can get more infomation about String.intern() from http://www.javaranch.com/ubb/Forum24/HTML/004054.html

Regards,
Ying Xing
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
That�s an interesting question. My first reply is not complete.
When is a String interned? From Jane� quote: �All literal strings and string-valued constant expressions are interned�. What are �string-valued constant expressions�, JLS mentioned? I tried a small program:

The last case is the most interesting. As mohit joshi said , expression "c".trim() doesn�t create a new object, it returns reference to �c� literal.

Nevertheless, the whole expression with �c�.trim() is not interned.
I think we do not need to know all that for the exam, just curious.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Still thinking about this question�


[This message has been edited by Mapraputa Is (edited October 01, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed on intern(), compile time Vs run time
 
Similar Threads
JBoss Online Tutorial / Book
JBoss Online Tutorial / Book
How to get started with JSTL
JBoss Online Tutorial / Book
Help on Struts 2