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
3 answers

Autoboxing for String in java

Integer l = Integer.valueOf("100"); // valid no compilation Integer l = "100"; //Error : cannot convert from string to integer Why I am facing the above error please suggest to me. (Need to use Autoboxing concept for second line)
0
votes
2 answers

Integer becomes Long when passed to the method that is expecting a Object Type

public class Main { public static void main(String args[]) { process(true ? 1 : 2L); } static void process(Object object) { System.out.println(object instanceof Integer); } } My expected output is true.…
0
votes
2 answers

Seeking documentation on compiler-generated lambdas to convert specialized to generic functional objects in Java 8

I’ve been working with Java 8 lambdas for several months and just now discovered a behavior that I don’t remember seeing any mention of in the Java docs or programming websites. It can be seen in the following code: public class…
0
votes
1 answer

Why I got position at 0? When it should be 1

I learn Java, now I'm at ArrayList, I wrote some methods about banking, when I want to add a new customer at a specific branch, the position always return 0. This is the source code of the methods that I'm talking about: The instances in Main class…
Abdelillah
  • 79
  • 1
  • 14
0
votes
1 answer

Why toString does not work when String.valueOf() works at casting

I need to parse Json into Map[String,String] structure. Json may contain numeric and string types as values. So in order to store it as String I've applied toString method and it throws ClassCastException. However if String.valueOf() is applied…
jk1
  • 593
  • 6
  • 16
0
votes
0 answers

Why can't my Java functional interface modify its Integer input?

EDIT 1: I am aware this question has been marked as a duplicate and I initially deleted it. However, after reading in this Meta Post that deleting is bad and that I should edit my question rather than deleting it, I undeleted and am trying to…
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
0
votes
1 answer

Java autoboxing int to Long

Why can't the Java compiler directly cast an int to a Long? long test = 1; // ok Long test2 = 2; // does not compile! Long test3 = 3L; // ok It's especially frustrating since (of course) one can do long test = 1; Long test2 = test; Is it a…
Nicola Ambrosetti
  • 2,567
  • 3
  • 22
  • 38
0
votes
2 answers

Java : autoboxing of an Object array of integers, cast to int from LinkedList.toArray()

I would like to use code similar to the following: int letterIndex[]; LinkedList letterList; ... if(!letterList.isEmpty()) letterIndex = (Integer[])letterList.toArray(); However, it is not allowed, and apparently the cast to Integer[] is…
TurtleToes
  • 2,047
  • 2
  • 17
  • 17
0
votes
1 answer

If method argument is a primitive int, then myArrayList.contains(primitiveArg) within a loop within a method is enormously inefficient or OK?

I have code like this: public int getDistanceToNumber(int number) { List tuple5 = null; int distanceCounter = 0; for (int i = 0; i < allDraws.size(); i++) { tuple5 = allDraws.get(i).getTupleAsList(); if…
caasdads
  • 409
  • 4
  • 12
0
votes
1 answer

Quick autoboxing/auto-unboxing question in Java

I was just introduced to the concept of autoboxing in Java and I have a couple of quick questions to help me clarify my understand. From what I understand is that when we declare an arraylist such as ArrayList myList = new…
Ayumu Kasugano
  • 440
  • 4
  • 13
0
votes
0 answers

Unnecessary unboxing and unboxing performance in Java

For a code excerpt like the following int a; Integer b = new Integer(1); a = b.intValue(); the Java compiler generates an "Unnecessary unboxing" warning. Is that superfluous, or is there more substance behind it? Other answers (like this and…
PNS
  • 19,295
  • 32
  • 96
  • 143
0
votes
3 answers

Auto-boxing followed by Widening in Java

Following program prints Object as output, and when I remove the overloaded method contains Object as parameters, following compile time error is there: The method m1(Long) in the type LangPackage is not applicable for the arguments (int) …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
0
votes
3 answers

java autoboxing from int to java.lang.Long casting issue

I'm getting compilation error from trivial substraction Long result; int operand1 = 10; int operand2 = 5; result = operand1 - operand2; from last line: incompatible types: int cannot be converted to java.lang.Long beside the fact that I believe it…
Pavel Niedoba
  • 1,554
  • 2
  • 19
  • 36
0
votes
0 answers

JdbcTemplate update, why do we need to pass in types?

This post helped me a bit, explaining that JdbcTemplate will figure out which column to fill on its own. However, it means that there's no need to pass in the types, so why/when should we do it? Another related question: I accidentally passed an…
PhoenixPan
  • 536
  • 7
  • 19
0
votes
2 answers

Do wrapper class objects get unboxed while being assigned to another object?

When we assign objects of the same type to one another, the new object stores the address of the object that's being assigned to it. For instance, if I have my own class named CLA then the following code will produce 11 as the output: public class…
progyammer
  • 1,498
  • 3
  • 17
  • 29