Java Primitive Information Varieties |


Java Developer Tutorials

You could have heard it stated earlier than that Java is a strongly typed programming language. That implies that each variable that’s outlined needs to be assigned an information kind, which determines what sort of knowledge it might maintain at some stage in program execution. In different phrases, as soon as outlined, a variable can not change kind…no less than not with out recompilation. With that in thoughts, you might need to think twice earlier than assigning a variable’s kind, as selecting the fallacious type can have pretty severe repercussions.

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

Trying to be taught Java software program improvement in a category or on-line course? We now have an inventory of the Finest On-line Programs to Study Java to assist get you began.

What are the Information Varieties in Java?

Broadly talking, Java knowledge varieties could be categorized into two varieties: primitive and non-primitive.

Primitive knowledge varieties embody:

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

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

In the meantime, non-primitive varieties are these which check with issues, and are thus termed reference varieties. As objects, non-primitive varieties can embody strategies that carry out actions. Strings, Arrays, Lessons, 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 in a while, as we go over the primitive knowledge varieties obtainable in Java in additional element.

Learn: What are Variables in Java?

Primitive Information Varieties 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 might characterize each optimistic and adverse numbers. It has a minimal worth of -128 and a most worth of 127, inclusively. Its small dimension makes it very best for utilizing in giant arrays, the place the reminiscence financial savings could be substantial. They may also be used rather than int, the place their strict dimension limits might 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 below are some examples of how you can 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 include the default worth of 0, since we didn’t assign it a price.


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

Listed below are some examples of how you can 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 optimistic facet of the dimensions, 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 below are some examples of how you can 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 may 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 below are some examples of how you can 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 giant 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 turn into much less exact and extra of an estimate. Therefore, if you happen to require exact values, resembling for foreign money, you will have to make use of the java.math.BigDecimal class as a substitute. Its default worth is 0.0f.

Listed below are some examples displaying how you can 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, giant 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 will due to this fact solely be used if approximation errors are acceptable. Its default worth is 0.0d.

Listed below are some examples of how you can assign values to a double knowledge kind in Java:

double myDouble = 256.7879837;
double myOtherDouble;

Learn: Java Instruments to Enhance Productiveness


From the biggest to smallest knowledge kind, the boolean has solely two attainable 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 superb for easy flags that monitor true/false circumstances, it doesn’t have an “unset” worth, which may lead one to erroneously conclude {that a} worth is false, when it’s actually not set. Its default worth is false.

Listed below are some examples of how you can 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 below are some examples of how you can use the char knowledge kind in Java:

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

Remaining Ideas on Java Primitive Information Varieties

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 modifications to the variable inside a technique with out affecting the unique.

Learn extra Java programming tutorials and software program improvement suggestions.


Leave a Reply