unboxing

How is it that an enum derives from System.Enum and is an integer at the same time?

烈酒焚心 提交于 2019-12-02 17:16:06
Edit : Comments at bottom. Also, this . Here's what's kind of confusing me. My understanding is that if I have an enum like this... enum Animal { Dog, Cat } ...what I've essentially done is defined a value type called Animal with two defined values, Dog and Cat . This type derives from the reference type System.Enum (something which value types can't normally do—at least not in C#—but which is permitted in this case), and has a facility for casting back and forth to/from int values. If the way I just described the enum type above were true, then I would expect the following code to throw an

boxing unboxing

夙愿已清 提交于 2019-12-02 15:21:25
问题 I found the following code snippet while searching about boxing and unboxing in C#. class TestBoxing { static void Main() { int i = 123; // Boxing copies the value of i into object o. object o = i; // Change the value of i. i = 456; // The change in i does not effect the value stored in o. System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); } } /* Output: The value-type value = 456 The object-type value = 123 */ Over here it

boxing unboxing

非 Y 不嫁゛ 提交于 2019-12-02 09:42:36
I found the following code snippet while searching about boxing and unboxing in C#. class TestBoxing { static void Main() { int i = 123; // Boxing copies the value of i into object o. object o = i; // Change the value of i. i = 456; // The change in i does not effect the value stored in o. System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); } } /* Output: The value-type value = 456 The object-type value = 123 */ Over here it says that even though he value of i changes the value of o remains the same.If so then o is referencing

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

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

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

How to unbox from object to type it contains, not knowing that type at compile time?

◇◆丶佛笑我妖孽 提交于 2019-11-30 10:53:21
At the run-time I get boxed instance of some type. How to unbox it to underlying type? Object obj; String variable = "Some text"; obj = variable // boxing; // explicit unboxing, because we know the type of variable at compile time. var x = (String)obj // Now let's pretend that we don't know the type of underlying object at compile time. Type desiredType = obj.GetType(); // But we can figure out. //And now the question. //How to express something like this: var y = (desiredType)obj; //Need to get unboxed instance of initial variable here; If you don't know the type at compile time , then you

Difference in behaviour of the ternary operator on JDK8 and JDK10

僤鯓⒐⒋嵵緔 提交于 2019-11-29 20:33:39
Consider the following code public class JDK10Test { public static void main(String[] args) { Double d = false ? 1.0 : new HashMap<String, Double>().get("1"); System.out.println(d); } } When running on JDK8, this code prints null whereas on JDK10 this code results in NullPointerException Exception in thread "main" java.lang.NullPointerException at JDK10Test.main(JDK10Test.java:5) The bytecode produced by the compilers is almost identical apart from two additional instructions produced by the JDK10 compiler which are related to autoboxing and seem to be responsible for the NPE. 15:

Android Studio: Unboxing of 'xxx' may produce 'java.lang.NullPointerException'

早过忘川 提交于 2019-11-29 20:18:40
问题 I'm following the Android book example: //Get the drink from the intent int drinkIdd = (Integer)getIntent().getExtras().get(EXTRA_DRINKID); Drink drink = Drink.drinks[drinkIdd]; And this project could be ran in Android Studio but with a yellow warning on line: int drinkIdd = (Integer)getIntent().getExtras().get(EXTRA_DRINKID); with: info: Unboxing of '(Integer)getIntent().getExtras().get(EXTRA_DRINKID)' may produce 'java.lang.NullPointerException' From my understanding, get(EXTRA_DRINKID)