How is unboxing done in Java? [duplicate] - java

This question already has answers here:
How does auto boxing/unboxing work in Java?
(4 answers)
Closed 7 years ago.
I know how boxing is done. For example, when you write Integer a = 4, Integer.valueOf(int) is being called. Likewise, can i see the implementation code for unboxing as well?
Update : It is answered in the suggested question.

In such cases,intValue() gets called to get the primitive value.
Integer a= new Integer(4);
int val = a.intValue();
Like the integer, all the wrapper classes have this method to get it's primitive value.

Related

Why use int instead of Integer (or the other way around) in Java? [duplicate]

This question already has answers here:
When to use wrapper class and primitive type
(11 answers)
What is the difference between Integer and int in Java?
(11 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I know that Integer is a class and int is just a number, but when should we use one over another and how to convert Integer to int (and vice versa)?
** Also, there is this
question (what is autoboxing by the way) but I am asking WHEN to use int over Integer in actual programming (such as performance issues, ease of use and readability) and vice versa and not the DIFFERENCE between them.

Why do we create object variables like this? [duplicate]

This question already has answers here:
Differences between new Integer(123), Integer.valueOf(123) and just 123
(6 answers)
create a new Integer object that holds the value 1?
(2 answers)
Advantage of using new Integer(a) over a
(3 answers)
using new(Integer) versus an int
(3 answers)
Closed 3 years ago.
I am new to learning Java, and I was told to create object variables like this:
Integer a = new Integer(2);
Instead of like this:
Integer a = 2;
Can someone explain why is creating object variables the 2nd way bad?
edit: I am adding this here cause I am getting mixed answers
Which one am I supposed to use and when?
The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
(Emphasis mine.)
See also: Autoboxing

Java Newbie - When to use it? Relating to Boxing/Unboxing I think [duplicate]

This question already has answers here:
Which is better: letting Java do autoboxing or using valueOf()
(3 answers)
Closed 6 years ago.
Integer x = 5;
Integer x = Integer.valueOf(5);
Is there any scenario where I would want to use the 2nd one specifically or is it redundant altogether and shouldn't not bother about it?
After Java 5 (because of autoboxing / unboxing) there is no difference except the first one is shorter.
Both statements are equivalent.
The statement Integer x = 5 would be compiled to
Integer x = Integer.valueOf(5);
The compiler will do that for you behind the scene, so the only difference is the number of character in source file.

What is the correct way to create a object array in java? [duplicate]

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I've seen examples such as:
Type arrayname[] = new Type[];
also as written as:
Type[] arrayname = new Type[]
I am quite confused about such expressions!
Where exactly should I put the []?
Any of the above are allowed. All produce the same bytecode. JLS-10.2 Array Variables says (in part)
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

What is the difference between i+=d and i=i+d in Java? [duplicate]

This question already has answers here:
Closed 13 years ago.
Duplicate:
java += question
Why aren’t op-assign operators type safe in java?
When i is an integer and d is a double, i+=d works but i= i+d does not.
Why is this?
i = i + d does not work because you would be assigning a double to an int and that is not allowed.
The += operator casts the double automatically to an int, so that is why it works.
This is the link to the information on the spec:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5304

Categories