|
|
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.
|
|

11-23-2007, 04:33 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
String vs new String
String myString = “hello world”;
String myString1 = new String(“Hello World”);
Whats the difference between the statements above?
|
|

11-26-2007, 03:42 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 8
|
|
|
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);
|
|

11-26-2007, 09:19 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 11:39 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
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.
String str = new String("String Msg");
|
|

11-26-2007, 11:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 12:35 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
Ok. If the buffer has the same size, as the number of characters in the string, how about the followinf example:
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??
|
|

11-26-2007, 12:43 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
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.
|
|

11-26-2007, 01:07 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
I tried the following:
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:
Buffer size: 5
Buffer size: 17
Buffer size: 2
So buffer grows or shrink according to the String. Makes sense 
|
|

11-26-2007, 01:10 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 01:20 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
Thanks all of you. Now I know the difference.
|
|

11-26-2007, 01:25 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 01:27 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
Well, I discovered something interesting. Review the code below. I think, every time we change the value in String, new object is being created.
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:
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.
|
|

11-26-2007, 01:34 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 01:39 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
|
Yes, may be it stores at different memory location as the size changes.
Please share if you have some interesting information.
|
|

11-26-2007, 01:44 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 01:51 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
Makes sense. But if the buffer is deleted and then new is created, isnt it like
String a = "a";
a= "aa";
...
I mean then what is the real difference?
|
|

11-26-2007, 01:57 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 02:10 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 97
|
|
|
Yep - OOP sure is handy.
|
|

11-26-2007, 02:14 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
|
|

11-26-2007, 02:16 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 111
|
|
|
Thanks guys for the fruitful discussion.
I learned new things.
Keep educating.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |