Wednesday, October 5, 2022
HomeSoftware DevelopmentJava Primitive Information Sorts |

Java Primitive Information Sorts |

Java Developer Tutorials

You’ll have heard it mentioned earlier than that Java is a strongly typed programming language. That implies that each variable that’s outlined must be assigned a knowledge sort, which determines what sort of knowledge it could possibly maintain during program execution. In different phrases, as soon as outlined, a variable can’t change sort…at the least not with out recompilation. With that in thoughts, you might wish to consider carefully earlier than assigning a variable’s sort, as selecting the mistaken sort can have pretty severe repercussions.

This programming tutorial will record the entire primitive knowledge sorts 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 sorts.

Seeking to study Java software program improvement in a category or on-line course? Now we have a listing of the Greatest On-line Programs to Study Java to assist get you began.

What are the Information Sorts in Java?

Broadly talking, Java knowledge sorts might be labeled into two sorts: primitive and non-primitive.

Primitive knowledge sorts embrace:

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

Along with the values it could comprise, the information sort 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’ll be able to consider them because the constructing blocks for the non-primitive knowledge sorts.

In the meantime, non-primitive sorts are these which confer with issues, and are thus termed reference sorts. As objects, non-primitive sorts can embrace strategies that carry out actions. Strings, Arrays, Lessons, and Interfaces are all examples of non-primitive sorts.

There are a number of different variations between primitive and non-primitive sorts, however we’ll cowl these slightly afterward, as we go over the primitive knowledge sorts obtainable in Java in additional element.

Learn: What are Variables in Java?

Primitive Information Sorts in Java

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


The byte knowledge sort is a really small 8-bit signed integer. The time period “signed” implies that it could possibly symbolize each optimistic and detrimental numbers. It has a minimal worth of -128 and a most worth of 127, inclusively. Its small measurement makes it very best for utilizing in giant arrays, the place the reminiscence financial savings might be substantial. They can be used rather than int, the place their strict measurement 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 easy methods to use byte knowledge sorts in Java:

byte b = 99;
byte b2;

The above code would add the worth 99 to the byte knowledge sort 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 price.


With its 16-bit measurement, the brief knowledge sort suits neatly between a byte and int. It has a a lot bigger vary of -32,768 to 32,767 inclusive. It’s also 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 easy methods to use the brief knowledge sort in Java:

brief s = 29999;
brief s2;


The int knowledge sort is an attention-grabbing knowledge sort 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 aspect 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 here are some examples of easy methods to use the int knowledge sort in Java:

int a = 456789;
int b; 


The lengthy knowledge sort has much more storage capability than an int, requiring 64 bits. Just like the int, the lengthy sort can 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 easy methods to assign values to a lengthy knowledge sort in Java:

lengthy l = 1556667890L;
lengthy l_2;


The float knowledge sort 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 develop into much less exact and extra of an estimate. Therefore, should you require exact values, akin to for forex, you have to to make use of the java.math.BigDecimal class as an alternative. Its default worth is 0.0f.

Listed here are some examples displaying easy methods to assign values to a float knowledge sort in Java:

float myFloat = 256.8f;
float myFloat2; 


The double knowledge sort 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 subsequently solely be used if approximation errors are acceptable. Its default worth is 0.0d.

Listed here are some examples of easy methods to assign values to a double knowledge sort in Java:

double myDouble = 256.7879837;
double myOtherDouble;

Learn: Java Instruments to Enhance Productiveness


From the most important to smallest knowledge sort, 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 sort is that, whereas superb for easy flags that monitor true/false situations, 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 here are some examples of easy methods to assign values to a boolean knowledge sort in Java:

boolean myBool = true;
boolean myBool2;


The char knowledge sort 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 easy methods to use the char knowledge sort in Java:

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

Last Ideas on Java Primitive Information Sorts

In Java, primitive knowledge sorts 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 sorts. 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 technique with out affecting the unique.

Learn extra Java programming tutorials and software program improvement ideas.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments