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
}
}
Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.
Unboxing in Java is an automatic conversion of an object of a wrapper class to the value of its respective primitive data type by the compiler. It is the opposite technique of Autoboxing. For example converting Integer class to int datatype, converting Double class into double data type, etc.
Boxing is the mechanism (ie, from int to Integer ); autoboxing is the feature of the compiler by which it generates boxing code for you.
Yes, this will work it is equivalent to
studentId==student.getId().intValue()
as long student.id is not null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With