Questions tagged [primitive]

A primitive type is a data type provided by a programming language as a basic building block.

The actual range of primitive data types that is available is dependent upon the specific programming language that is being used. For example, in C, strings are a composite but built-in data type, whereas in modern dialects of BASIC and in JavaScript, they are assimilated to a primitive data type that is both basic and built-in.

Classic basic primitive types may include:


  • (character, char)
  • (integer, int, short, long, byte) with a variety of precisions
  • number (float, double, real, double precision)
  • number (fixed) with a variety of precisions and a programmer-selected scale
  • , logical values (true, false)

Reference

977 questions
18
votes
3 answers

typeof in Java 8

If we want to check the datatype of variable in javascript, we can use typeof operator . Consider this snippet var c = 'str' ; console.log(typeof(c)); // string c = 123 ; console.log(typeof(c)); // number c = {} ; console.log(typeof(c)) ; //…
user8838577
17
votes
4 answers

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

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…
scravy
  • 11,904
  • 14
  • 72
  • 127
17
votes
6 answers

What is the use of passing const references to primitive types?

In a project I maintain, I see a lot of code like this for simple get/set methods const int & MyClass::getFoo() { return m_foo; } void MyClass::setFoo(const int & foo) { m_foo = foo; } What is the point in doing that instead of the following? int…
foraidt
  • 5,519
  • 5
  • 52
  • 80
17
votes
2 answers

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

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…
GabrielBB
  • 2,479
  • 1
  • 35
  • 49
17
votes
4 answers

java: boolean instanceOf Boolean?

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) { …
epegzz
  • 837
  • 2
  • 10
  • 21
17
votes
3 answers

Java Double vs double: class type vs primitive type

I was curious to what the performance differences between Java's class and primitive type for double were. So I created a little benchmark and found the class type to be 3x-7x slower than the primitive type. (3x on local machine OSX, 7x on…
Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74
16
votes
3 answers

IsPrimitive doesn't include nullable primitive values

I want to check if a Type is primitive or not and used the following code: return type.IsValueType && type.IsPrimitive; This works fine aslong as the primitive isnt nullable. For example int?, how can I check if the type is a nullable primitive…
Rand Random
  • 7,300
  • 10
  • 40
  • 88
16
votes
2 answers

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

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,…
user1487088
  • 251
  • 3
  • 6
15
votes
2 answers

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

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…
bpapa
  • 21,409
  • 25
  • 99
  • 147
15
votes
4 answers

Javascript Error: Cannot Convert Object to Primitive Value

I'm receiving this error using the following javascript code: function tempTest(evt) { alert(evt.currentTarget.id); ct = document.getElementById(evt.currentTarget.id); rslt = document.getElementById('rslt'); var props; for (var…
Eric
  • 2,061
  • 8
  • 28
  • 37
15
votes
3 answers

What is an int() Called?

It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo(): class Foo{ int _bar; }; So obviously int() is not a constructor. But what is it's name? In this…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
15
votes
4 answers

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

For example, do I need to lock a bool value when multithreading?
Benny
  • 8,547
  • 9
  • 60
  • 93
15
votes
5 answers

When should I use primitives instead of wrapping objects?

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?
yanchenko
  • 56,576
  • 33
  • 147
  • 165
15
votes
2 answers

Why == operator and equals() behave differently for values of AnyVal in Scala

In the scaladoc of scala.Any, the operator == (or, method ==) is explained: The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that) http://www.scala-lang.org/api/current/#scala.Any For objects of subclasses of…
Naetmul
  • 14,544
  • 8
  • 57
  • 81