unboxing

What is the difference between boxing/unboxing and type casting?

烂漫一生 提交于 2019-11-26 10:19:34
问题 What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably. 回答1: Boxing refers to a conversion of a non-nullable-value type into a reference type or the conversion of a value type to some interface that it implements (say int to IComparable<int> ). Further, the conversion of an underlying value type to a nullable type is also a boxing conversion. (Caveat: Most discussions of this subject will ignore the latter two types of conversions.)

Comparing boxed value types

ぐ巨炮叔叔 提交于 2019-11-26 08:26:56
问题 Today I stumbled upon an interesting bug I wrote. I have a set of properties which can be set through a general setter. These properties can be value types or reference types. public void SetValue( TEnum property, object value ) { if ( _properties[ property ] != value ) { // Only come here when the new value is different. } } When writing a unit test for this method I found out the condition is always true for value types. It didn\'t take me long to figure out this is due to boxing/unboxing.

Performance surprise with “as” and nullable types

跟風遠走 提交于 2019-11-26 02:33:57
I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I thought this was really neat, and that it could improve performance over the C# 1 equivalent, using "is" followed by a cast - after all, this way we only need to ask for dynamic type checking once, and then a simple value check. This appears not to be the case, however. I've included a sample test app below, which basically sums all the integers within

How java auto boxing/unboxing works?

感情迁移 提交于 2019-11-26 01:59:54
问题 Since JDK 5.0, auto boxing/unboxing was introduced in java, the trick is simple and helpful, but when i started testing different conversions between wrapper classes and primitive types, i get really confused how the concept of auto boxing works in java, for example: Boxing int intValue = 0; Integer intObject = intValue; byte byteValue = 0; intObject = byteValue; // ==> Error After trying different cases ( short , long , float , double ), the only case which is accepted by the compiler is

Performance surprise with “as” and nullable types

感情迁移 提交于 2019-11-26 01:11:14
问题 I\'m just revising chapter 4 of C# in Depth which deals with nullable types, and I\'m adding a section about using the \"as\" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I thought this was really neat, and that it could improve performance over the C# 1 equivalent, using \"is\" followed by a cast - after all, this way we only need to ask for dynamic type checking once, and then a simple value check. This appears not

Integer wrapper class and == operator - where is behavior specified? [duplicate]

和自甴很熟 提交于 2019-11-26 01:01:58
问题 This question already has answers here : Integer wrapper objects share the same instances only within the value 127? [duplicate] (5 answers) Closed 5 years ago . Integer integer1 = 127; Integer integer2 = 127; System.out.println(integer1 == integer2);//true integer1 = 128; integer2 = 128; System.out.println(integer1 == integer2);//false I found it returns == (if it is) under the range of -128 - 127 , why is there such specification ? 回答1: Because of this code in Integer.valueOf(int): public

Integer wrapper class and == operator - where is behavior specified? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-25 22:46:39
This question already has an answer here: Integer wrapper objects share the same instances only within the value 127? [duplicate] 5 answers Integer integer1 = 127; Integer integer2 = 127; System.out.println(integer1 == integer2);//true integer1 = 128; integer2 = 128; System.out.println(integer1 == integer2);//false I found it returns == (if it is) under the range of -128 - 127 , why is there such specification ? Because of this code in Integer.valueOf(int) : public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new

What is boxing and unboxing and what are the trade offs?

与世无争的帅哥 提交于 2019-11-25 20:34:06
I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. Boxed values are data structures that are minimal wrappers around primitive types *. Boxed values are typically stored as pointers to objects on the heap . Thus, boxed values use more memory and take at minimum two memory lookups to access: once to get the pointer, and another to follow that pointer to the primitive. Obviously this isn't the kind of thing you want in your inner loops. On the other hand, boxed values typically play better with other types in the