Wednesday, October 20, 2021

Java Datatypes

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

There are two kinds of datatypes is Java. They are primitive datatypes and user-defined datatypes.

In this tutorial, we shall learn about primitive datatypes in detail with the help of examples. User-defined datatypes are classes and we shall discuss about these in Java Object Oriented Programming concept tutorials.

Java Datatypes

What is Datatype?

A datatype represents the type of value stored in a variable. It defines the size of storage in memory for a variable and range of values a variable can hold.

List of Primitive Datatypes in Java

There are eight primitive datatypes in Java. They are

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

Though a string of chars is not a primitive datatype, there is a lot of support given to char strings via java.lang.String class.

Primitive Datatypes – Size & Default Value

The following table summarises the size of a value, default value, and information about the possibility of 2’s complement for each datatype.

Note #1: int could be used as an unsigned with the help of Integer class, in Java SE8.

Note #2: Variables that are declared but not initialized, gets a default value by the compiler. But there is an exception for local variables. The compiler does not initialize local variables, which could turn things uncertain and therefore it is a good practice to initialize variables. When we try to access these un-initialized local variables, compiler may throw an error during compilation that the variable is used without initialization.

Note #3: Signed/Unsigned reveals if that datatype could hold a negative value or not, respectively.

Datatype of Variable

As we know, Java is strictly a statically-typed language. Therefore, we should declare the datatype for a variable before we can use it. Literally, we have to type in the datatype before writing out your variable name for the first time in the program. This ensures that the name used for the variable with a specific datatype is not used for another datatype in subsequent program statements. If the same variable name is used to declare a variable of another datatype, the compiler throws an error during compiling.

An example to declare a variable n with an int datatype is

int n;

When we mention the variable n in our program for the first time, it has to be declared, which in this case is ‘int’. And from this moment on, in its lifetime, it is stored in memory like an integer, it behaves like an integer and everything like an integer. The variable once declared with a specific datatype, cannot be redeclared.

Initialize Variables

It is always a good practice to initialize a variable. It makes sure we know the initial state of that variable and avoid compile errors(Note #3 above). Being said that, now let us see how to initialize variables of different datatypes with the help of examples in the following table.

In the following program, we will initialize variables of different datatypes that we mentioned in the above table.

Example.java

public class Example {

    public static void main(String[] args) {

        byte b = 65;

        short s = 456;

        int i = 10;

        long l = 25589;

        float f = 5.6f;

        double d = 5.63128e3;

        boolean bool = false;

        char ch = 'A';

        String str = "Good day!";

    }

}

1 comment:

  1. i found it very helpful. thank u

    Mast Banarasi Paan is one of the best selling paan such as Rajwadi paan, Laddu paan, Choclate Paan, Fire paan, Silver paan. Mast Banarasi Paan is made available to every customer with unique flavor, quality taste, and hygiene and we are the best paan provider in India.
    banarasi paan

    ReplyDelete