• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer.parseInt() vs String concatenation  RSS feed

 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between converting an primitive data type integer to a string object?
int i=10;
String s1=Integer.parseInt(i) and
String s2=i+" ";
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parseInt doesn't have a version that accepts an int.
 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith!
Please see my posting on Exception
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that

i + "";

basically compiles to

new StringBuffer().append(i).append("").toString();

where append(i) basically leads to a call to String.valueOf(i). So I think it would make sense to simply call that method directly...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
parseInt doesn't have a version that accepts an int.


I'm sure they meant Integer.toString(i). As Ilja hinted at the first doesn't require constructing an entire StringBuilder and going through a lot of extra work to do the same thing.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Notice that

i + "";

basically compiles to

new StringBuffer().append(i).append("").toString();

where append(i) basically leads to a call to String.valueOf(i). So I think it would make sense to simply call that method directly...


Most 1.5 implementations use StringBuilder here. The specification leaves it open for implementations.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:


Most 1.5 implementations use StringBuilder here. The specification leaves it open for implementations.


Ah, true, thanks. I often forget about that. :roll:

It's still basically the same argument, though...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!