The Artima Developer Community
Sponsored Link

Java Buzz Forum
Cute way to convert an int to a String

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Nick Lothian

Posts: 397
Nickname: nicklothia
Registered: Jun, 2003

Nick Lothian is Java Developer & Team Leader
Cute way to convert an int to a String Posted: Jun 7, 2005 7:41 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Nick Lothian.
Original Post: Cute way to convert an int to a String
Feed Title: BadMagicNumber
Feed URL: http://feeds.feedburner.com/Badmagicnumber
Feed Description: Java, Development and Me
Latest Java Buzz Posts
Latest Java Buzz Posts by Nick Lothian
Latest Posts From BadMagicNumber

Advertisement

I picked up a nice little tip for easy conversion between an int and a String. Previously I'd always done something like

int one = 1;
String str = String.valueOf(one);
The alternative is
int one = 1;
String str = one + "";
From the bytecode point of view it isn't quite as efficient:

   L0 (0)
    ICONST_1
    ISTORE 1: one
   L1 (3)
    ILOAD 1: one
    INVOKESTATIC String.valueOf(int) : String
    ASTORE 2: str
   L2 (7)
    RETURN
   L3 (9)
vs

   L0 (0)
    ICONST_1
    ISTORE 1: one
   L1 (3)
    NEW StringBuffer
    DUP
    ILOAD 1: one
    INVOKESTATIC String.valueOf(int) : String
    INVOKESPECIAL StringBuffer.(String) : void
    INVOKEVIRTUAL StringBuffer.toString() : String
    ASTORE 2: str
   L2 (11)
    RETURN
   L3 (13)
All the same, I'd never thought of using autoconversion to convert to a string like that before. (note this doesn't rely on JDK 1.5 autoboxing)

Read: Cute way to convert an int to a String

Topic: Dirty J2ME Secrets Previous Topic   Next Topic Topic: XBox 360 - Reaching 1 billion people? - Interesting interview of J Allard, Microsoft Corporate...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use