File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String.intern()-regd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String.intern()-regd" Watch "String.intern()-regd" New topic
Author

String.intern()-regd

ganesh subbiah
Ranch Hand

Joined: Nov 18, 2004
Posts: 39
I need to know about the String.intern() method and its use.is it there
for SCJP1.4. I would be grateful if you could explain this through the
example given in java language specification

the test program consisting of the compilation unit (�7.3):

package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }

and the compilation unit:

package other;
public class Other { static String hello = "Hello"; }

produces the output:

true true true true false true

ganesh1.s@tcs.com
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No it's not in SCJP. But it's nice to know what it does.

Take three string literals: "Hello", " ", and "World". Concatenate them:

String s = "Hello" + " " + "World";

The variable s references a new String object which resides in the heap. If we call intern() on the string reference s and assign it to s:

s = s.intern();

The object s references gets put into the literal pool area as "Hello World".

This code may be a little easier to understand:

[ December 13, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
ganesh subbiah
Ranch Hand

Joined: Nov 18, 2004
Posts: 39
Thank You very much Barry
Archana Babu
Greenhorn

Joined: Nov 22, 2004
Posts: 6
I have a doubt in the code, eventhough its offline from the point of discussion


We are trying to access "other.Other.hello" (default access level) from a class in different package. Hence, won't the third print statement result in a compile time error?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What happens when you do compile it?
Archana Babu
Greenhorn

Joined: Nov 22, 2004
Posts: 6

That's the error
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, you are correct - it's best to have hard evidence. I'll check out the new JLS 3.0 draft to see if the problem is still there. If so you can make yourself famous by reporting the error to Sun
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yup it's in JLS 2 and the new draft JLS 3. Go tell Sun! Please do not ask me how, unless you want me to take credit for finding it
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Barry said:
you can make yourself famous by reporting the error to Sun


I don't get it. What error?

Class other.Other is public but its member hello has default access and can't be accessed outside its package, which is what the error message said.


Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It is not supposed to give any compilation error, Mike, it's supposed to run and produce "true true true true false true" as output. So the variable hello in other.Other must be made public.

[ December 16, 2004: Message edited by: Barry Gaunt ]
[ December 16, 2004: Message edited by: Barry Gaunt ]
ganesh subbiah
Ranch Hand

Joined: Nov 18, 2004
Posts: 39
Hi archana babu
I have not compiled the code as I dont have java installed in my PC
at my workplace .
but according to JLs thats the output
the hello variable in outer should be made public
barry can u help guys like me who cant have java installed in their workplace by
providing it some how in ur site
siva krishnan
Greenhorn

Joined: Apr 26, 2003
Posts: 21


Barry, I still dont get it.
It is not supposed to give any compilation error, Mike, it's supposed to run and produce "true true true true false true" as output. So the variable hello in other.Other must be made public.

It is throwing an error as the hello variable is in another package (other.Other) with default access which can not be seen by any class outside the package.
Why should it run ?


Cheers<br />Siva<br />SCJP 1.4<br />SCWCD 1.3
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sun has missed out the public modifier by mistake and they, Sun, say it compiles and runs and produces the given result.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Yes Siva, I also agree with you.
Could you please clarify Barry??? Please answer why the program should run with default access???


Regards,<br /> <br />MannY<br />----------------------------------------------------------- <br />"Beam me up Scotty, there's no intelligent life down here" !!! <br />------------------------------------------------------------
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57

Sun has missed out the public modifier by mistake and they, Sun, say it compiles and runs and produces the given result.


But the program does not run... it gives you compiler error as expected...

How Sun has missed out on public modifier???
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Oh.. now I got it...

Listen up Mike, Siva and Ganesh. Archana and Barry were talking about the error in the Java Language Specification 2nd and 3rd edition book.
It is printed in both the books that the given source code compiles and gives output :

true true true true false true

Which is wrong... this was pointed out by Archana....

So Siva and Mike both of you are right in your understanding that default variable can't be accessed outside package

It was just a miscommunication between us.... phew..
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think that some posts on this thread implied (at least to me) a Java software bug.

This is actually a mistake in the book.

In the good old days, when IT was spelled IBM, every computer manual had a postage-paid return form in the back for reporting errors in the doc. Every few weeks, IBM would issue a "Technical Newsletter" with replacement pages to correct the errors. Soon, the manual had no significant errors. Of course, in those days software vendors were expected to fix serious bugs in a week or two for free, not defer the fixes until the next (purchasable) release.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57


I think that some posts on this thread implied (at least to me) a Java software bug.

This is actually a mistake in the book.



You are right Mike, a mistake in the book....

I also thought that they (Archana and Barry) were talking about the software bug in the java (My mistake).

Anyways thanks for sharing the IBM info
Archana Babu
Greenhorn

Joined: Nov 22, 2004
Posts: 6
Hi Barry,
I didn't quite realise that the code snippet was from JLS until you mentioned it in your reply.... I have reported the error. Hope it is taken taken care of in the latest edition of JLS.

Archana.
-------------------------------------------------
The first step in fixing a broken program is getting it to fail repeatedly.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Second sentence of first post:
"I would be grateful if you could explain this through the
example given in java language specification"

Hey, don't you guys read all of the print on the sides of Corn Flakes packets?
ganesh subbiah
Ranch Hand

Joined: Nov 18, 2004
Posts: 39
Hi
barry How could sun make such a documentation mistake. Anway archana
babu gud work. But archana and barry ,this is jus for fun
i too should be credited for bringing in the doubt from JLS
wht u all say???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.intern()-regd