primitive

Is primitive assigned a memory address?

和自甴很熟 提交于 2019-12-01 03:17:16
问题 I am trying to understand the process of declaration and assignment of a primitive type at the back stage. int i; i = 3; For 1), on the memory stack, it assigns a space for storing an int type value named i For 2), it assigns the value 3 to the space preserved above Is there a memory address there? From my impression, memory address is always associated with the objects on the heap? Update: Regarding the replies: So, for every variable on the stack, they are all assigned a memory address as

Is the performance/memory benefit of short nullified by downcasting?

雨燕双飞 提交于 2019-12-01 02:28:18
问题 I'm writing a large scale application where I'm trying to conserve as much memory as possible as well as boost performance. As such, when I have a field that I know is only going to have values from 0 - 10 or from -100 - 100, I try to use the short data type instead of int . What this means for the rest of the code, though, is that all over the place when I call these functions, I have to downcast simple int s into short s. For example: Method Signature public void coordinates(short x, short

why are there Primitive datatype in Java? [duplicate]

爱⌒轻易说出口 提交于 2019-11-30 22:30:09
Possible Duplicate: When we have wrappers classes, why primitives are supported? If there are Wrapper classes which make Java pure object-oriented language, then why are there Primitive datatypes which can be used in Java??? For efficiency. Variables of primitive types contain the value directly; variables of non-primitive types are references, referring to an object stored somewhere else in memory. Each time you need to use the value of a wrapper type, the JVM needs to lookup the object in memory to get at the value. This isn't needed for primitive types, because the variable contains the

Why ambiguous error when using varargs overloading with primitive type and wrapper class? [duplicate]

空扰寡人 提交于 2019-11-30 20:03:35
This question already has an answer here: Ambiguous varargs methods 4 answers I do not understand why here in case 1, it is not giving compilation error, contrary in case 2 (varargs), it gives compilation error. Can anyone please elaborate what differences the compiler makes in these two cases? I went through many posts about it, but not able to understand it yet. Case #1 public class Test { public void display(int a) { System.out.println("1"); } public void display(Integer a) { System.out.println("2"); } public static void main(String[] args) { new Test().display(0); } } The Output is: 1 Case

Declaring, Properties, Synthesizing, and Implementing int[] array in Objective C

限于喜欢 提交于 2019-11-30 15:15:32
问题 How do you declare, set properties, synthesize, and implement an int array of size 5 in Objective C? I'm writing this code for an iphone app. Thanks. 回答1: I think the "Cocoa-y" thing to do is hide the int array even if you use it internally. Something like: @interface Lottery : NSObject { int numbers[5]; } - (int)numberAtIndex:(int)index; - (void)setNumber:(int)number atIndex:(int)index; @end @implementation Lottery - (int)numberAtIndex:(int)index { if (index > 4) [[NSException

Declaring, Properties, Synthesizing, and Implementing int[] array in Objective C

£可爱£侵袭症+ 提交于 2019-11-30 13:51:43
How do you declare, set properties, synthesize, and implement an int array of size 5 in Objective C? I'm writing this code for an iphone app. Thanks. I think the "Cocoa-y" thing to do is hide the int array even if you use it internally. Something like: @interface Lottery : NSObject { int numbers[5]; } - (int)numberAtIndex:(int)index; - (void)setNumber:(int)number atIndex:(int)index; @end @implementation Lottery - (int)numberAtIndex:(int)index { if (index > 4) [[NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"Index %d is out of range", index] userInfo:nil]

One-byte bool. Why?

会有一股神秘感。 提交于 2019-11-30 11:47:09
问题 In C++, why does a bool require one byte to store true or false where just one bit is enough for that, like 0 for false and 1 for true? (Why does Java also require one byte?) Secondly, how much safer is it to use the following? struct Bool { bool trueOrFalse : 1; }; Thirdly, even if it is safe, is the above field technique really going to help? Since I have heard that we save space there, but still compiler generated code to access them is bigger and slower than the code generated to access

Is int (Int32) considered an object in .NET or a primitive (not int?)?

百般思念 提交于 2019-11-30 09:50:53
Is int (aka Int32 ) an object , or a primitive in .NET (I'm not asking regarding int? )? I hit F12 on the saved word int and got : public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int> { ... } It doesn't inherit from Object , does it mean that int is a primitive ? Everything in C# inherits from object , including int . From msdn : Int32 is an immutable value type that represents signed integers and Both reference and value types are derived from the ultimate base class Object. Referring to this MSDN site there are 15 build in types, from which 2 are

Correct way of thinking about primitive assignment

北城余情 提交于 2019-11-30 09:24:49
问题 In this example, int x = 5; int y = x; x = 4; y will remain 5 because x is just being reassigned and it is not manipulating the object it used to refer to in anyway. My question is, is what I just said a correct way of thinking about it? Or is there a duplication of the memory stored in 'x' and that duplication is put in 'y'. 回答1: Primitives, unlike objects, are stored directly inside the variable. That is, a variable of primitive type is not storing a reference to a primitive, it stores the

Behind the scenes, what's happening with decimal value type in C#/.NET?

非 Y 不嫁゛ 提交于 2019-11-30 08:14:28
How is the decimal type implemented? Update It's a 128-bit value type (16 bytes) 1 sign bit 96 bits (12 bytes) for the mantissa 8 bits for the exponent remaining bits (23 of them!) set to 0 Thanks! I'm gonna stick with using a 64-bit long with my own implied scale. Decimal Floating Point article on Wikipedia with specific link to this article about System.Decimal . A decimal is stored in 128 bits, even though only 102 are strictly necessary. It is convenient to consider the decimal as three 32-bit integers representing the mantissa, and then one integer representing the sign and exponent. The