autobox « Integer « Java Data Type Q&A

1. Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);

Autoboxing seems to come down to the fact that I can write:

Integer i = 0;
instead of:
Integer i = new Integer(0);
So, the compiler can automatically convert a primitive to an Object. ...

2. Java: Integer value comparison

I'm a newbie Java coder and I just read a variable of an integer class can be described 3 different ways in the api. I have the following code..

if ...

3. When comparing two Integers in Java does auto-unboxing occur?

I know that if you compare a boxed primitive Integer with a constant such as:

Integer a = 4;
if (a < 5)
a will automatically be unboxed and the comparison will work. However, ...

4. Java: Is it ok to set Integer = null?

I have a function that returns an id number if the argument exists in the database. If not, it returns null. Is this begging for a null pointer exception? Negative id ...

5. Why can't the compiler/JVM just make autoboxing "just work"?

Autoboxing is rather scary. While I fully understand the difference between == and .equals I can't but help have the follow bug the hell out of me:


6. Is it guaranteed that new Integer(i) == i in Java?

Consider the following snippet:

    int i = 99999999;
    byte b = 99;
    short s = 9999;
    Integer ii = ...

7. Why does int num = Integer.getInteger("123") throw NullPointerException?

the following code throws NPE for me:

int num = Integer.getInteger("123");
is my compiler invoking getInteger on null since it's static? that doesn't make any sense! can someone explain what's happening? thanks.

8. The value of Integer.valueOf()

Is there any reason to use Integer.valueOf(X) to initialize a final Integer, as below:

public class MyClass
  public static final Integer DAY_1 = Integer.valueOf(1); // Why do it this way?

9. Java question about autoboxing and object equality / identity

public class Main { 
      * @param args the command line arguments */ 
    public static void main(String[] ...

10. Why do the Integer.valueOf(...) comparisons in this example return different values?

From the answer to a question about primitive types and autoboxing in java:

for biziclop: class biziclop {
public static void main(String[] args) {

11. double and Integer conversion

I am trying to figure this out:

double chiSquare = ((double)(hashtable.get(key).intValue()/noWords))/* * Math.log10((NO_DOCUMENTS/all.get(key)))*/;
if (key.equals("love")){
    System.out.println(all.get(key));           ...

12. Why aren't Integers cached in Java?

I know there are similar posts on the topic, but they don't quite address my question. When you do:

Integer a = 10;
Integer b = 10;
System.out.println("a == b: " + (a ...

13. Why can Integer and int be used interchangably?

I am confused as to why Integer and int can be used interchangeably in Java even though one is a primitive type and the other is an object? For example:

Integer b = ...

14. autoboxing: int to double

15. AutoBoxing Integer to int conversion