• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

String

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
What is the difference between
 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry didn't complete my question.

What is the difference between

String s="hell";

and

String s="hello";

cheers,
Poonam K.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is exactly your question? The difference is obvious, isn't it?
 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry again(so foolish of me)

I want to know the difference between

String s="hell";
and

String s=new String("hello");

cheers,
poonam
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!
about the difference i can say is
in the first statement String s="hell";
s is a variable of type string which is given value "hell".

But in the second statement String s=new String("hello");
here s is pointing to the reference of class String and it is instantiated using the keyword "new" (on the right hand side).
now If you use s with a dot operator ie s.somemethod(); you will get the methods of class String example s.concat("som other string");

i hope it helps you.

 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dhwani,
for String s="hell";
will s.concat("o"); work

Because I feel that String s="hell" would also create a string object

cheers,
poonam
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both the cases what you have is the "String". So little surprisingly you can do all the operations on string literal that you can do on String object.

But note that

will keep the string pointed by s unchanged. The operation will return a new string with the value "hello".

 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!
i think the answer given by Pradeep is exactly right
when you use String s="hell";

than the method s.concat("o");
later you print the value of s you wil get "hell" no change will be there...
instead you have to take another String str;
than str=s.concat("o");
this will print "hello".
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poonam,

visit PreciseJava

to know more about String
 
lowercase baba
Posts: 12911
64
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poonam Kadu,

first, you can always go back and edit a post. at the top/center, you should see several icons. the one that looks like a paper/pencil will let you edit your posts, or even delete it entirely.

The difference between your two examples is subtle. Since Strings are so common, the creators of java gave you a shortcut for creating them. BASICALLY, there is not difference between s = "hello" and s = new String("hello");

That does not mean there is NO difference. You may want to try searching around this forum for "string pool". Once you've read about that a little more, come back here and ask any new questions you may have.
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Poonam,

Since you are keen to know more on String, this may help you to satiate the inquisitor in you

String Immutability

 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.The material really helped.

Cheers,
Poonam K.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic