• Post Reply Bookmark Topic Watch Topic
  • New Topic

String manipulation  RSS feed

 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi java masters,
I have a doubt. I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?


Regards,
Jude
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming each value returned by the call to rs.getString(inc+1) is unique then each call will create a new String object.
Assigning rslt to rowArr[inc] will not create a new String object.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,
I appreciate your answer. One more thing to clarify. That string in each loop run, if i got different results will it still make different references by the name 'rslt' ?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean but Strings are immutable (ie they can't be changed) so if you have a unique arrangement of characters then you will always get a new String object. If the particular arrangement of characters is not unique then depending on how the String is created and what values are currently in the String pool you may get a reference to an existing String object or you may get a new String object.

You will never get the same reference to a String object which contains different characters - if you did String wouldn't be immutable.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Realy, it is very expensive create new string for every new value and put in into array. Write rowArr[inc] = rs.getString(inc+1); it may save some memory in creating new Object.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahaha.. nice one bro.... simply the solution
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko wrote:Realy, it is very expensive create new string for every new value and put in into array. Write rowArr[inc] = rs.getString(inc+1); it may save some memory in creating new Object.


Well, it's not creating a new string, it's simply a local reference, so no new objects.

Not sure if there would be any meaningful overhead to the local reference.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your are right. But looking for what purpose develop an application!
If, it something for mobile, and 'numberOfColumns' а big value, i think it can help).
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko wrote:Your are right. But looking for what purpose develop an application!
If, it something for mobile, and 'numberOfColumns' а big value, i think it can help).


You'd need to actually test that assumption.
Us developers are notoriously bad at guessing performance costs.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh... may be you're right...
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He is definitely right
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko wrote:Realy, it is very expensive create new string for every new value and put in into array. Write rowArr[inc] = rs.getString(inc+1); it may save some memory in creating new Object.

I'm sorry but that statement is so full of holes I have to comment on it.

1. You state as fact "it is very expensive create new string for every new value and put in into array" - please explain your rational for that statement.
2. You have to create a new String object for every unique value - Stings are immutable so you can't reuse them.
3. Assigning an object to a local variable and then assigning that local variable to an array element doesn't create any new objects.

Therefore, the only memory you will save is that used by the local variable to hold a reference to the object. And even that tiny overhead is not guaranteed as the compiler/runtime will possibly optimize it out of existence. The only other point to consider is whether or not the proposed change makes the code more or less readable - in this case the proposed change does not make the code inscrutable so either approach is acceptable although personally I prefer the original approach as it's marginally easier to read and to add debugging.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I know, creating new Object always was expensive operation for java(for memory).
For not big value origin value really look nice. But for 1 mln. records creating 1 mln. of new String objects will be more expensive than save 1 mln. of links? Is not it?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko wrote:As I know, creating new Object always was expensive operation for java(for memory).

I never seen any evidence to show that it is particularly expensive - can you point me to the source of your information?
I'd be surprised if creating a String object in Java is significantly more expensive than creating an equivalent object in any other OO language.

Vitaliy Gaydarenko wrote:
For not big value origin value really look nice. But for 1 mln. records creating 1 mln. of new String objects will be more expensive than save 1 mln. of links? Is not it?

But you need to have created the 1 min. String objects to be able to have 1 min. links to them so I don't understand the point you are making.

You can't get strings from a ResultSet without creating String objects. The call to getString() creates a String object and passes back a reference to that object. The original code doesn't create anymore String Objects it just copies the reference to a local variable and then to the array elelment.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, ResultSet return new String and it is true. But for what to create a template for this string and put it into array? if you can put this String into array right away. And it will be just a little? but quickly(I teased it).
1) String[] st = new String[100000];
for (int i = 0; i < 100000; i++) {
String str = new String();
st[i] = str;
}

2) String[] st = new String[100000];

for (int i = 0; i < 100000; i++) {
st[i] = "2";
}
Second was quickly at 15%
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not an equivalent test. The first code creates 100000 new String objects the second code assigns the same String object 100000 times.

I don't understand how this relates to the code change you proposed.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I don't understand how this relates to the code change you proposed.

Sorry, first example i made with another code, something like this:
String str = "2";
st[i] = str;

Thank that noticed it.)
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code:



On my system produces the following results:


Which shows there is no real difference between the two approaches
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, different is not really big, a made test with nanoSecond and only in nanoSecond i saw that different in 15% about what i told early.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko wrote:Yes, different is not really big, a made test with nanoSecond and only in nanoSecond i saw that different in 15% about what i told early.

I'm not sure which version of Java you are using and on what OS but on my system there is no difference in time taken.
If you up the number of iterations to reduce the impact of the OS, garbage collections etc (eg having each method take about half a second seems to do it on my system) and then average the time taken for 50 iterations of the test cycle the average times are always within + or - 1% of each other.

 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vitaliy Gaydarenko made some points which helped me to understand some key points about string creation.
Thanks a lot buddies.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key thing that beginners need to learn, not only about Strings but about objects in general: Make sure to distinguish between objects and the variables which contain references to them. You can declare a variable without creating an object for it to refer to, and likewise you can create an object and not have a variable refer to it. Hopefully you have that in your mind now.

That distinction applies to all objects; the only thing different about Strings is that String literals exist in the language. Those, as I hope you know by now, represent String objects which are stored in a special place.
 
Vitaliy Gaydarenko
Greenhorn
Posts: 22
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude Niroshan wrote:Vitaliy Gaydarenko made some points which helped me to understand some key points about string creation.
Thanks a lot buddies.

Jude Niroshan, You're welcome, I am glad, that I could help you)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!