autoboxing

Why can't I call toString() on a Java primitive?

I want to convert a primitive to a string, and I tried: myInt.toString(); This fails with the error: int cannot be dereferenced Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods. However, Java 5 introduced autoboxing and unboxing (a la C#... which I never liked in C#, but that's besi...

Is this really widening vs autoboxing?

I saw this in another question in reference to shortcomings of the java spec: There are more shortcomings and this is a subtle topic. Check this out: public class methodOverloading{ public static void hello(Integer x){ System.out.println("Integer");}public static void hello(long x){ System.out.println("long");}public stati...

Userland autoboxing?

Is it possible to implement autoboxing for your own classes? To illustrate my example, this is what I might want to write: Foo foo = "lolcat"; And this is what Java would do (as per my own definitions, somewhere, somehow), under the hood: Foo foo = new Foo(); foo.setLolcat("lolcat"); So, is this possible somehow, or is it a JVM-fe...

What code does the compiler generate for autoboxing?

When the Java compiler autoboxes a primitive to the wrapper class, what code does it generate behind the scenes? I imagine it calls: The valueOf() method on the wrapper The wrapper's constructor Some other magic? ...

Why does autoboxing make some calls ambiguous in Java?

I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The simplest example appears to be this: public class Test { static void f(Object a, boolean b) {} static void f(Object a, Object b) {} static void m(int a, boolean b) { f(a,b); } } When compiled, it causes the following error: ...

Java: What's the difference between autoboxing and casting?

This question is about "Why does autoboxing make some calls ambiguous in Java?" But reading through the answers, there are a number of references to casting and I'm not sure I completely understand the difference. Can someone provide a simple explanation? ...

Java: Array of primitive data types does not autobox

I have a method like this: public static <T> boolean isMemberOf(T item, T[] set) { for (T t : set) { if (t.equals(item)) { return true; } } return false; } Now I try to call this method using a char for T: char ch = 'a'; char[] chars = new char[] { 'a', 'b', 'c' }; boolean member = isMemberOf(ch, ...

Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);

Autoboxing seems to come down to the fact that I can write: Integer i = 0; instead of: Integer i = new Integer(0); So, the compiler can automatically convert a primitive to an Object. Is that the idea? Why is this important? ...

Java: Integer value comparison

I'm a newbie Java coder and I just read a variable of an integer class can be described 3 different ways in the api. I have the following code.. if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table. My goal is the figure out how to see if th...

How to convert int[] into List<Integer> in Java?

How do I convert int[] into List<Integer> in Java? Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java. ...

build tool to detect autoboxing?

Does anyone know of any style checkers or build tools that would flag autoboxing and unboxing from the build server? I already have the eclipse option to flag it on my end, but not everyone in the project uses the same IDE or same settings. Finding it on the build side seems the only way to detect where it might creep into the project...

I get these weird characters when I try to print out a vector element!

I'm using Netbeans. When I run the program below, I get this as output [I@de6ced! How come? import java.util.Arrays; import java.util.Vector; public class Test { public static void main (String[] args) { int[] a = new int[1]; a[0] = 5; Vector<Integer> a1 = new Vector(Arrays.asList(a)); System.out.println(a1.element...

When comparing two Integers in Java does auto-unboxing occur?

I know that if you compare a boxed primitive Integer with a constant such as: Integer a = 4; if (a < 5) a will automatically be unboxed and the comparison will work. However, What happens when you are comparing two boxed Integers and want to compare either equality or less than/greater than? Integer a = 4; Integer b = 5; if (a == ...

Do autoboxing and unboxing behave differently in Java and C#

I am manually converting code from Java (1.6) to C# and finding some difficulty with the behaviour of primitives (int and double). In C# it appears that almost all conversions happen automatically List<double> list1 = new List<double>(); // legal, C# double d0 = 3.0; list1.Add(d0); // ...

Java: Why isn't autoboxing happening here?

This gives me an error: int[] l = new int[] {0, 2, 192, -1, 3, 9, 2, 2}; int[] l2 = new int[] {9001, 7, 21, 4, -3, 11, 10, 10}; int[] l3 = new int[] {5, 5, 5, 64, 21, 12, 13, 200}; Set<List<Integer>> lists = new HashSet<List<Integer>>(); lists.add(Arrays.asList(l)); Eclipse: The method add(List<Integer>) in the type Set<List<Inte...

Java auto boxing

I switched a project I'm working on over to maven and suddenly auto boxing seems to have broken. My IDE (NetBeans) complains on lines such as the one below with the error "Incompatible types" Integer order = 4; ...

How can I prevent myself from accidentally using an NSNumber's pointer when I want its intValue?

One feature of Java that I really like is autoboxing, in which the compiler automatically converts between primitives and their wrapper classes. I'm writing a Core Data application in Objective-C/Cocoa, and I'm finding it frustrating to deal with my integer attributes programmatically. Here's why: //img is a managed object that I have ...

Internal compiler error ArrayIndexOutOfBoundsException: -1 ... generateUnboxingConversion

I got some weird exception when trying to compile this: Byte b = 2; if (b < new Integer(5)) { ... } Is it a valid check (unboxing-implicit cast - unboxing)? ...

Boxed Primitives and Equivalence

So I was asked this question today. Integer a = 3; Integer b = 2; Integer c = 5; Integer d = a + b; System.out.println(c == d); What will this program print out? It returns true. I answered it will always print out false because of how I understood auto (and auto un) boxing. I was under the impression that assigning Integer a = 3 wil...

Java automatic unboxing - is there a compiler warning?

I am a big fan of auto-boxing in Java as it saves a lot of ugly boiler plate code. However I have found auto-unboxing to be confusing in some circumstances where the Number object may be null. Is there any way to detect where auto-unboxing is occurring in a codebase with a javac warning? Any other solution to detect occurrences of unboxi...