Java Basic Data Types

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in 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 data types available in Java:

  • Primitive Data Types
  • Reference/Object Data Types

 

Primitive Data Types:

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.

byte:

  • Byte data type is an 8-bit signed two's complement integer.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
  • Example: byte a = 100, byte b = -50

 

short:

  • Short data type is a 16-bit signed two's complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767(inclusive) (2^15 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
  • Default value is 0.
  • Example: short s= 10000, short r = -20000

 

int:

  • int data type is a 32-bit signed two's complement integer.
  • Minimum value is – 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Example: int a = 100000, int b = -200000

 

long:

  • Long data type is a 64-bit signed two's complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.
  • Example: int a = 100000L, int b = -200000L

 

float:

  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.

double:

  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Example: double d1 = 123.4

 

 

Here is a short program that uses double variables to compute the area of a circle:

// Compute the area of a circle.
class Area {
    public static void main(String args[]) {
        double pi, r, a;
        r = 10.8; // radius of circle
        pi = 3.1416; // pi, approximately
        a = pi * r * r; // compute area
        System.out.println("Area of circle is " + a);
    }
}

 

boolean:

  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false.
  • Example: boolean one = true

 

Here is a program that demonstrates the boolean type:

// Demonstrate boolean values.
class BoolTest {
    public static void main(String args[]) {
        boolean b;
        b = false;
        System.out.println("b is " + b);
        b = true;
        System.out.println("b is " + b);
        
        // a boolean value can control the if statement
        if(b) 
            System.out.println("This is executed.");

        b = false;
        if(b) 
            System.out.println("This is not executed.");

        // outcome of a relational operator is a boolean value
        System.out.println("10 > 9 is " + (10 > 9));
    }
}

The output generated by this program is shown here:

b is false

b is true

This is executed.

10 > 9 is true

 

char:

  • char data type is a single 16-bit Unicode character.
  • Minimum value is '\u0000' (or 0).
  • Maximum value is '\uffff' (or 65,535 inclusive).
  • Char data type is used to store any character.
  • Example: char letterA ='A';

Here is a program that demonstrates char variables:

// Demonstrate char data type.

class CharDemo {

   public static void main(String args[]) {

      char ch1, ch2;

      ch1 = 88; // code for X

      ch2 = 'Y';

      System.out.print("ch1 and ch2: ");

      System.out.println(ch1 + " " + ch2);

 }

}

This program displays the following output:

ch1 and ch2: X Y

 

Java Variable Types

A variable provides us with named storage that our programs can manipulate.

Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

You must declare all variables before they can be used. The basic form of a variable declaration is shown here:

 data type variable [ = value][, variable [= value] …] ;

Here data type is one of Java's data types and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list. Following are valid examples of variable declaration and initialization in Java:

int a, b, c; // Declares three ints, a, b and c.

int a = 10, b = 10;

// Example of initialization

byte B = 22; // initializes a byte type variable B.

double pi = 3.14159; // declares and   assigns a value of PI.

char a = 'a'; // the char variable a is initialized with value 'a'

This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java:

  • Local variables
  • Instance variables
  • Class/static variables

 

Local variables:

  • Local variables are declared in methods, constructors, or blocks.
  •  Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
  • Access modifiers cannot be used for local variables.

 

Dynamic Initialization

  • Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared.
  • For example, here is a short program that computes the length of the hypotenuse of a right triangle given the lengths of its two opposing sides:
// Demonstrate dynamic initialization.
class DynInit {
    public static void main(String args[]) {
        double a = 3.0, b = 4.0;
        // c is dynamically initialized
        double c = Math.sqrt(a * a + b * b);
        System.out.println("Hypotenuse is " + c);
    }
}

Here, three local variables—a, b, and c—are declared. The first two, a and b, are initialized by constants. However, c is initialized dynamically to the length of the hypotenuse (using the Pythagorean theorem). The program uses another of Java’s built-in methods, sqrt( ), which is a member of the Math class, to compute the square root of its argument. The key point here is that the initialization expression may use any element valid at the time of the initialization, including calls to methods, other variables, or literals.

Java Introduction
Scope and Lifetime of Variables