boxing

How large is the Integer cache?

假装没事ソ 提交于 2019-11-26 16:41:41
Class Integer has cache, which caches the Integer values. So if I use method valueOf or inboxing the new value will not be instantiated, but get from the cache. I know that the default cache size is 127 but can be extended due to VM settings. My question is: how large is the default value of cache size in these settings and can I manipulate this value? Is this value depended on which VM I use (32 or 64 bits)? I'm now on tuning of a legacy code, and probably will need the conversion from int to Integer. Clarification: Following code I've found in Java source private static class IntegerCache {

.NET Tuple and Equals performance

被刻印的时光 ゝ 提交于 2019-11-26 16:13:39
问题 This is something I had not noticed until today. Apparently, the .NET implementation of the much used tuple classes ( Tuple<T> , Tuple<T1, T2> etc) causes boxing penalties for value types when equality based operations are performed. Here is how the class is kind of implemented in the framework (source via ILSpy): public class Tuple<T1, T2> : IStructuralEquatable { public T1 Item1 { get; private set; } public T2 Item2 { get; private set; } public Tuple(T1 item1, T2 item2) { this.Item1 = item1

Boxing and unboxing: when does it come up?

我们两清 提交于 2019-11-26 14:01:31
问题 So I understand what boxing and unboxing is. When's it come up in real-world code, or in what examples is it an issue? I can't imagine doing something like this example: int i = 123; object o = i; // Boxing int j = (int)o; // Unboxing ...but that's almost certainly extremely oversimplified and I might have even done boxing/unboxing without knowing it before. 回答1: It's much less of an issue now than it was prior to generics. Now, for example, we can use: List<int> x = new List<int>(); x.Add(10

Why doesn&#39;t delegate contravariance work with value types?

戏子无情 提交于 2019-11-26 13:59:28
问题 This snippet is not compiled in LINQPad. void Main() { (new[]{0,1,2,3}).Where(IsNull).Dump(); } static bool IsNull(object arg) { return arg == null; } The compiler's error message is: No overload for 'UserQuery.IsNull(object)' matches delegate 'System.Func' It works for a string array, but doesn't work for int[] . It's apparently related to boxing, but I want to know the details. 回答1: The answer given (that there is no variance involving value types) is correct. The reason covariance and

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.

In Java 8, is there a ByteStream class?

瘦欲@ 提交于 2019-11-26 06:45:23
问题 Java 8 provides Stream<T> specializations for double , int and long : DoubleStream , IntStream and LongStream respectively. However, I could not find an equivalent for byte in the documentation. Does Java 8 provide a ByteStream class? 回答1: No, it does not exist. Actually, it was explicitly not implemented so as not to clutter the Stream API with tons of classes for every primitive type. Quoting a mail from Brian Goetz in the OpenJDK mailing list: Short answer: no. It is not worth another 100K

Structs, Interfaces and Boxing [duplicate]

有些话、适合烂在心里 提交于 2019-11-26 04:25:19
问题 Possible Duplicate: Is it safe for structs to implement interfaces? Take this code: interface ISomeInterface { public int SomeProperty { get; } } struct SomeStruct : ISomeInterface { int someValue; public int SomeProperty { get { return someValue; } } public SomeStruct(int value) { someValue = value; } } and then I do this somewhere: ISomeInterface someVariable = new SomeStruct(2); is the SomeStruct boxed in this case? 回答1: Yes, it is. Basically whenever you need a reference and you've only

Why comparing Integer with int can throw NullPointerException in Java?

血红的双手。 提交于 2019-11-26 03:30:01
问题 It was very confusing to me to observe this situation: Integer i = null; String str = null; if (i == null) { //Nothing happens ... } if (str == null) { //Nothing happens } if (i == 0) { //NullPointerException ... } if (str == \"0\") { //Nothing happens ... } So, as I think boxing operation is executed first (i.e. java tries to extract int value from null ) and comparison operation has lower priority that\'s why the exception is thrown. The question is: why is it implemented in this way in

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