Select Page

Primitive Data Types in Java


Java has eight (8) primitive data types. Variables are declared to be one of the data types in Java. The data type of a variable determines what value can be stored in it.


nanadwumor

May 21, 2023

Primitive Data types in java


  • Java has eight (8) primitive data types.
  • A variable is declared by writing the dataType first and the variable name beside it. The declaration ends with a semi-colon.
  • Primitive data cannot do anything except storing data

RECOMMENDED ARTICLES


Variables and Literals in Java
Variables and Literals in Java

Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...

double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal number.   The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...

The print and println methods in Java
The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters  onto the console The print...


There are different data types in Java. Java has data types that are termed as primitive.

Java has eight (8) primitive data types. Variables are declared to be one of the data types in Java. The data type of a variable determines what value can be stored in it. Variables are examples of identifiers.


Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!


Why are some variables called primitive?

When something is termed primitive, it implies it is crude, archaic or undeveloped.

Java calls data types that cannot do anything else except storing values as primitive.

For instance, a variable that holds the age of a person contains only the number of years of the person. This age value doesn’t do anything else in addition. The value cannot perform any other function.

Hence, such data are termed primitive.

Why is it important to assign data types to variables?

In real life, programs are created to collect trove of data. These data may have different categories.

For example, a program created to collect data about students will collect their names. This will be a string of characters. Thus, such variable will be of String data type. Note that a String data type is an object. Example,

String name=“Douglas”;

Another variable will likely collect the students’ age in years. Such a variable will likely be declared as an int (integer) because decimal figures won’t be needed for months or weeks. Example:

int age=19;

There are several advantages for giving a data type to a variable.

First, it determines the amount of memory the variable will use. For instance, a double variable uses more memory than an int variable. It also helps to know how the variable formats data.

For instance, if you write a program to calculate distance between the stars or planets, you need a data type that can hold huge values.

If, however, you declare a variable to hold the distance between atoms, you need data types that can handle small distances.

There are eight primitive data types in Java. These are byte, short, int, long float, double, char, and boolean. Each data type has its memory size.

Tabular Representation of  primitive data types

TYPE DESCRIPTION DEFAULT VALUE SIZE EXAMPLE LITERALS
VALUE RANGE
byte two’s complement integer 0 8 bits (none) -128 to 127
short two’s complement integer 0 16 bits (none) -32,768 to 32,767
int two’s complement integer 0 32 bits -5, -4, -3, -2, -1, 0, 1, 2

-2,147,483,648 to

2,147,483,647

long two’s complement integer 0 64 bits -5L, -4L, -3L, -2L, -1L, 0L, 1L, 2L

-9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

float IEEE 754 floating point 0.0 32 bits 1.2F, 0.5f, 0.003F, up to 7 decimal digits
double IEEE 754 floating point 0.0 64 bits 1.2, 0.5, 0.003, up to 16 decimal digits
boolean true or false false 1 bit true, false true, false
char unicode character \u0000 16 bits ‘a’, ‘A’, ‘B’, ‘\u001’

0 to 255

(ASCII Values)

For the Java language, all the sizes and ranges of all the primitive data types are the same on all computers.

The integer data types

Out of the eight (8) primitive data types, four(4) of them are integers. These are byte, short, int and long.

Declaring a variable

A variable is declared by writing the dataType first and the variable name beside it. The declaration ends with a semi-colon. That’s ,

dataType variableName;

Examples are;

int age;

double price;

float height;

char alphabet;

boolean statust;

Multi-declaration of same Data Type

In real programs, many variables are used. Because of that, declaring variables one after the other can be tedious. There’s a way to make the declaration less demanding especially if the variables are of the same dataType.

If two or more variables are of the same data type, they can be declared together in one line horizontally. That’s,

dataType age, height, weight;

Note that multi-declaration of variables ends with a semi-colon.


You May Also Like…


double data type in Java
double data type in Java

The double data type is also a floating point number. It is called double because it has a...



0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *