Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sanjeev Verma
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ying Xing
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still thinking about this question�


[This message has been edited by Mapraputa Is (edited October 01, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic