• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object Create

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s ="hai";

In this line how many objects are created?
I think 2 obj, one is 's' and another is 'hai'


String s = String("hai");

and this statement also 2 obj are created
Let me know am i right or not?
 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in both cases there is only one object is created


String s = String("hai");



should be new String("hai")

s is only reference variable

correct me if iam wrong
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This shall create only one Object and a reference to it. So guess its only 1 object!
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s ="hai";
One object is created, the string "hai". Variable s simply refers to it.

String s = String("hai"); is wrong. It should be String s = new String("hai");
Two objects are created. One is the string "hai" as in the first example and the other is a new string that was passed "hai" as an argument.

Every time you have a string in quotes ("xxx"), you are implicitly creating a new string object.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s=new String("hai");
String s1="hai";
String s2="hai";

Just for your info, here only 2 objects and 3 references are created. s1 and s2 are string references which point to the same object "hai"
whereas s references a new "hai"
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

is following code creates only string object

String s=new String("hai");
String s1=s.intern();
String s2=s.intern();


With Regards,
siva Prakash
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to my understanding from the K&B book.

String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.

String s = new String("abc"); // creates two objects,
// and one reference variable

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 in the pool.
 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,

i think u have misunderstood my question , I am taking about intern() method , from java API it was mentioned , that if we use intern method with string methods ( ie toLowerCase & toUpperCase etc) , it will check at string pool for existing string objects , if it presents then it will return the same without creating new one , correct me if iam wrong .


Thanks,
Siva Prakash
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva,

I am sorry i have not noticed your reply I m just trying to address the above problem where they are trying to find out how many object are created in previous reply.

Ya you are right about intern method.

Gaurav
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by siva prakash:
Hi,

is following code creates only string object

String s=new String("hai");
String s1=s.intern();
String s2=s.intern();


With Regards,
siva Prakash



But you are still creating two objects in the first line, the "hai" in the pool, and the s object using the new keyword
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic