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

Converting characters to integers in Java

Can someone please explain to me what is going on here: char c = '+'; int i = (int)c; System.out.println("i: " + i + " ch: " + Character.getNumericValue(c)); This prints i: 43 ch:-1. Does that mean I have to rely on primitive conversions to convert…
JRR
  • 6,014
  • 6
  • 39
  • 59
69
votes
3 answers

what does ">>>" mean in java?

I found this code to find duplicates in SO post here. but I dont understand what this line means int mid = (low + high) >>> 1; private static int findDuplicate(int[] array) { int low = 0; int high = array.length - 1; while…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
69
votes
10 answers

Using int vs Integer

I came across a class using Integer variables to capture size to be used in a for loop. Is this good practice or should we use the int primitive data type? Integer size = something.getFields().size(); for (Integer j = 0; j < size - 1; ++j)
developer
  • 9,116
  • 29
  • 91
  • 150
59
votes
6 answers

Are Java primitives immutable?

If a method has a local variable i: int i = 10; and then I assign a new value: i = 11; Will this allocate a new memory location? Or just replace the original value? Does this mean that primitives are immutable?
fYre
  • 1,212
  • 3
  • 11
  • 16
58
votes
4 answers

Check type of primitive field

I'm trying to determine the type of a field on an object. I don't know the type of the object when it is passed to me but I need to find fields which are longs. It is easy enough to distinguish the boxed Longs but the primitive long seems more…
macbutch
  • 3,241
  • 2
  • 27
  • 27
58
votes
7 answers

Is an array a primitive type or an object (or something else entirely)?

The question is basically self-explanatory. I haven't been able to find an API for arrays (other than this Arrays, but this just defines a bunch of static helper functions for dealing with actual arrays). If there is no class for it, this seems to…
asteri
  • 11,402
  • 13
  • 60
  • 84
54
votes
4 answers

Why are there no byte or short literals in Java?

I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast? And if the answer is "Because there was no short literal in C", then why are…
Will Wagner
  • 4,128
  • 3
  • 22
  • 14
54
votes
8 answers

Dynamically find the class that represents a primitive Java type

I need to make some reflective method calls in Java. Those calls will include methods that have arguments that are primitive types (int, double, etc.). The way to specify such types when looking up the method reflectively is int.class, double.class,…
Mike Furtak
  • 1,105
  • 1
  • 11
  • 17
45
votes
10 answers

How do I draw lines using XNA?

I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted. Can someone show me, using code, the simplest XNA implementation of drawing…
mmcdole
  • 91,488
  • 60
  • 186
  • 222
45
votes
5 answers

Integer.class vs int.class

What is the difference between Integer.class, Integer.TYPE and int.class? acc to me Integer.class is a reference of Integer (Wrapper) Class object but what is then int.class as int is not a class, it's a primitive type. And what does Integer.TYPE…
user3380123
  • 703
  • 1
  • 6
  • 14
44
votes
11 answers

Cast primitive type array into object array in java

Why I cannot do this in java? Object[] o = (Object[])(new int[]{0,1,2,3.14,4}); I have a method that receives an object and then represents it as a string, but depending on his type (primitive, primitive wrapper, array, etc...). When I was creating…
aumanets
  • 3,703
  • 8
  • 39
  • 59
44
votes
5 answers

Fastest way to check if a byte array is all zeros

I have a byte[4096] and was wondering what the fastest way is to check if all values are zero? Is there any way faster than doing: byte[] b = new byte[4096]; b[4095] = 1; for(int i=0;i
PureGero
  • 937
  • 2
  • 8
  • 16
44
votes
5 answers

One-byte bool. Why?

In C++, why does a bool require one byte to store true or false where just one bit is enough for that, like 0 for false and 1 for true? (Why does Java also require one byte?) Secondly, how much safer is it to use the following? struct Bool { …
Sam
  • 1,842
  • 3
  • 19
  • 33
43
votes
6 answers

Do uninitialized primitive instance variables use memory?

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…
WVrock
  • 1,725
  • 3
  • 22
  • 30
40
votes
4 answers

Are primitive datatypes thread-safe in Java

Are the primitive data types like int & short thread-safe in Java? I have executed the following code and couldn't see expected result 500 some times. public class SampleThree extends Thread { static long wakeUpTime = System.currentTimeMillis()…
krishna
  • 807
  • 2
  • 11
  • 19
1
2
3
65 66