boxing

Does autoboxing call valueOf()?

ⅰ亾dé卋堺 提交于 2019-11-28 07:55:20
I'm trying to determine whether the following statements are guaranteed to be true: ((Boolean)true) == Boolean.TRUE ((Boolean)true) == Boolean.valueOf(true) ((Integer)1) == Integer.valueOf(1) I've always assumed that autoboxing was equivalent to calling valueOf() on the corresponding type. Every discussion that I've seen on the topic seems to support my assumption. But all I could find in the JLS was the following ( §5.1.7 ): If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character

Java signed zero and boxing

时光毁灭记忆、已成空白 提交于 2019-11-28 07:13:52
Lately I've written a project in Java and noticed a very strange feature with double/Double implementation. The double type in Java has two 0's, i.e. 0.0 and -0.0 (signed zero's). The strange thing is that: 0.0 == -0.0 evaluates to true , but: new Double(0.0).equals(new Double(-0.0)) evaluates to false . Does anyone know the reason behind this? It is all explained in the javadoc : Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if d1.doubleValue() == d2.doubleValue() also has the value true. However, there are two exceptions

Will Boxing and Unboxing happen in Array?

梦想与她 提交于 2019-11-28 07:06:33
问题 I'm new to programming, As per MSDN, Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. I knew We can store any objects in an arraylist, because system.object is a base for all all types. Boxing and unboxing happens in

Does the VB.NET “If” operator cause boxing?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 07:00:07
问题 Those of us who've worked in VB/VB.NET have seen code similar to this abomination: Dim name As String = IIf(obj Is Nothing, "", obj.Name) I say "abomination" for three simple reasons: IIf is a function , all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#. Because IIf is a function, it thus incurs the

Boxing and unboxing with generics

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 03:32:26
The .NET 1.0 way of creating collection of integers (for example) was: ArrayList list = new ArrayList(); list.Add(i); /* boxing */ int j = (int)list[0]; /* unboxing */ The penalty of using this is the lack of type safety and performance due to boxing and unboxing. The .NET 2.0 way is to use generics: List<int> list = new List<int>(); list.Add(i); int j = list[0]; The price of boxing (to my understanding) is the need to create an object on the heap, copy the stack allocated integer to the new object and vice-versa for unboxing. How does the use of generics overcome this? Does the stack

C# non-boxing conversion of generic enum to int?

时光毁灭记忆、已成空白 提交于 2019-11-28 03:03:47
Given a generic parameter TEnum which always will be an enum type, is there any way to cast from TEnum to int without boxing/unboxing? See this example code. This will box/unbox the value unnecessarily. private int Foo<TEnum>(TEnum value) where TEnum : struct // C# does not allow enum constraint { return (int) (ValueType) value; } The above C# is release-mode compiled to the following IL (note boxing and unboxing opcodes): .method public hidebysig instance int32 Foo<valuetype .ctor ([mscorlib]System.ValueType) TEnum>(!!TEnum 'value') cil managed { .maxstack 8 IL_0000: ldarg.1 IL_0001: box !

Does int.class equal Integer.class or Integer.TYPE in Java?

痴心易碎 提交于 2019-11-27 19:45:26
Let's imagine one retrieves the declaring type of a Field using reflection. Which of the following tests will correctly indicate whether one is dealing with an int or an Integer ? Field f = ... Class<?> c = f.getDeclaringClass(); boolean isInteger; isInteger = c.equals(Integer.class); isInteger = c.equals(Integer.TYPE); isInteger = c.equals(int.class); isInteger = ( c == Integer.class); isInteger = ( c == Integer.TYPE); isInteger = ( c == int.class); Based on Field.getType() (instead of f.getDeclaringClass() ), I get the following: Type: java.lang.Integer equals(Integer.class): true equals(int

Does calling a method on a value type result in boxing in .NET?

[亡魂溺海] 提交于 2019-11-27 19:23:11
I was just participating in Stack Overflow question Is everything in .NET an object? . And one poster (in comments of accepted answer) seemed to think that performing a method call on a value type resulted in boxing. He pointed me to Boxing and Unboxing (C# Programming Guide) which doesn't exactly specify the use case we're describing. I'm not one to trust a single source, so I just wanted to get further feedback on the question. My intuition is that there is no boxing but my intuition does suck. :D To further elaborate: The example I used was: int x = 5; string s = x.ToString(); // Boxing??

When does a using-statement box its argument, when it's a struct?

本秂侑毒 提交于 2019-11-27 18:33:22
问题 I have some questions about the following code: using System; namespace ConsoleApplication2 { public struct Disposable : IDisposable { public void Dispose() { } } class Program { static void Main(string[] args) { using (Test()) { } } static Disposable Test() { return new Disposable(); } } } My questions are: Will the using-statement that operates on the Disposable struct, returned from Test() box the struct, or not? How can I find the answer myself? To try to find out myself, I inspected the

how to convert byte[] to Byte[], and the other way around?

拜拜、爱过 提交于 2019-11-27 17:56:47
How to convert byte[] to Byte[], and also Byte[] to byte[], in the case of not using any 3rd party library? Is there a way to do it fast just using the standard library? Juvanis Byte class is a wrapper for the primitive byte . This should do the work: byte[] bytes = new byte[10]; Byte[] byteObjects = new Byte[bytes.length]; int i=0; // Associating Byte array values with bytes. (byte[] to Byte[]) for(byte b: bytes) byteObjects[i++] = b; // Autoboxing. .... int j=0; // Unboxing Byte values. (Byte[] to byte[]) for(Byte b: byteObjects) bytes[j++] = b.byteValue(); byte[] to Byte[] : byte[] bytes =