• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regd. Strings...

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....
 
Sidharth Panwar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice Arul , now you have clearly explained to him with an example ....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic