• Post Reply Bookmark Topic Watch Topic
  • New Topic

Autoboxing & API :-D  RSS feed

 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The API says
public static int parseInt(String s) throws NumberFormatException

I can understand that because of Autoboxing feature the above code works fine inspite of API's declaration of int

Why sun change their API according to their new feature Autoboxing like
public static [int|Integer] parseInt(String s) throws NumberFormatException

So while seeing API itself we can understand the return type is int or Integer.

Correct me if I am wrong.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun did not change their API. The method Integer.parseInt(...) returns an int. It did so in Java 1.4 and also in Java 5 and 6. There is no version of Integer.parseInt(...) that returns an Integer.

What is happening is that the int returned by Integer.parseInt(...) is automatically wrapped into an Integer in this line:

Integer K = Integer.parseInt("5");

This is exactly the same as:

Integer K = new Integer(Integer.parseInt("5"));

The JDK 1.5 (or newer) compiler is so kind to automatically add the "new Integer(...)" part for you - that's autoboxing.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:

Integer K = Integer.parseInt("5");

This is exactly the same as:

Integer K = new Integer(Integer.parseInt("5"));


Not *exactly*. It's

Integer K = Integer.valueOf(Integer.parseInt("5"));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!