primitive

Direct Initialization vs Copy Initialization for Primitives

谁说胖子不能爱 提交于 2019-11-28 05:50:13
问题 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

object vs. primitive

放肆的年华 提交于 2019-11-28 04:33:52
Recently had an interviewer ask to define the difference between objects and primitives. Seemed like an odd question considering that all languages begin with a primitive. How would you have answered this question? I should also note that this interview was for a front-end development position so the language (I assume) he was referring to was JavaScript. A primitive is a data type that is composed of no other data types and can not be broken down any further. It is like the atoms in the programming scenario. I say atom because atom is a basic unit of matter and there is nothing that can be

autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

别等时光非礼了梦想. 提交于 2019-11-28 03:36:58
问题 Please consider 2 cases: //1 Short s = 10; //obviously compiles //2 takeShort(10); //error - int is not applicable //where: static void takeShort(Short s) {} I assume that case 1 is changed by compiler to : short _temp_s = 10; Short s = Short.valueOf(_temp_s); Could you please explain what compiler is trying to do in case 2, so it does not compile ? If it is not trying to apply autoboxing as it does in case 1, then why ? EDIT Reference to JSL in johnchen902 answer explains compiler's

Primitive vs Object type in Java [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-28 03:19:00
问题 This question already has answers here : Why do people still use primitive types in Java? (20 answers) Closed 6 years ago . This question came to my mind because I have read somewhere that Java is not a pure Object oriented language since it is using primitives (which are not objects). I can agree with that. Now my problem is why we are using primitives/wrappers while we already have Object in same type? As an example if we consider Integer , It has same value limit as int other than object

Detect duplicate values in primitive Java array

馋奶兔 提交于 2019-11-28 01:49:14
问题 I want to detect duplicate values in a Java array. For example: int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 }; How could I get the specific duplicated entry and how many times it occurs? 回答1: I'll have a Map<Integer, Integer> where the first integer is the value of the number that occurs in the array and the second integer is the count (number of occurrence). Run through the array.length in a loop for each item in the array, do a map.containsKey(array[i]) . If there exists a number in a map,

Overriding Default Primitive Type Handling in Json.Net

放肆的年华 提交于 2019-11-28 01:14:25
问题 Is there a way to override the default deserialization behaviour of Json.net when handling primitive types? For example when deserializing the json array [3.14,10,"test"] to a type of object[] 3.14 will be of type double and 10 will be of type long . Is there anyway I can intercept or override this type decision so I could deserialize the values as decimal and int respectively? I basically always want json integers to always return as int and floats to return as decimal . This will save me

Why won't .NET deserialize my primitive array from a web service?

被刻印的时光 ゝ 提交于 2019-11-28 01:07:16
问题 Help! I have an Axis web service that is being consumed by a C# application. Everything works great, except that arrays of long values always come across as [0,0,0,0] - the right length, but the values aren't deserialized. I have tried with other primitives (ints, doubles) and the same thing happens. What do I do? I don't want to change the semantics of my service. 回答1: Here's what I ended up with. I have never found another solution out there for this, so if you have something better, by all

Why does autoboxing in Java allow me to have 3 possible values for a boolean?

こ雲淡風輕ζ 提交于 2019-11-27 23:12:01
Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let's think about the fact that you'll get a NPE when trying to autounbox null. what this means is that you can't safely perform boolean operations on Booleans without null-checking or exception handling. same goes for doing math operations on an Integer. for a long time,

Are primitive data types in c# atomic (thread safe)?

依然范特西╮ 提交于 2019-11-27 23:07:01
For example, do I need to lock a bool value when multithreading? There is no such thing as an atomic type . Only operations can be atomic. Reading and writing a data type that fits into a single word ( int on a 32-bit processor, long on a 64-bit processor) is technically "atomic", but the jitter and/or processor can decide to reorder instructions and thus create unexpected race conditions, so you either need to serialize access with lock , use the Interlocked class for writes (and in some cases reads), or declare the variable volatile . The short answer is: If two different threads may access

Java storing two ints in a long

纵饮孤独 提交于 2019-11-27 21:06:11
I want to store two ints in a long (instead of having to create a new Point object every time). Currently, I tried this. It's not working, but I don't know what is wrong with it: // x and y are ints long l = x; l = (l << 32) | y; And I'm getting the int values like so: x = (int) l >> 32; y = (int) l & 0xffffffff; y is getting sign-extended in the first snippet, which would overwrite x with -1 whenever y < 0 . In the second snippet, the cast to int is done before the shift, so x actually gets the value of y . long l = (((long)x) << 32) | (y & 0xffffffffL); int x = (int)(l >> 32); int y = (int)l