To properly understand the difference, you should read an introductory textbook on Java. I will make a small stab at answering here.
Everything I say here applies analogously to char and Character, short and Short, long and Long, float and Float, double and Double.
An Integer, is a Object that contains a single int field. An Integer is much bulkier than an int. It is sort like a Fedex box to contain the int. Integers are immutable. If you want to affect the value of a Integer variable, the only way is to create a new Integer object and discard the old one.
|int vs Integer|
|Calculate with it, + - * / % ^ etc.|
|Pass it as a parameter|
|return it as a value|
|Use methods on it from java.lang.Integer|
|Store it in a Vector or other Collection|
|Use it as a HashMap key|
|send it by itself over RMI (Remote Method Invocation)|
|send it as part of another Object over RMI|
|pass it as a generic object to a TableCellRenderer. You may pass a variety of different types via the same paramter.|
|Allow for a null value that means there is no value.|
// to int i from Integer ii int i = ii.intValue(); // to Integer ii from int i Integer ii = new Integer( i );See this Amanuensis for other conversions.
Starting with Java 1.5, with boxing and unboxing, the compiler will sometimes automatically do the conversions to and from int and Integer for you so you can treat int and Integer as if they were almost the same thing.
Why are there both int and Integer? For speed. ints, without any Object packaging are compact and fast. Would it not have been easier if there were only one of sort of creature that could do everything and have the compiler automatically figure out when the packaging was needed and when not? The Eiffel language designers thought so, and the Java designers are gradually coming around to the same conclusion.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[184.108.40.206]
|Feedback||You are visitor number 412,914.|