primitive

Why are there no byte or short literals in Java?

孤者浪人 提交于 2019-11-27 20:58:13
I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast? And if the answer is "Because there was no short literal in C", then why are there no short literals in C? This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're up late at night. Someone at some point made a design decision to make it possible to enter literals

Cast primitive type array into object array in java

◇◆丶佛笑我妖孽 提交于 2019-11-27 20:26:29
Why I cannot do this in java? Object[] o = (Object[])(new int[]{0,1,2,3.14,4}); I have a method that receives an object and then represents it as a string, but depending on his type (primitive, primitive wrapper, array, etc...). When I was creating a Unit test, I was passing an array as Object which is Ok, but when I perform cast of that object into Object[] I'm getting ClassCastException . This is only happening with primitive type arrays. Is there any way to avoid this behavior? If not, could someone explain what is the reason of this behavior on Java Virtual Machine. Any help is very

Are Java primitives immutable?

孤街浪徒 提交于 2019-11-27 19:18:53
If a method has a local variable i : int i = 10; and then I assign a new value: i = 11; Will this allocate a new memory location? Or just replace the original value? Does this mean that primitives are immutable? Will this allocate a new memory location? Or just replace the original value? Java does not really make any guarantees that variables will correspond to memory locations; for example, your method might be optimized in such a way that i is stored in a register — or might not even be stored at all, if the compiler can see that you never actually use its value, or if it can trace through

How do I draw lines using XNA?

拈花ヽ惹草 提交于 2019-11-27 19:15:46
I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted. Can someone show me, using code, the simplest XNA implementation of drawing one or two lines on to the screen? Perhaps with a brief explanation (including the boilerplate)? I'm not a games programmer and I have little XNA experience. My ultimate goal is to draw some lines onto the screen which I will eventually transform with rotations, etc (by hand). However, for this first step.. I need to simply draw the lines! I remember

What is the use/purpose of primitive type classes?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 16:11:22
问题 I recently learned that there are Class representations for the primitive types in the JVM. For example, int.class , double.class , and even a void.class . What I don't understand is why these are there. They don't seem to serve any functional role. Using reflection, I searched through the classes, and they have no constructors, no methods, and no fields. For all intents and purposes, they seem empty and useless. The primitive type variables are not even instances of their respective classes,

What is the storage cost for a boxed primitive in Java?

懵懂的女人 提交于 2019-11-27 16:04:02
问题 How large, in bytes, is a boxed primitive like java.lang.Integer or java.lang.Character in Java? An int is 4 bytes, a typical pointer is also 4 byte (if not compressed by the JVM). Is the cost for an Integer (without caching) thus 4 bytes + 4 bytes = 8 bytes ? Are there any more hidden fields within the box-object or additional overhead incurred regarding objects (i.e. is there a general cost for objects that I'm not aware of?). I'm not interested in caching issues. I know that Integers

how to create and allocate a C buffer as part of an objective C class

ぃ、小莉子 提交于 2019-11-27 15:57:51
best to explain with an example: in my AudioItem.h #define ITEM_CAPACITY 100 typedef struct DataStruct { void * content; UInt32 size; } DataStruct; typedef DataStruct *DataStructRef; @interface AudioItem : NSObject { DataStructRef data; } @property (assign, readwrite) DataStructRef data; in AudioItem.m @synthesize data; -(id)initWithID:(NSString *)itemID { self = [super init]; data->content = malloc(ITEM_CAPACITY); return self; } The above code looks a lot like this one, but I get a BAD_EXEC_ERROR.. how come? The reason why I would like to use a C buffer rather than some NSMutableData or

Is String a primitive or an Object in Android or Java?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 13:45:47
问题 In the Android API http://developer.android.com/guide/topics/data/data-storage.html#pref It says: Shared Preference allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. Is String a primitive data type or an Object? 回答1: As far as Java programming language is considered, A primitive type is predefined by the language and is named by a reserved keyword. In

When should I use primitives instead of wrapping objects?

情到浓时终转凉″ 提交于 2019-11-27 13:04:13
问题 Actually here is a similar topic with little practical value. As far as I understand, primitives perform better and should be used everywhere except for the cases where Object-related features (e.g. null check) are needed. Right? 回答1: Do not forget that, since creating a new wrapper for every boxing occurrence is quite expensive, especially considering it usually being used at a single scope of a method, Autoboxing uses a pool of common wrappers. This is in fact an implementation of the

what does “>>>” mean in java? [duplicate]

六眼飞鱼酱① 提交于 2019-11-27 12:47:23
问题 This question already has an answer here: Difference between >>> and >> 7 answers Java >>> Operator [duplicate] 3 answers I found this code to find duplicates in SO post here. but I dont understand what this line means int mid = (low + high) >>> 1; private static int findDuplicate(int[] array) { int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) >>> 1; System.out.println(mid); int midVal = array[mid]; if (midVal == mid) low = mid + 1; else high = mid - 1; }