Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

Autoboxing in Java

How does following expression evaluated?

Student class :

public class Student
{
    private Integer id;
    // few fields here

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id=id;
    }

    //setters and getters
}

And in some method :

{
    int studentId;

    // few lines here

    if(studentId==student.getId())  // **1. what about auto-unboxing here? Would it compare correctly? I am not sure.**
    {
        //some operation here
    }
}

Upvotes: 5

Views: 1022

Answers (6)

dku.rajkumar
dku.rajkumar

Reputation: 18588

Yes it will work fine. But it is usually not advisable to use wrapper class until there is not other go.

Upvotes: 1

Guillaume
Guillaume

Reputation: 22822

As per specifications, http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

The only advisable cases to use the wrapper classes (Integer, etc.) are when you want to stick numeric values in a collection, or null is an acceptable value for your use-cases. That's it.

Side-effect include unwanted potential NullPointerException and decrease in performance.

Upvotes: 1

soulcheck
soulcheck

Reputation: 36777

Yes it will work, as it will convert right operand to corresponding numeric type, according to java language specification:

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

corresponding paragraph in jls

So actually any of the operands can be of numeric type for java to autounbox the other one.

And then §5.1.8 says that conversions include unboxing conversion.corresponing paragraph in jls

Upvotes: 1

David Webb
David Webb

Reputation: 193814

The comparison

studentId==student.getId()

will work, but will throw a NullPointerException if student is null.

As a rule autoboxing prefers primitives, i.e. it will convert an Integer to int where possible rather than the other way around. Your example shows one good reason for this, since equality for reference objects is tricky. So it is possible for:

studentId==student.getId().intValue()  

to be true but

new Integer(studentId)==student.getId()

to be false, since whilst they have the same value they're not the same object.

Upvotes: 1

Thomas Johan Eggum
Thomas Johan Eggum

Reputation: 915

Yes this will work, but note!

If the Integer value id in Student is null, you will have a NullPointerException when evaluating

studentId == student.getId();

Note also that autoboxing will have some performance cost, so you should only use it if you have to.

Read more here: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

Upvotes: 3

stacker
stacker

Reputation: 69002

Yes, this will work it is equivalent to

studentId==student.getId().intValue()  

as long student.id is not null.

Upvotes: 4

Related Questions