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
40
votes
6 answers

Objective C Boolean Array

I need to utilize an array of booleans in objective-c. I've got it mostly set up, but the compiler throws a warning at the following statement: [updated_users replaceObjectAtIndex:index withObject:YES]; This is, I'm sure, because YES is simply not…
Allyn
  • 20,271
  • 16
  • 57
  • 68
39
votes
2 answers

Is there ever a good time to use int32 instead of sint32 in Google Protocol Buffers?

I've been reading up on Google Protocol Buffers recently, which allows for a variety of scalar value types to be used in messages. According to their documentation, there's three types of variable-length integer primitives - int32, uint32, and…
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
38
votes
3 answers

Java storing two ints in a long

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…
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
37
votes
8 answers

Code duplication caused by primitive types: How to avoid insanity?

In one of my Java projects I am plagued by code repetition due to the way Java handles (not) primitives. After having to manually copy the same change to four different locations (int, long, float, double) again, for the third time, again and again…
thkala
  • 84,049
  • 23
  • 157
  • 201
37
votes
4 answers

Hashcode of an int

What is the hashcode of a primitive type, such as int? for example, let's say num was an interger. int hasCode = 0; if (num != 0) { hasCode = hasCode + num.hashCode(); }
Ed Lee
  • 387
  • 2
  • 7
  • 12
34
votes
6 answers

Using int as a type parameter for java.util.Dictionary

When I try to declare a Dictionary as such: private Dictionary map; The compiler gives me the following error: Syntax error on token "int", Dimensions expected after this token But it works fine with Integer. I'm vaguely aware that…
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
33
votes
5 answers

object vs. primitive

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…
Zach Shallbetter
  • 1,901
  • 6
  • 23
  • 37
32
votes
6 answers

Passing primitives to an OCMock's stub

I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY: method, and a Render class using HeightMap. I'm trying to unit test Render using some HeightMap mocks. This works: id…
Eduardo Costa
  • 1,974
  • 1
  • 16
  • 22
31
votes
3 answers

Async function returning promise, instead of value

I'm trying to understand how async/await works in conjunction together with promises. async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof…
user3223162
  • 413
  • 1
  • 4
  • 6
31
votes
3 answers

Is this really widening vs autoboxing?

I saw this in an answer to another question, in reference to shortcomings of the Java spec: There are more shortcomings and this is a subtle topic. Check this out: public class methodOverloading{ public static void hello(Integer x){ …
Andy
  • 455
  • 6
  • 12
30
votes
5 answers

Map alternative for primitive values

I did some profiling on my application and one of the results turned out that about 18% of memory on the heap is used by objects of type Double. It turns out these objects are the values in Maps, where I cannot use the primitive type. My reasoning…
hotzst
  • 7,238
  • 9
  • 41
  • 64
30
votes
10 answers

Is an int a 64-bit integer in 64-bit C#?

In my C# source code I may have declared integers as: int i = 5; or Int32 i = 5; In the currently prevalent 32-bit world they are equivalent. However, as we move into a 64-bit world, am I correct in saying that the following will become the…
Guy
  • 65,082
  • 97
  • 254
  • 325
30
votes
5 answers

Java: Why am I required to initialize a primitive local variable?

public class Foo { public static void main(String[] args) { float f; System.out.println(f); } } The print statement causes the following compile-time error, The local variable f may not have been initialized If primitives…
user1329572
  • 6,176
  • 5
  • 29
  • 39
28
votes
3 answers

How does double to int cast work in Java

I am new to Java, and wondering how does double to int cast work ? I understand that it's simple for long to int by taking the low 32 bits, but what about double (64 bits) to int (32 bits) ? those 64 bits from double in binary is in Double-precision…
peter
  • 8,333
  • 17
  • 71
  • 94
27
votes
8 answers

What is the third boolean state in java?

While I know that by definition a boolean consists of only two states, true or false. I was wondering what value does a boolean have before it is initialized with one of these states.
Bobby
  • 18,217
  • 15
  • 74
  • 89
1 2
3
65 66