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

Auto boxing and primitive types to match method signature

In version 1.5, Java have introduced the concept of auto-boxing. public interface SomeInterface { public void test(Integer val); } public class Main implements SomeInterface { /*The method signature gets different and compiler is asking to…
Vishrant
  • 15,456
  • 11
  • 71
  • 120
0
votes
4 answers

java: Auto-boxing and casting?

I'm confused with a small problem , see the following : Double j = new Double(5); // No problem. double j =5;// //But //Here the problem: Double j = 5; Long k =5; Float g = 5.0; I know the solution but I want to understand why…
0
votes
1 answer

Autoboxing not working for Boolean

I have a simple class below which when compiled autoboxes the Integer correctly But, fails to do it for my Boolean it insists that I should change the parameter to a boolean. I am using jdk 1.8 otherwise the compiler would have complained about the…
user1561783
  • 483
  • 1
  • 4
  • 14
0
votes
1 answer

Role of cache in autoboxing

Item 5 in Effective Java Joshua Bloch says avoid creating your object pool unless the objects are extremely heavy weight but in jdk source I see IntergerCache in Integer class, LongCache and CharacterCache in Long and Character class. public…
rakesh99
  • 1,234
  • 19
  • 34
0
votes
1 answer

Order of searching java overloaded method

I have the following methods: static void f(double x) { System.out.println("f(double)"); } static void f(Double xObj) { System.out.println("f(Double)"); } static void f(double... s) { System.out.println("f(double...)"); } public…
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
0
votes
1 answer

Integer/ int autoboxing query with generic functions in Java

Okay, so I am trying to write a generic sorting package in Java (I'm taking an algorithms course and I see it as good practice, both in the algorithmic sense and in Java as I'm very new to the language). Anyway, I figure the best way to do this is…
Keir Simmons
  • 1,634
  • 7
  • 21
  • 37
0
votes
3 answers

Integer Comparison Results Vary in Java

I am a novice Java programmer and came across a very weird scenario, as below. public static void main(String[] args) { Integer a = 500; Integer b = 500; // Comparing the values. a <= b; // true a >= b; // true a == b; //…
user3374518
  • 1,359
  • 3
  • 11
  • 11
0
votes
3 answers

error: no suitable method found for put(String,int)

I got errors when compiling this: TreeMap myMap = new TreeMap (); //populate the map myMap.put("preload_buffer_size", 1024); myMap.put("net_buffer_length", 1024); //etc... error: no suitable method found for…
user3344382
  • 1,941
  • 4
  • 18
  • 17
0
votes
0 answers

valueOf inside Mockito.when

I have this construction: Mockito.when(Integer.valueOf(serviceMock.getScore(bet))).thenThrow(e); int getScore(final MatchBetModel bet); what does it mean valueOf inside when? I have strange eclipse specific, and if I delete valuOf I get error. I…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0
votes
6 answers

Java propery way to delete an Integer from ArrayList

I have an array list of integers and would like to delete an Integer value from it. To clarify: Let's say I have a function that takes in an ArrayList of Integers, and specified Integer value. I need to return the list with (value+1) deleted from…
Maggie
  • 7,823
  • 7
  • 45
  • 66
0
votes
3 answers

How to ensure == will always work with primitives as an equality test

From what I understand, if I have two long or int, the == operator to test equality of values will not work sometimes due to autoboxing. What do I need to do to ensure that == will work in every possible scenario when dealing with primitives?
user2763361
  • 3,789
  • 11
  • 45
  • 81
0
votes
2 answers

JLayeredPane error when the layer is an Integer

I have a Canvas subclass object that I'm trying to add, along with some other Canvas subclasses, to a JLayeredPane. In the documentation for JLayeredPane, the layer is given as an Integer, e.g. layeredPane.add(child, new Integer(0)); However, when…
Jorocco
  • 83
  • 2
  • 9
0
votes
6 answers

Is converting int to a String counted as Autoboxing?

AFAIK When Java automatically converts a primitive type into wrapper class object than its called autoboxing because primitive is boxed into wrapper class. So is int test = 3; String str = String.valueOf(test); counted as autoboxing? Reason to…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0
votes
2 answers

Java: is happening automatic wrapping?

Directly from the javadoc: s', 'S' general If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString(). Does it mean…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
0
votes
2 answers

Get autobox class from type

I know this question may seem silly because I could just do this manually. But I like to have all my options in one place (and one place only). I want to set up the available options for a program (using commons-cli) by using the names of the…
dspyz
  • 5,280
  • 2
  • 25
  • 63