This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any one advice me difference between this two

 
vincent vincent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and



please any one tell which one is correct, why ?

Thanks,
Louis

[edit]Add code tags. CR[/edit]
[ December 17, 2008: Message edited by: Campbell Ritchie ]
 
Roland Maurer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you like to do ?

where is the "itr" comming from ?
what do you like to add to ArrayList ?
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. If we don't know what you are doing, how do we know if it is correct? But here are a couple of points...

In both cases, the valueOf() method doesn't do anything, as the result is thrown away. In both cases, empty strings are placed into the collection. Is this what you want?

Henry
 
vincent vincent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)created String object outside loop

and

2)Created String Object inside loop


Q) Added two type of code which one is correct and explain why
[edit]Add code tags. CR[/edit]
[ December 17, 2008: Message edited by: Campbell Ritchie ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, instead of...

...do you mean the following?

As a question of scope, you might ask yourself whether "key" needs to be used outside of the loop. And if not, does it make much sense to create a new empty String instance for each iteration, only to immediately reassign "key" to reference a different String?

So if "key" is not needed outside the scope of this loop, then you might consider declaring it inside the loop, and assigning it a value in the same line...

Or better yet, eliminate the "key" variable entirely...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and welcome to JavaRanch, by the way!
 
vincent vincent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey all,

what my doubt is,

1)will create object inside loop ?
2)will create object outside loop?


which one is correct, if you didn't get my point pl let me know, if get means pl explain which one is correct.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. But the objects you['re reating in both examples aren't actually being used for anything useful. Saying

String key;

just defines a name for a variable; it creates nothing, and allocates no memory. But saying

String key = new String();

creates an actual object. In your examples, there's no reason to create the objects in the first place. Showing a realistic example would be a better way to ask your question -- or you could simply ask your actual question, whatever it is.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. I know there are people who have the same first and last names, but please confirm that is your real name, as required by our naming policy.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Welcome to JavaRanch. I know there are people who have the same first and last names, but please confirm that is your real name, as required by our naming policy.


And you also signed your original post with "Louis".

Henry
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vincent,

Here, only one string object is created.

Here, for every iteration one String object is created.
Now, one question for you vincent.

What's going on here?
 
vincent vincent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Pandya:
vincent,

Here, only one string object is created.

Here, for every iteration one String object is created.
Now, one question for you vincent.

What's going on here?


Vishal,

yeah, here also every iteration create one new object.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct!
So I guess this is what you wanted to know?
 
vincent vincent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal,

yes,
what I understand is
If we will create object outside of loop means finally we will get last value only.
If we will create object inside of loop means we will get all value.

am I correct or what?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In above code snippet, we have created one reference named 'referenceToObject' and it points to one string object whose value is 'vincent'. Then in the next line we have created one more string object whose value is vishal. So now our reference is no longer pointing to the last object('vincent') but points to newly created string object(vishal).
If we will create object outside of loop means finally we will get last value only.
If we will create object inside of loop means we will get all value.

So In any case your reference will be referencing to last created object(value) only.
[ December 17, 2008: Message edited by: Vishal Pandya ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic