Apps/Gaming

Java Primitive Data Types

You may have heard it said before that Java is a strongly typed programming language. That means that every variable that is defined has to be assigned a data type, which determines what kind of data it can hold for the duration of program execution. In other words, once defined, a variable cannot change type…at least not without recompilation. With that in mind, you may want to think carefully before assigning a variable’s type, as choosing the wrong kind can have fairly serious repercussions.

This programming tutorial will list all of the primitive data types supported by the Java language, as well as provide a description of each, so that developers will be better equipped to match variables to their appropriate types.

Looking to learn Java software development in a class or online course? We have a list of the Best Online Courses to Learn Java to help get you started.

What are the Data Types in Java?

Broadly speaking, Java data types can be classified into two types: primitive and non-primitive.

Primitive data types include:

  • byte
  • short
  • internal
  • long
  • float
  • double
  • boolean
  • char

In addition to the values ​​it may contain, the data type also determines the operations that may be performed on it. Moreover, primitive values ​​do not share state with other primitive values, and are, as such, completely discreet and autonomous. You can think of them as the building blocks for the non-primitive data types.

Meanwhile, non-primitive types are those which refer to things, and are thus termed reference types. As objects, non-primitive types can include methods that perform actions. Strings, arrays, classes, and interfaces are all examples of non-primitive types.

There are a few other differences between primitive and non-primitive types, but we will cover those a little later on, as we go over the primitive data types available in Java in more detail.

Reading: What are Variables in Java?

Primitive Data Types in Java

As we will see in this section, each primitive data type allocates a certain amount of memory and comes pre-initialized with its own default value.

byte

The byte data type is a very small 8-bit signed integer. The term “signed” means that it can represent both positive and negative numbers. It has a minimum value of -128 and a maximum value of 127, inclusively. Its small size makes it ideal for using in large arrays, where the memory savings can be substantial. They can also be used in place of internal, where their strict size limits may help to notify other developers that a variable is meant to store fairly small numbers, like a person’s age. Its default value is 0.

Here are some examples of how to use byte data types in Java:

bytes b = 99; bytes b2;

The above code would add the value 99 to the byte data type named b. The second line initialized a byte named b2which would contain the default value of 0since we did not assign it a value.

Short

With its 16-bit size, the short data type fits neatly between a byte other internal. It has a much larger range of -32,768 to 32,767 inclusive. It is also great for managing memory and informing other developers about the kinds of numbers it may contain. Its default value is 0.

Here are some examples of how to use the short data type in Java:

short s = 29999; short s2;

international

the internal data type is an interesting data type in that it can be either signed or unsigned. As a 32-bit signed integer (the default), it has a minimum value of -231 and a maximum value of 231-1or -2,147,483,648 to 2,147,483,647. declaring on internal as an unsigned integer shifts its range to the positive side of the scale, giving it a minimum value of 0 and a maximum value of 232-1or 4,294,967,295. Its default value is 0.

Here are some examples of how to use the internal data type in Java:

int a = 456789; int b;

Long

the long data type has even more storage capacity than an internal, requiring 64 bits. Like the internalthe long type can also be signed or unsigned. The signed long has a minimum value of -263 and a maximum value of 263-1or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Meanwhile, an unsigned 64-bit long has a minimum value of 0 and a maximum value of 264-1or a whopping 18,446,744,073,709,551,615! Its default value is 0L.

Here are some examples of how to assign values ​​to a long data type in Java:

long l = 1556667890L; longl_2;

float

the float data type is a 32-bit single precision floating point. While large enough to store factual numbers between 3.4e-038 other 3.4e+038 with up to seven decimal digits, once the value gets past six decimal points, the number will become less precise and more of an estimate. Hence, if you require precise values, such as for currency, you will need to use the java.math.BigDecimal class instead. Its default value is 0.0f.

Here are some examples showing how to assign values ​​to a float data type in Java:

float myFloat = 256.8f; float myFloat2;

doubles

the double data type is a double-precision 64-bit floating point, large enough for storing fractional numbers between 1.7e-308 other 1.7e+308 up to 15 decimal digits. It suffers from the same limitation limit as float does and should therefore only be used if approximation errors are acceptable. Its default value is 0.0d.

Here are some examples of how to assign values ​​to a double data type in Java:

double myDouble = 256.7879837; double myOtherDouble;

Reading: Java Tools to Increase Productivity

boolean

From the largest to smallest data type, the boolean has only two possible values: true and false. It stores its value in a single bit. However, for convenience, Java pads the value and stores it in a single byte. One limitation of the boolean data type is that, while fine for simple flags that track true/false conditions, it does not have an “unset” value, which can lead one to erroneously conclude that a value is false, when it is really not set. Its default value is false.

Here are some examples of how to assign values ​​to a boolean data type in Java:

boolean myBool = true; boolean myBool2;

Char

the char data type stores one single 16-bit Unicode character. It has a minimum value of ‘u0000‘ (or 0) and a maximum value of ‘uffff‘ (or 65,535 inclusive). Its default value is ‘u0000‘.

Here are some examples of how to use the char data type in Java:

char myChar1 = ‘A’; char myChar2 = 65;

Final Thoughts on Java Primitive Data Types

In Java, primitive data types are distinct containers for values ​​that do not share state with other primitive values. They also act as the building blocks for the non-primitive data types. One thing that was not mentioned previously is that primitive variables are passed to methods by value, which means that methods receive a copy of the value. That allows developers to make changes to the variable within a method without affecting the original.

read more Java programming tutorials and software development tips.

Related posts

Formatting Strings in Java: String.format() Method

TechLifely

An introduction to Annotations in Java

TechLifely

Overview of DSDM Project Management

TechLifely

Leave a Comment