Java Primitive Knowledge Sorts |



Java Developer Tutorials

You will have heard it stated earlier than that Java is a strongly typed programming language. That implies that each variable that’s outlined must be assigned an information kind, which determines what sort of knowledge it may possibly maintain all through program execution. In different phrases, as soon as outlined, a variable can not change kind…at the least not with out recompilation. With that in thoughts, you could wish to consider carefully earlier than assigning a variable’s kind, as selecting the improper form can have pretty critical repercussions.

This programming tutorial will record the entire primitive knowledge varieties supported by the Java language, in addition to present an outline of every, in order that builders might be higher outfitted to match variables to their applicable varieties.

Trying to be taught Java software program growth in a category or on-line course? We’ve got a listing of the Greatest On-line Programs to Be taught Java to assist get you began.

What are the Knowledge Sorts in Java?

Broadly talking, Java knowledge varieties will be categorised into two varieties: primitive and non-primitive.

Primitive knowledge varieties embody:

  • byte
  • brief
  • int
  • lengthy
  • float
  • double
  • boolean
  • char

Along with the values it could comprise, the information kind additionally determines the operations that could be carried out on it. Furthermore, primitive values don’t share state with different primitive values, and are, as such, fully discreet and autonomous. You may consider them because the constructing blocks for the non-primitive knowledge varieties.

In the meantime, non-primitive varieties are these which discuss with issues, and are thus termed reference varieties. As objects, non-primitive varieties can embody strategies that carry out actions. Strings, Arrays, Courses, and Interfaces are all examples of non-primitive varieties.

There are just a few different variations between primitive and non-primitive varieties, however we’ll cowl these a bit of in a while, as we go over the primitive knowledge varieties obtainable in Java in additional element.

Learn: What are Variables in Java?

Primitive Knowledge Sorts in Java

As we’ll see on this part, every primitive knowledge kind allocates a certain quantity of reminiscence and comes pre-initialized with its personal default worth.


The byte knowledge kind is a really small 8-bit signed integer. The time period “signed” implies that it may possibly symbolize each constructive and destructive numbers. It has a minimal worth of -128 and a most worth of 127, inclusively. Its small dimension makes it ultimate for utilizing in massive arrays, the place the reminiscence financial savings will be substantial. They will also be used rather than int, the place their strict dimension limits could assist to inform different builders {that a} variable is supposed to retailer pretty small numbers, like an individual’s age. Its default worth is 0.

Listed here are some examples of find out how to use byte knowledge varieties in Java:

byte b = 99;
byte b2;

The above code would add the worth 99 to the byte knowledge kind named b. The second line initialized a byte named b2, which might comprise the default worth of 0, since we didn’t assign it a worth.


With its 16-bit dimension, the brief knowledge kind suits neatly between a byte and int. It has a a lot bigger vary of -32,768 to 32,767 inclusive. It is usually nice for managing reminiscence and informing different builders in regards to the sorts of numbers it could comprise. Its default worth is 0.

Listed here are some examples of find out how to use the brief knowledge kind in Java:

brief s = 29999;
brief s2;


The int knowledge kind is an fascinating knowledge kind in that it may be both signed or unsigned. As a 32-bit signed integer (the default), it has a minimal worth of -231 and a most worth of 231-1, or -2,147,483,648 to 2,147,483,647. Declaring an int as an unsigned integer shifts its vary to the constructive facet of the size, giving it a minimal worth of 0 and a most worth of 232-1, or 4,294,967,295. Its default worth is 0.

Listed here are some examples of find out how to use the int knowledge kind in Java:

int a = 456789;
int b; 


The lengthy knowledge kind has much more storage capability than an int, requiring 64 bits. Just like the int, the lengthy kind will also be signed or unsigned. The signed lengthy has a minimal worth of -263 and a most worth of 263-1, or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. In the meantime, an unsigned 64-bit lengthy has a minimal worth of 0 and a most worth of 264-1, or a whopping 18,446,744,073,709,551,615! Its default worth is 0L.

Listed here are some examples of find out how to assign values to a lengthy knowledge kind in Java:

lengthy l = 1556667890L;
lengthy l_2;


The float knowledge kind is a 32-bit single precision floating level. Whereas massive sufficient to retailer factional numbers between 3.4e-038 and 3.4e+038 with as much as seven decimal digits, as soon as the worth will get previous six decimal factors, the quantity will develop into much less exact and extra of an estimate. Therefore, if you happen to require exact values, resembling for foreign money, you’ll need to make use of the java.math.BigDecimal class as a substitute. Its default worth is 0.0f.

Listed here are some examples displaying find out how to assign values to a float knowledge kind in Java:

float myFloat = 256.8f;
float myFloat2; 


The double knowledge kind is a double-precision 64-bit floating level, massive sufficient for storing fractional numbers between 1.7e-308 and 1.7e+308 as much as 15 decimal digits. It suffers from the identical limitation restrict as float does and may due to this fact solely be used if approximation errors are acceptable. Its default worth is 0.0d.

Listed here are some examples of find out how to assign values to a double knowledge kind in Java:

double myDouble = 256.7879837;
double myOtherDouble;

Learn: Java Instruments to Improve Productiveness


From the biggest to smallest knowledge kind, the boolean has solely two potential values: true and false. It shops its worth in a single bit. Nonetheless, for comfort, Java pads the worth and shops it in a single byte. One limitation of the boolean knowledge kind is that, whereas high-quality for easy flags that observe true/false situations, it doesn’t have an “unset” worth, which might lead one to erroneously conclude {that a} worth is false, when it’s actually not set. Its default worth is false.

Listed here are some examples of find out how to assign values to a boolean knowledge kind in Java:

boolean myBool = true;
boolean myBool2;


The char knowledge kind shops one single 16-bit Unicode character. It has a minimal worth of ‘u0000‘ (or 0) and a most worth of ‘uffff‘ (or 65,535 inclusive). Its default worth is ‘u0000‘.

Listed here are some examples of find out how to use the char knowledge kind in Java:

char myChar1 = 'A';
char myChar2 = 65;

Remaining Ideas on Java Primitive Knowledge Sorts

In Java, primitive knowledge varieties are distinct containers for values that don’t share state with different primitive values. Additionally they act because the constructing blocks for the non-primitive knowledge varieties. One factor that was not talked about beforehand is that primitive variables are handed to strategies by worth, which implies that strategies obtain a replica of the worth. That enables builders to make adjustments to the variable inside a way with out affecting the unique.

Learn extra Java programming tutorials and software program growth suggestions.