• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

valueOf() and new

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Henry Wong. The sooner you get used to creating objects with "new" the better.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?"
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As usual, that's a perfect way you have explained Jim
 
Dick Summerfield
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic