Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Any one advice me difference between this two Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Any one advice me difference between this two" Watch "Any one advice me difference between this two" New topic
Author

Any one advice me difference between this two

vincent vincent
Greenhorn

Joined: Dec 17, 2008
Posts: 6


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

Joined: Nov 23, 2008
Posts: 17
what do you like to do ?

where is the "itr" comming from ?
what do you like to add to ArrayList ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vincent vincent
Greenhorn

Joined: Dec 17, 2008
Posts: 6
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

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Oh, and welcome to JavaRanch, by the way!
vincent vincent
Greenhorn

Joined: Dec 17, 2008
Posts: 6
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Dec 17, 2008
Posts: 6
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

Joined: Aug 16, 2007
Posts: 1374
Correct!
So I guess this is what you wanted to know?
vincent vincent
Greenhorn

Joined: Dec 17, 2008
Posts: 6
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

Joined: Aug 16, 2007
Posts: 1374

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any one advice me difference between this two
 
Similar Threads
Reading Elements from an ArrayList, 3 at a time
can i call a function of servlet using jstl
how to print only strings using generics when the list is having multiple types
Map typeCasting
NULL POINTER eXCEPTION