Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 04:33 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
String vs new String
Code:
String myString = “hello world”; String myString1 = new String(“Hello World”);
Whats the difference between the statements above?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-26-2007, 03:42 AM
Member
 
Join Date: Nov 2007
Posts: 8
m_srikanth is on a distinguished road
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:


String str = "abc";
is equivalent to:


char data[] = {'a', 'b', 'c'};
String str = new String(data);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2007, 09:19 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yep, it is so useful when you want to change the string dynamically.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2007, 11:39 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks for the explanation. So it means that when we create a String using the following syntax, we create String buffer that can be changed unlike String constants.

Code:
String str = new String("String Msg");
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2007, 11:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Correct. Size of the buffer same as the number of characters int the string.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-26-2007, 12:35 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Ok. If the buffer has the same size, as the number of characters in the string, how about the followinf example:

Code:
String str = new String("Austarlia"); str = "Australia Football Team";
str was declared with buffer size 9. Then I put a String of size 23 characters. Of course its not a syntax error but does this mean that the buffer is flexible and grow according to the String??
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-26-2007, 12:43 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Try it and see.

Simply print some characters or find the size of the buffer. Then you can see what's going on there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-26-2007, 01:07 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
I tried the following:
Code:
String myString = new String("Hello"); System.out.println("Buffer size: " + myString.length()); myString = "Hello-Hello-Hello"; System.out.println("Buffer size: " + myString.length()); myString = "hi"; System.out.println("Buffer size: " + myString.length());
Output:
Code:
Buffer size: 5 Buffer size: 17 Buffer size: 2
So buffer grows or shrink according to the String. Makes sense
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-26-2007, 01:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yep, now you know what is happened there. That is the important of String objects.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-26-2007, 01:20 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks all of you. Now I know the difference.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-26-2007, 01:25 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok. It's cool, isn't it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-26-2007, 01:27 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Well, I discovered something interesting. Review the code below. I think, every time we change the value in String, new object is being created.

Code:
String myString = new String("Hello"); System.out.println("Buffer size: " + myString.length() + " Hash code " + myString.hashCode()); myString = "Hello-Hello-Hello"; System.out.println("Buffer size: " + myString.length() + " Hash code " + myString.hashCode()); myString = "hi"; System.out.println("Buffer size: " + myString.length() + " Hash code " + myString.hashCode());
Output:
Code:
Buffer size: 5 Hash code 69609650 Buffer size: 17 Hash code -2022942916 Buffer size: 2 Hash code 3329

Last edited by javaplus : 11-26-2007 at 01:34 PM.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-26-2007, 01:34 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I don't think so. It use the same object with different hash code, that mean different memory locations. I'm not 100% sure, I want to check it now. May be some explanation can be available in documentation.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-26-2007, 01:39 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Yes, may be it stores at different memory location as the size changes.
Please share if you have some interesting information.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-26-2007, 01:44 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Sure. As far as I know the effect is that dynamically changing the buffer, first what have happened is destroyed/delete the first buffer, then create a new buffer with new size. So the memory location is differ. Basically memory location is selected randomly.

If you can just test your code on another machine. I think it give the different hash code for you.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-26-2007, 01:51 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Makes sense. But if the buffer is deleted and then new is created, isnt it like

Code:
String a = "a"; a= "aa"; ...
I mean then what is the real difference?
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 11-26-2007, 01:57 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Real difference comes with real application programming. Working with objects gives the best way to work. That is why OOP is handy.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 11-26-2007, 02:10 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Yep - OOP sure is handy.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 11-26-2007, 02:14 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I have no a best explanation to you on this. Actually you can do above all the things we have tested by using String constant. Depends on the situation of the coding it should be decide, either working with constants or with objects.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 11-26-2007, 02:16 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks guys for the fruitful discussion.
I learned new things.

Keep educating.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread