Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Autoboxing & API :-D

 
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
author
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"));
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic