• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.intern()-regd

 
ganesh subbiah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
ganesh subbiah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much Barry
 
Archana Babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you do compile it?
 
Archana Babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's the error
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Siva, I also agree with you.
Could you please clarify Barry??? Please answer why the program should run with default access???
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic