primitive

Why can I assign an integer literal to a short type variable but not to a short type method parameter?

蹲街弑〆低调 提交于 2019-11-30 06:45:20
Why can I do this: short a = 5; But not this: void setNum(short a); setNum(5); It throws: Possible lossy conversion from int to short I understand that 5 is an integer literal and you have to cast it. I also understand that if the value is not a constant then is obvious that it needs to throw that error because maybe the value reaches the limit of a short type. But why if the compiler knows I'm passing a constant that a short can hold (as in the assignment) it doesn't let it compile? I mean, what is the difference between them? In order to understand why the assignment type-conversion works

How do I test if a primitive in Objective-C is nil?

情到浓时终转凉″ 提交于 2019-11-30 04:58:10
I'm doing a check in an iPhone application - int var; if (var != nil) It works, but in X-Code this is generating a warning "comparison between pointer and integer." How do I fix it? I come from the Java world, where I'm pretty sure the above statement would fail on compliation. Primitives can't be nil . nil is reserved for pointers to Objective-C objects. nil is technically a pointer type, and mixing pointers and integers will without a cast will almost always result in a compiler warning, with one exception: it's perfectly ok to implicitly convert the integer 0 to a pointer without a cast. If

Are primitive types different in Java and C#?

五迷三道 提交于 2019-11-30 04:57:34
I am manually converting code from Java to C# and struggling with (what I call) primitive types (see, e.g. Do autoboxing and unboxing behave differently in Java and C# ). From the answers I understand that double (C#) and Double (C#) are equivalent and double (C#) can also be used in containers, e.g. as a key in a Dictionary. However, double (Java) cannot be used in containers like HashMap which is why it is auto-boxed to Double (Java). Is double (C#) a primitive or an object? If it's a primitive what makes it behave differently from double (Java)? double (C#) cannot be set to null unless it

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

做~自己de王妃 提交于 2019-11-30 04:37:11
问题 This question already has answers here : Ambiguous varargs methods (4 answers) Closed 3 years ago . 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

One-byte bool. Why?

孤人 提交于 2019-11-30 01:14:48
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 the primitives. Oliver Charlesworth Why does a bool require one byte to store true or false where just

Generic type checking

心已入冬 提交于 2019-11-29 18:41:37
Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.) Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn't fit the bill for primitives (AFAIK) because they do not all have a common ground (apart from object before someone says! :P). So, my current thoughts are to just grit my teeth and do a big switch statement and throw an ArgumentException on failure.. EDIT 1: Just to clarify: The code definition should be like: public class MyClass<GenericType> .... And

Correct way of thinking about primitive assignment

﹥>﹥吖頭↗ 提交于 2019-11-29 15:48:12
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'. 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 primitive's value directly. When one primitive variable is assigned to another primitive variable, it copies

How comparison Object and primitive, with operator == works in Java? [duplicate]

早过忘川 提交于 2019-11-29 15:34:38
This question already has an answer here: When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done 3 answers For example: Long objectLong = 555l; long primitiveLong = 555l; System.out.println(objectLong == primitiveLong); // result is true. Is there invocation objectLong.longValue() method to compare Long to long or maybe some other way? As ever, the Java Language Specification is the appropriate resource to consult From JLS 15.21.1 ("Numerical Equality Operators == and !="): If the operands of an equality operator are both of numeric type, or one is of numeric

Is int an object in Java?

痞子三分冷 提交于 2019-11-29 13:27:06
More precisely, is int a part of the Integer class (a stripped down version or something) or is it something else entirely? I am aware that int is a value type and Integer a reference type, but does int inherit from Object anyway? (I am assuming that in this regard int, long, boolean etc are all similar. int was just chosen for convenience) The basic types in Java are not objects and does not inherit from Object. Since Java 1.5 introduced allowed auto boxing between int and Integer(and the other types). Because ints aren't Objects that can't be used as generic type parameters eg the T in list

Direct Initialization vs Copy Initialization for Primitives

徘徊边缘 提交于 2019-11-29 11:40:14
When initializing primitive types like int or pointers one can use either copy-initialization or direct-initialization . int a = 10; int b(10); Although the latter way is preffered for objects with constructors, I don't see people using it for primitives. I understand that it is kind of "more natural" (especially for numbers) to use the '=' operator but is there anybody writing things like in real-life code: for (int i(0); i < 5; ++i) { cout << i << endl; } Thanks. EDIT: The question asks about coding styles and best practices rather than technical implementation. Some people do this to be