Questions tagged [autoboxing]

Boxing is the process of using an object to wrap a primitive value so that it can be used as a reference object; extracting a previously-boxed primitive is called unboxing. Auto(un)boxing is a form of "syntactic sugar" where the compiler automatically performs (un)boxing for you, allowing you to use value and referenced types interchangeably.

Autoboxing is a Java term where the Java compiler automatically performs from a primitive value to its corresponding wrapper class. The inverse of autoboxing is called . It is considered a form of boxing in Object-oriented programming.

Examples of autoboxing:

Converting a char to a Character.

Double x = 3.141592 as opposed to Double x = new Double(3.141592). The double literal is converted to the Double wrapper.

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(100); // Autoboxing performed as an int is passed. Compiler converts to Integer.

Link to Oracle Java Documentation about autoboxing and unboxing

398 questions
0
votes
2 answers

Understand Scala immutable Map behaviour

I used a scala immutable map as per below. val d = "4.55" This is working fine. val properties = Map("title"->"title" , "value" -> d ) Its convert from [String , AnyRef] to [String, Any] val properties = Map("title"->"title" , "value" ->…
Sagar Vaghela
  • 1,165
  • 5
  • 20
  • 38
0
votes
0 answers

How does unboxing and auto boxing occur when a method reference is passed for a generic method?

Here is a piece of code: @FunctionalInterface interface NumericFunc{ int fact(T[] a,T b); } class MyStringOps{ static int counter(T[] a,T b){ int count=0; for(int i=0;i
0
votes
1 answer

The expression of type long is boxed into Long

I am having a method public double getNumberOfBooks(final int count, final double Price, final Long Quantity, final double totalValue) Now I am calling this method from other java class as mentioned below public void…
Jhon
  • 27
  • 4
0
votes
1 answer

Java Autoboxing/Unboxing and Generic Type issue

I'm testing my knowledge on Generics in Java, and I'm coming across an issue. The code return (t%2==0); in my IsEven.java wont compile, as well as int comparison = t.compareTo(memberT); in my GreaterThan.java I assume this is an error with how I…
0
votes
1 answer

autoboxing in generic, can autoboxing work with object

why no autoboxing in line sum +=nums[i].doubleValue(). u can see nums[i] is in object form why can't just write sum +=nums[i], why need doubleValue() which is manual way of autoboxing and unboxing. i have successfully compiled a program where number…
indian0 girl
  • 121
  • 1
  • 8
0
votes
1 answer

the experiment of auto boxing in algorithms 4th edition

it is a experiment about auto-boxing. The main idea is to execute a mount of pop() and push(), separately with Stack and Stack. The core class below: public class FixedCapcityStack { private final int cap; private Item[] arr; …
user13457
  • 23
  • 3
0
votes
2 answers

Where do you put the parentheses to concisely convert a casted object to a primitive type without auto-unboxing?

With autounboxing, this statement will automatically work: int myPrimitive = (Integer) doIt(); But if I want to explicitly convert from an Integer to an int here in a single line, where do I have to put the parentheses?
faq
  • 757
  • 2
  • 8
  • 9
0
votes
0 answers

How does assigning primitives to Object works?

Lets say I got those two lines: Object obj; obj = false; Will obj casted into a Boolean (the wrapped class of boolean)?
Delupara
  • 359
  • 1
  • 4
  • 10
0
votes
0 answers

Why Autoboxed Integer isn't incremented inside the caller

Consider the code: static void incr(Integer n) { n++; } public static void main(String args[]) { Integer n = 66; // Autoboxing incr(n); System.out.println(n); } The above program outputs 66. I expected it to print 67 and here is…
user5818995
0
votes
2 answers

How to check the return type of a generic method to prevent NPE when unboxing

Consider the following code: import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * * @author Colby */ public class Entity { //test code public static void main(String[] args) { Entity e = new Entity(); …
0
votes
1 answer

Java doesn't box and convert primitive types at the same time

I know that java(c?) can box and unbox types and convert between primitive types when necessary, but why does it not want to do that both at the same time. For example if I were to do this: ArrayList bytes = new…
S.Klumpers
  • 410
  • 3
  • 14
0
votes
3 answers

confusion autoboxing usage with generics

As far as I learn, with Integer example autoboxing usage is: Integer iOb2 = 88; // auto-boxing Integer iOb = new Integer(88) // is it auto-boxing ? I think no // if it is auto-boxing what about above line? The above…
askque
  • 319
  • 2
  • 9
0
votes
0 answers

Java primitive-Object comparison: is the primitive autoboxed or the object unboxed?

According to the Java tutorial on autoboxing (and unboxing) Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a…
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
0
votes
4 answers

NPE while autoboxing in Java

I have the following piece of code: map = db_.treeMapCreate(validMapName_) .keySerializer(Serializer.LONG) .valueSerializer(Serializer.LONG) .make(); //mapDB protected void onTaskCompletion(TaskInfo task) { long…
user1715122
  • 947
  • 1
  • 11
  • 26
0
votes
0 answers

Why comparing an Object with a numeric literal compiles in Java?

I'm wondering why the following code snippet compiles in Java 7 without a warning Map map = new HashMap<>(); map.put("key", 0); if (map.get("key") != 0) { System.out.println("what?!"); } Studying the generated bytecode reveals…