autoboxing

Strange behaviour with Object.intValue()

不想你离开。 提交于 2019-12-01 14:46:07
I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int ? Why does it not work in the top code snippet, but it works in the bottom code snippet below the line? The only difference seems to be adding an extra variable, which is also typed as a double ? //Converting double to int using helper //This doesn't work- gets error message //Cannot invoke intValue() on the primitive type double double doublehelpermethod = 123.65; double doubleObj = new Double( doublehelpermethod); System.out.println("The double

Why doesn't my primitive-type-argumented method override the wrapper-type-argumented super class method?

自古美人都是妖i 提交于 2019-12-01 14:39:25
问题 public class WrapperClasses{ void overloadedMethod(Number N){ System.out.println("Number Class Type"); } void overloadedMethod(Double D){ System.out.println("Double Wrapper Class Type"); } void overloadedMethod(Long L){ System.out.println("Long Wrapper Class Type"); } public static void main(String[] args){ int i = 21; WrapperClasses wr = new WrapperClasses(); //wr.overloadedMethod(i); } } class mine extends WrapperClasses{ void overloadedMethod(int N){ System.out.println("Integer Class Type"

Strange behaviour with Object.intValue()

南笙酒味 提交于 2019-12-01 13:08:57
问题 I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int ? Why does it not work in the top code snippet, but it works in the bottom code snippet below the line? The only difference seems to be adding an extra variable, which is also typed as a double ? //Converting double to int using helper //This doesn't work- gets error message //Cannot invoke intValue() on the primitive type double double

Userland autoboxing?

。_饼干妹妹 提交于 2019-12-01 03:24:27
问题 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-feature only? 回答1: No, java does not support operator overloading (http://en.wikipedia.org/wiki/Operator_overloading). Autoboxing is a compiler feature and not

In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?

梦想的初衷 提交于 2019-12-01 02:09:58
While working with the idea of overriding and overridden methods in Java I have noticed that there is some flexiblity given for the return types of such methods. Here is a bit of theory: "The return type of an overriding method in the derived class can be the same, or a subclass of the return type of the overridden method in the base class. The return type of such an overriding method is known as a covariant return type." Example below supposes that B extends A. Method in A: public Object some_method() {....} Method in B: public Integer some_method() {....} So, we see that some_method() in B

Unboxing issues

醉酒当歌 提交于 2019-11-30 23:22:53
I have a class that extends the LinkedList class. Here's an excerpt of the code: class SortedList<Integer> extends LinkedList<Integer> { int intMethod(Integer integerObject){ return integerObject; } } This is expected to return the auto-unboxed int value. But for some reason, the compiler throws an error that says that the types are incompatible and that the required type was int and the type found was Integer. This works in a different class perfectly fine! What gives? :( This is because you have <Integer> after SortedList . Usually you use T for type parameters: class SortedList<T> , but you

Why can Integer and int be used interchangably?

£可爱£侵袭症+ 提交于 2019-11-30 22:32:36
问题 I am confused as to why Integer and int can be used interchangeably in Java even though one is a primitive type and the other is an object? For example: Integer b = 42; int a = b; Or int d = 12; Integer c = d; 回答1: The first few sentences of the posted article describe it pretty well: You can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the

In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?

早过忘川 提交于 2019-11-30 20:46:50
问题 While working with the idea of overriding and overridden methods in Java I have noticed that there is some flexiblity given for the return types of such methods. Here is a bit of theory: "The return type of an overriding method in the derived class can be the same, or a subclass of the return type of the overridden method in the base class. The return type of such an overriding method is known as a covariant return type." Example below supposes that B extends A. Method in A: public Object

Do autoboxing and unboxing behave differently in Java and C#

ⅰ亾dé卋堺 提交于 2019-11-30 19:14:19
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); // legal, C# Double dd = 2.3f; // legal, C# list1.Add(dd); // legal, C# List<Double> list2 = new List<Double>(); // legal, C# double d1 = 3.0; list2.Add(d1); // legal, C# list2.Add(2.0); // legal, C# double d2 = list2.get(0); // legal, C# but in Java only some are allowed List<double> list1 = new ArrayList<double>()

Unboxing issues

我与影子孤独终老i 提交于 2019-11-30 18:52:19
问题 I have a class that extends the LinkedList class. Here's an excerpt of the code: class SortedList<Integer> extends LinkedList<Integer> { int intMethod(Integer integerObject){ return integerObject; } } This is expected to return the auto-unboxed int value. But for some reason, the compiler throws an error that says that the types are incompatible and that the required type was int and the type found was Integer. This works in a different class perfectly fine! What gives? :( 回答1: This is