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
361
votes
10 answers

Why is typeof null "object"?

I'm reading 'Professional Javascript for Web Developers' Chapter 4 and it tells me that the five types of primitives are: undefined, null, boolean, number and string. If null is a primitive, why does typeof(null) return "object"? Wouldn't that mean…
thetrystero
  • 5,622
  • 5
  • 23
  • 34
288
votes
5 answers

Why don't Java Generics support primitive types?

Why do generics in Java work with classes but not with primitive types? For example, this works fine: List foo = new ArrayList(); but this is not allowed: List bar = new ArrayList();
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
275
votes
11 answers

Generic type conversion FROM string

I have a class that I want to use to store "properties" for another class. These properties simply have a name and a value. Ideally, what I would like is to be able to add typed properties, so that the "value" returned is always of the type that I…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
196
votes
7 answers

Java: Integer equals vs. ==

As of Java 1.5, you can pretty much interchange Integer with int in many situations. However, I found a potential defect in my code that surprised me a bit. The following code: Integer cdiCt = ...; Integer cdsCt = ...; ... if (cdiCt != null && cdsCt…
Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
183
votes
21 answers

Why do people still use primitive types in Java?

Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth. I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
119
votes
10 answers

Java equivalent of unsigned long long?

In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int, or via uint64_t. Now, in Java longs are 64 bits, I know. However, they are signed. Is there an unsigned long (long) available as a Java primitive? How do I use…
eleven81
  • 6,301
  • 11
  • 37
  • 48
92
votes
8 answers

In Java, is the result of the addition of two chars an int or a char?

When adding 'a' + 'b' it produces 195. Is the output datatype char or int?
orange
  • 5,297
  • 12
  • 50
  • 71
92
votes
3 answers

How to cast Object to boolean?

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.
Ravi Gupta
  • 4,468
  • 12
  • 54
  • 85
84
votes
6 answers

How many primitives does it take to build a LISP machine? Ten, seven or five?

On this site they say there are 10 LISP primitives. The primitives are: atom, quote, eq, car, cdr, cons, cond, lambda, label, apply. http://hyperpolyglot.wikidot.com/lisp#ten-primitives Stevey reckons there are seven (or five): Its part of the…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
84
votes
6 answers

How can you pass multiple primitive parameters to AsyncTask?

There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This…
robguinness
  • 16,266
  • 14
  • 55
  • 65
81
votes
9 answers

What's the difference between primitive and reference types?

This is a past exam question and I was wondering what a primitive type and reference type are first off? With an array I know that a reference type is where the array is composed of objects or variables, but a primitive type is where you would…
WestJackson
  • 1,067
  • 3
  • 11
  • 14
79
votes
11 answers

Primitive type 'short' - casting in Java

I have a question about the primitive type short in Java. I am using JDK 1.6. If I have the following: short a = 2; short b = 3; short c = a + b; the compiler does not want to compile - it says that it "cannot convert from int to short" and…
user42155
  • 48,965
  • 27
  • 59
  • 60
76
votes
11 answers

Which one to use, int or Integer

I need to create a data transfer object, which I will use for storing the records retrieved from database. In this data transfer object, I need to declare a numeric field. For that which one is better - int or Integer If I am defining the field as…
Veera
  • 32,532
  • 36
  • 98
  • 137
72
votes
9 answers

Generic type checking

Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.) Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn't fit the bill…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
70
votes
3 answers

Why doesn't Kotlin allow to use lateinit with primitive types?

In the Kotlin language we, by default, have to initialize each variable when it is introduced. To avoid this, the lateinit keyword can be used. Referring to a lateinit variable before it has been initialized results in a runtime exception. lateinit…
Kallja
  • 5,362
  • 3
  • 23
  • 33
1
2 3
65 66