• 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

String.valueOf(X) vs. (String)X

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's faster assuming I'm 100% sure that X is a String?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A cast is always going to be faster than a method call.

Less obvious questions can frequently be answered by looking at the source code.



Bill
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Brogden wrote:A cast is always going to be faster than a method call.



Well, at least, if you're working in a naive environment. In Java, casts have to pass security constraints, and method calls can be optimized . And the net result may every well be either the exact same code, or even more efficient code on the method call (if the compiler decides it can bypass the security checks for a statically-defined type). So YMMV, depending on run environment and JVM.

So ALWAYS benchmark if it matters. And if it isn't a case where some sort of horrendous performance bottleneck has been detected, save your energy for higher-level optimizations where you'll get more performance with less effort.

The main difference, in fact, is that string.valueOf() can throw a NullPointerException, so the two code samples are not functionally equivalent!
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to point out that ALL method calls with an object reference have an implicit cast for security reasons so there is no way around it. Either explicit or implicit, a cast check will be performed.

Bill
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Brogden wrote:I would like to point out that ALL method calls with an object reference have an implicit cast for security reasons so there is no way around it. Either explicit or implicit, a cast check will be performed.

Bill



Which is a point worth noting. Traditional strongly-typed languages would have ascertained the datatype from context at compile-time in many cases. But context can be defeated by patching code or jumping into the middle of otherwise secure code. Java is paranoid and doesn't assume that it can trust that the actual execution sequence and context is related to the source code.

But things like this can be done in some extremely gnarly ways. I think the Intel iapx432 CPU actually went so far as to put objects in their own private address spaces, so potentially, a class violation would manifest itself at the hardware level. And I don't even want to think of what IBM might do next on their zSeries hardware - they've already added Java-compatible (IEEE) floating point alongside their original floating point instructions and placed a good-sized chunk of the ANSI C library into microcode (zSeries is the antithesis of a RISC machine!).

So I've completely given on on trying to predict how low-level things get done anymore. There are just so many evil possibilities. Better just to try things and measure them. And expect that tomorrow, they'll be even more unpredictable.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:
The main difference, in fact, is that string.valueOf() can throw a NullPointerException, so the two code samples are not functionally equivalent!



In a way, it's actually the other way around. The static method String.valueOf(s) will always return non-null, even if s is null, whereas (String) s will return null if s is null. For this reason I find I use String.valueOf() quite often, as it often makes sense to get rid of nulls this way in UI code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic