primitive

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

北城以北 提交于 2019-11-29 10:33:07
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 behaviour. Still not exactly clear why JLS does not support "A narrowing primitive conversion followed by a

Primitive vs Object type in Java [duplicate]

扶醉桌前 提交于 2019-11-29 09:57:58
This question already has an answer here: Why do people still use primitive types in Java? 20 answers 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 behavior. why still Java use primitives under these condition? As my opinion, if Java only use Object type Autoboxing and

What is the complexity of std::vector<T>::clear() when T is a primitive type?

橙三吉。 提交于 2019-11-29 09:17:59
I understand that the complexity of the clear() operation is linear in the size of the container, because the destructors must be called. But what about primitive types (and POD)? It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. If this is possible, is it also possible for std::unordered_map? dasblinkenlight It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. In general, the complexity of resizing a vector to zero is linear in the number of elements currently stored in the vector .

Detect duplicate values in primitive Java array

前提是你 提交于 2019-11-29 07:46:42
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? 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, increment that number (something like map.put(array[i], map.get(array[i]) + 1) . Otherwise, create a new

Overriding Default Primitive Type Handling in Json.Net

只愿长相守 提交于 2019-11-29 07:40:31
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 some having to inject double to decimal conversions in my code. I've looked into extending Newtonsoft

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

跟風遠走 提交于 2019-11-29 07:40:00
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. 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 means, contribute. First, the long array definition in the wsdl:types area: <xsd:complexType name="ArrayOf

Concat an integer to a String - use String literal or primitive from performance and memory point of view?

…衆ロ難τιáo~ 提交于 2019-11-29 06:56:02
Option 1: String newStr = someStr + 3 + "]"; Option 2: String newStr = someStr + "3" + "]"; Which option is better with regards to performance, memory and general practice? What are some recommended tools/ways I can use to measure memory usage of my code and its performance (besides measuring the start time and the end time and calculate the difference) The first will become: StringBuilder sb = new StringBuilder (String.valueOf (someStr)); sb.append (3); sb.append ("]"); String newStr = sb.toString (); the second will become: StringBuilder sb = new StringBuilder (String.valueOf (someStr)); sb

Are there primitive types in Ruby?

限于喜欢 提交于 2019-11-29 02:58:08
I'm a Java developer who is just starting to learn Ruby. Does Ruby have any primitive types? I can't seem to find a list of them. If not, why? A core principle of Ruby is that all data should be represented as objects. Other languages such as Smalltalk follow a similar paradigm. The benefit of this design is that it makes Ruby more elegant and easier to learn. The rules applying to objects are consistently applied to all of Ruby. For instance, when beginners are first learning Java, the difference between the primitive type int and the wrapper class Integer can be confusing. This confusion is

What is the use/purpose of primitive type classes?

♀尐吖头ヾ 提交于 2019-11-29 01:49:49
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, as indicated by the following returning false: int a = 3; int.class.isInstance(a); So why do they

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

别等时光非礼了梦想. 提交于 2019-11-29 01:42: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 within a certain range are cached by the JVM. One could rephrase the question: What is the maximum factor to