primitive

Are primitive types different in Java and C#?

倖福魔咒の 提交于 2019-12-18 11:52:36
问题 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

why are there Primitive datatype in Java? [duplicate]

送分小仙女□ 提交于 2019-12-18 04:59:12
问题 This question already has answers here : Closed 6 years ago . 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??? 回答1: 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

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

微笑、不失礼 提交于 2019-12-18 04:44:14
问题 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) 回答1: The first will become: StringBuilder sb = new StringBuilder (String.valueOf (someStr)); sb.append (3); sb.append ("]"); String newStr =

java: boolean instanceOf Boolean?

强颜欢笑 提交于 2019-12-18 04:38:11
问题 I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so? public String test(Object value) { if (! (value instanceof Boolean) ) return "invalid"; if (((Boolean) value).booleanValue() == true ) return "yes"; if (((Boolean) value).booleanValue() == false ) return "no"; return "dunno"; } String result = test(true); // will result in "yes" 回答1:

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

有些话、适合烂在心里 提交于 2019-12-18 04:35:20
问题 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? 回答1: 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

How are the “primitive” types defined non-recursively?

若如初见. 提交于 2019-12-18 03:13:22
问题 Since a struct in C# consists of the bits of its members, you cannot have a value type T which includes any T fields: // Struct member 'T.m_field' of type 'T' causes a cycle in the struct layout struct T { T m_field; } My understanding is that an instance of the above type could never be instantiated*—any attempt to do so would result in an infinite loop of instantiation/allocation (which I guess would cause a stack overflow? ** )—or, alternately, another way of looking at it might be that

How are the “primitive” types defined non-recursively?

假如想象 提交于 2019-12-18 03:12:05
问题 Since a struct in C# consists of the bits of its members, you cannot have a value type T which includes any T fields: // Struct member 'T.m_field' of type 'T' causes a cycle in the struct layout struct T { T m_field; } My understanding is that an instance of the above type could never be instantiated*—any attempt to do so would result in an infinite loop of instantiation/allocation (which I guess would cause a stack overflow? ** )—or, alternately, another way of looking at it might be that

Why is this method overloading ambiguous?

≡放荡痞女 提交于 2019-12-17 23:16:31
问题 public class Primitive { void m(Number b, Number ... a) {} // widening, autoboxing->widening->varargs void m(byte b, Number ... a) {} // unboxing, autoboxing->widening->varargs public static void main(String[] args) { Byte b = 12; Primitive obj = new Primitive(); obj.m(b, 23); } } I have already searched and found that widening priority is higher than unboxing, so in above method invocation, first method should have been called because second parameter is same for both. But this does not

Do uninitialized primitive instance variables use memory?

血红的双手。 提交于 2019-12-17 22:56:06
问题 In Java, does it cost memory to declare a class level instance variable without initializing it? For example: Does int i; use any memory if I don't initialize it with i = 5; ? Details: I have a huge super-class that many different (not different enough to have their own super classes) sub-classes extend. Some sub-classes don't use every single primitive declared by the super-class. Can I simply keep such primitives as uninitialized and only initialize them in necessary sub-classes to save

How to cast Object to boolean?

给你一囗甜甜゛ 提交于 2019-12-17 21:57:39
问题 How can I cast a Java object into a boolean primitive I tried like below but it doesn't work boolean di = new Boolean(someObject).booleanValue(); The constructor Boolean(Object) is undefined Please advise. 回答1: If the object is actually a Boolean instance, then just cast it: boolean di = (Boolean) someObject; The explicit cast will do the conversion to Boolean , and then there's the auto-unboxing to the primitive value. Or you can do that explicitly: boolean di = ((Boolean) someObject)