wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes valueOf() and new Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "valueOf() and new" Watch "valueOf() and new" New topic
Author

valueOf() and new

Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
In the official solution to one of the basic Cattle Drive assignments I was surprised to see, at such an early stage,
an object created:


The solution I'd submitted contained, at the same point in the code, the statement:



I suppose this must also create an object but where is the new?
(Is it in the code of the valueOf() method?)

A more concrete example is given by:


[ December 22, 2007: Message edited by: Dick Summerfield ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

Why is learning to instantiate an object via the new operator a bad idea? Or are you saying that using the new operator versus valueOf() for the wrapper types a bad idea in general?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Agree with Henry Wong. The sooner you get used to creating objects with "new" the better.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

BTW, to answer your question...



For 123, the valueOf() method will get an integer that has already been created earlier from the integer cache.

You also need to have Java 5 or greater, as the valueOf() method, with the int as the signature, didn't exist prior to Java 5.

Henry
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by Campbell Ritchie:
Agree with Henry Wong. The sooner you get used to creating objects with "new" the better.


I can't wait! (it's what I'm here for ) ...but I only started the "drive" in October and as the second block of assignments is called OOP-1,2,3 etc. I assumed the first block was geared more toward giving the beginner a feel for the language structure and the use of methods etc., before introducing objects.
The only programming I did before (many years ago) was procedural so I really appreciate the gentle introduction given by the cattle drive.
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by Henry Wong:
Why is learning to instantiate an object via the new operator a bad idea? Or are you saying that using the new operator versus valueOf() for the wrapper types a bad idea in general?

Henry


I don't think either of these is a bad idea. I'm just saying I was (albeit mildly) surprised because I wasn't really expecting objects until the second block of assignments - I got that wrong .

The other thing is that I thought I had solved the problem without instantiating anything because of invoking valueOf() and then I wasn't so sure - all this after receiving the instructor's solution - which led to my question "Is new within the valueOf() method?"
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Dick]: I'm just saying I was (albeit mildly) surprised because I wasn't really expecting objects until the second block of assignments - I got that wrong.

Java will implicitly create objects for you in certain situations, whether you're intending to or not. I'm sure you'll learn a lot more about creating (and designing) objects intentionally, later on, but in the mean time, expect that there are probly some objects lurking in your programs even though you haven't intentionally put them there.

[Dick]: The other thing is that I thought I had solved the problem without instantiating anything because of invoking valueOf() and then I wasn't so sure - all this after receiving the instructor's solution - which led to my question "Is new within the valueOf() method?"

Yes and no. There's a new in valueOf(), which executes in some situations. Not, as it happens, in this particular situation. But there is additional code inside the Integer class, not in the valueOf() method, which leads to the creation of a preexisting cache of Integer objects available instantly on demand. These objects are also created with new. That's what happened here - when the Integer class was first used, it was loaded and initialized, and 256 Integer objects were created using "new", including one for 123. Generally you can assume that most methods that return objects somehow, do so by means of the "new" keyword, somewhere, somehow. There are some exceptions to this, notably String literals and objects created using reflection, but that's just gettuing us further from the main topic of this post.

And although you didn't exactly ask this: I would note that in fact, it's probably better to use valueOf() than to use new here. A method like valueOf() is more flexible - it can be (and is) implemented so that it doesn't bother creating a brand new object if there's already an existing object that will serve just as well. (Well, sometimes it creates one anyway, because it's too much trouble to keep track of all existing Integer objects, but the point is that there's at least an attempt to re-use existing objects when possible. But if you just use "new", there's no other option - the JVM must create a brand new object, because you instructed it to. Which may be less efficient than using valueOf(). To be fair, most of the time this really doesn't matter either way. But if a method like valueOf() is available, you might as well use it. Most of the time it won't matter; occasionally it will speed things up considerably.


"I'm not back." - Bill Harding, Twister
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3354

As usual, that's a perfect way you have explained Jim


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Thanks to all who answered...
especially for the intereseting bit about the "string pool" mentioned by Henry and Jim. Presumeably this is the one you can use: on which I read something about in Peter van der Linden's book
Originally posted by Jim Yingst:

[Dick]: "Is new within the valueOf() method?"

Yes and no. There's a new in valueOf(), which executes in some situations. Not, as it happens, in this particular situation. But there is additional code inside the Integer class, not in the valueOf() method, which leads to the creation of a preexisting cache of Integer objects available instantly on demand. These objects are also created with new. That's what happened here - when the Integer class was first used, it was loaded and initialized, and 256 Integer objects were created using "new", including one for 123.



This will do just fine for now Jim. Thank you very much for the clear explanation!
[ December 23, 2007: Message edited by: Dick Summerfield ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

Thanks to all who answered...
especially for the intereseting bit about the "string pool" mentioned by Henry and Jim. Presumeably this is the one you can use:


I don't believe that I mentioned anything about the string pool. The integer cache is something completely different -- and currently supports only a fixed range.

Henry
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by Henry Wong:


I don't believe that I mentioned anything about the string pool. The integer cache is something completely different -- and currently supports only a fixed range.

Henry


You're right of course, you didn't and neither did Jim! I was getting mixed up as beginners will ,
Sorry... I'll go away and practice a bit more
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Never mind about getting mixed up; we all do it. We are all here to learn and those of us who are more experienced have learnt something form this thread.
 
 
subject: valueOf() and new