aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regd. Strings... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regd. Strings..." Watch "Regd. Strings..." New topic
Author

Regd. Strings...

Sidharth Panwar
Greenhorn

Joined: Apr 30, 2006
Posts: 8
In K&B's book on page 420 the text says:

String s = "abc"; //Creates one string object and one ref. var.
In this simple case, "abc" will go to the pool and s will refer to it.

String s = new String("abc"); //Creates two objects, and one ref var.
In this case, because we used the new keyword, Java will create a new String object in normal(non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed.

Why two objects are created in the second case? Still don't understand why the two cases are working differently. Plus, the second case looks like more inefficient.

In second case if i say:
String s1 = s;
Would s1 refer to the intermediate object pointed to by s or directly to "abc".

Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
hi

understood the following statements and you will get clear on your doubts

1) Any initialisation object will be done by using new operator (i.e)

String s = new String("Testing");

2) Java provides a sring literal("") to create a String object so the above statment can be say as

String s = "Testing".


so the difference between the above two statement was , observe closely the second point and see in the first point now , we are using a String literal and also a new keyword

new String("Testing");

new keyword will create one Stirng object and also for the Stirng literal "Testing"


and


In second case if i say:
String s1 = s;
Would s1 refer to the intermediate object pointed to by s or directly to "abc".



yes you are right now you have two refernce s,s1 for a single String object "abc".

Have a look at immutable concept in String .... hope i'm clear to you ....


With Regards<br />Arul
Sidharth Panwar
Greenhorn

Joined: Apr 30, 2006
Posts: 8
Hi Arul,
Getting the gist of what you are saying. Now see this:

String s = "Hello";
s(Ref. variable, not object) ----> "Hello" (An 'Object' in String Pool)

String s1 = new String("Hello");
s1(Ref. variable) ----> An anonymous String obj. ----> "Hello"

Now if i say,
String s2 = s1;

Will it be:
1) s2(Ref. variable) ----> anonymous String obj. ----> "Hello"
or
2) s2(Ref. variable) ----> "Hello"

If case 1) is true then it's kinda holding your ear from over your head i.e ridiculous.
If case 2) is true then what happens to the anonymous String object?
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
hi

hope this example helps you to get the points clear




output
------------

abc == s2 false
s2 == s1 true
abc == s true
s == s1 false
s == sDuplicate true
Sidharth Panwar
Greenhorn

Joined: Apr 30, 2006
Posts: 8
Damn, why didn't i checked it myself!!! So this new thing does work in a mysterious way. Thanks Arul, now i'm crystal clear on this.
Basil Antony Xavier
Greenhorn

Joined: Aug 07, 2006
Posts: 3
nice Arul , now you have clearly explained to him with an example ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regd. Strings...