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

regarding Collection methods for versions >=Java 5

I got a doubt and want to know which class method might have been used by JRE for removing objects in below code. Because remove is having two signatures remove(int) and remove(Object). As a part of AutoBoxing feature, here int will be considered as…
Sudhakar Chavali
  • 809
  • 2
  • 14
  • 32
0
votes
4 answers

How can I auto box this class?

I've the following class: public class IntegerKey extends Number implements Comparable { private Integer m_key; public IntegerKey(Integer key) { m_key = key; } public IntegerKey(int key) { m_key = key; …
Shvalb
  • 1,835
  • 2
  • 30
  • 60
0
votes
1 answer

Autoboxing a character value and then returning a blank value

I have this piece of code that generates a random characters (ASCII) public char getRandChar(){ return (char)rand.nextInt(27); } and then I'll print it out using this System.out.println(new Character(getRandChar())); How but apparently it is…
KyelJmD
  • 4,682
  • 9
  • 54
  • 77
0
votes
3 answers

About Java memory management

Possible Duplicate: Integer wrapper objects share the same instances only within the value 127? I have a question about the memory management in Java. When I try the following code: Integer a = 1; Integer b = 1; System.out.println(a==b); // this…
penpen926
  • 115
  • 1
  • 6
0
votes
2 answers

How to commonly address primitives and objects

Here is a utility method I have: public static Class[] getTypes(Object[] objects){ Class[] types = new Class[objects.length]; for (int i = 0; i < objects.length; i++) { types[i] = objects[i].getClass(); } return…
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
0
votes
0 answers

Java Object of Type

I have seen this question posted a few times, but the answer does not seem to work for me. I have an Object and need to know what type this is ie (Object object) when I run the code and watch object then the object.typeName is shown as…
Tractor Boy
  • 21
  • 1
  • 2
0
votes
4 answers

Need to understand Boxing in core java

I cant figure out how the int 7 is consider as object in below example. The sifer(7) is consider to be method sifer(Object o). I am not able to get it how this happened. In one of my java reference book it says Int can be boxed to an Integer and…
MKod
  • 803
  • 5
  • 20
  • 33
0
votes
1 answer

Autoboxing doesn't work with Parameterized types

I have a parameterized generic class X which takes a type T. On which no conditions (like T extends/implements) have been defined. class X { Map map = new HashMap(); public void put() { …
Kryptic Coder
  • 612
  • 2
  • 8
  • 20
0
votes
3 answers

JavaScript: String compared with numeric

We know that by default, the 'obj' below is string. Without using 'parseInt', how does JavaScript compare it with a number? obj = document.frm.weight.value; if( obj < 0 || obj > 5 ){ alert("Enter valid range!"); return false; }
Pop Stack
  • 926
  • 4
  • 19
  • 27
-1
votes
1 answer

Equivalence of String and string in C# test

Possible Duplicate: String vs string in C# I have a test in C# code I'm reading: if (variable is string) I am wondering if this is strictly equivalent to: if (variable is String) or if some esoteric behavior of C# autoboxing may cause these…
Kheldar
  • 5,361
  • 3
  • 34
  • 63
-1
votes
1 answer

Why does this code give a null pointer Exception? I thought Character class could handle null being assigned?

public class Playground { public static void main(String[] args) { String s = "blah"; Character lclfs = s.contains("/") ? '/' : s.contains("\\") ? '\\' : null; } } What am I missing (using Java 1.8)?
JGFMK
  • 8,425
  • 4
  • 58
  • 92
-1
votes
2 answers

Why the code that the compiler approves but cannot be run by JVM?

This is an exercise in Head First Java. The exercise is about autoboxing (wrapping and unwrapping). Why does the compiler approve that assigning Integer i (default value is null) to int j (default value is 0)? When I run it, it shows: "Cannot invoke…
Allen
  • 11
  • 5
-1
votes
2 answers

I need Java Custom Autoboxing

Is it possible to define custom autoboxing in Java? I need to automatically convert java.lang.Number into my class org.expr.NumberExpression when typing parameters of a function. For two parameters, I ended up writing four very similar…
Joe DiNottra
  • 836
  • 8
  • 26
-1
votes
2 answers

Incompatible types: int cannot be converted to integer

The int should autobox but I do not know why it is not. When i try to compile, it gives me an incompatible types error. Is there something wrong with my code? Scanner console = new Scanner(System.in); ArrayList list = new…
-1
votes
2 answers

auto boxing and un boxing what is the difference and when to use it?

hi all i am still beginner to java may someone explain to me what is the difference between auto-boxing and Un-boxing use for and when to use that? //this is my sample code ArrayList listOfDoubles = new ArrayList(); …
Steven Y.
  • 41
  • 1
  • 6
1 2 3
26
27