Skip to main content

Data Type and Variable in Java

In Java programming, we deal with a lot of data such as numbers, characters, floating points etc.

We use and store all these data in programming purpose. All these data can be classified into 8 basic types.

These 8 basic data types are also known as primitive data types.

 

Primitive data types in Java:

We can store many types of data such as integer, decimal, char, etc. There are total 8 primitive data types used in Java programming. These data types are listed as follows.

 

Floating Data Type:

1.     Float

2.     Double

Integer Data Type:

3.     Byte

4.     Short

5.     Integer

6.     Long

Other Data Type:

7.     Character data type

8.     Boolean data type

Floating Data Type:

Floating data type is data type which contains fractional numbers or floating numbers or you can simply say the number which has decimal point. For example 4.5, 50.55, etc.

Floating data type is of two types –

1.     Float data type

2.     Double data type

The float and the double data types are the same as they both contains fractional values. The only difference is that, they own different storage capacities.

The double data type can contain decimal numbers ranging between -1.7976931348623157 x 10308, 1.7976931348623157 x 10308

Double data type takes 8 bytes of memory size to store floating digit number.

On the other hand, a float data type can store numbers ranging between -3.4028235E38F, 3.4028235E38F and a float data type consumes 4 bytes of memory.

 

Data Type

Number Range

Memory Allocation

Float

-3.4028235E38F to 3.4028235E38

4 Bytes

Double

(-) 1.7976931348623157 x 10308 to (+) 1.7976931348623157 x 10308

8 Bytes

 

                                              

Use of float and Double data types in Java Coding:

 

package JavaVariable;

 

public class JavaVariableClass {

 

     public static void main(String[] args) {

 

           float x = 3.4028235e38f;

           double y = 1.7976931348623157e308d;

               

           if(x>y) {

                System.out.println("FLOAT is greater than DOUBLE.");

                } else {

                System.out.println("DOUBLE is greater than FLOAT.");                

           }

               

     }

 

}

 

Console Result:

DOUBLE is greater than FLOAT.

 

Integer data type:

Integer data types are considered as those data which consists of numbers having no decimal point in between.

May it be a positive or a negative number that does not create any difference. The only condition that needs to be fulfilled to consider a number as an integer is that the number should be a whole number.

Byte, short, int, long these are various integer data types.

Byte is used to store small integer data, whereas long data type is known for storing big integer data.

Please use long, only when necessary otherwise memory will be wasted. If you want to store small numbers, use short variable type or int variable type.

 

Data Type

Number Range

Memory Allocation

Byte

-128 to 127

1 Byte

Short

-32768 to 32767

2 Bytes

Int

-2147483648 to 2147483647

4 Bytes

Long

-9223372036854775808 to 9223372036854775807

8 Bytes

 

Character data type:

This Data type represents single character value.

For example, ‘a’, ‘A’, ‘b’, ‘B’, ‘@’ etc.

When we assign the value of any character variable, we have to write the same inside a single quote.

The character variable is declared by the word ‘char’.

Character data type consumes only 2 bytes in the memory space.

 

Boolean data type:

Boolean data type contains only two values viz; true and false.

So, this data type is used to declare variables which store data to perform some conditional tasks.

Boolean data type requires only 1-bit of memory space to store in a variable.

 

Binary Number System:

In order to understand data type and variable we should properly understand Binary system.

All numbers / values stored in computer are in the form of 1 or 0. This is binary number system.

1-bit of a memory can either contain a 0 or a 1. So it has 2 possible outcomes.

If there are 2 bits available in the memory, then there are 22 = 4 possible outcomes.

For 3 bits there are 23 = 8 possible outcomes.

(000, 001, 010, 011, 100, 101, 110,  111).

 

For n-bits there are 2n possible outcomes.

For 8 bits (1 byte) there are 28 = 256 possible outcomes.

So 8 bits should contain numbers between 0 – 256, but it actually contains numbers between -128 to 127 because, although in 1-bytes there are 8 bits but the 1st bit (most significant bit) is a sign bit.

1 denotes (–ve) & 0 denotes (+ve) numbers.

So, remaining 7 bits can contain number up to 27 = 128.

There are up to 256 numbers only that can be contained in 1 byte in total including positive and negative numbers.

You can not only take positive numbers, you will require negative numbers also during programming.

So, you can store numbers between -27 to +27.

 

Variables:

Variable is a location in memory which contains data.

As the name suggests, the data stored in a variable can be edited.

Without variable we cannot access the data stored in the memory.

The variable is assigned a name and by that name we can access the variable and data.

Suppose we want to store a number (say 30), then we need to assign a variable and into that variable we have to store the number 30.  The variable must be assigned a name.

 

For example, Variable name = roll_number

Data type of variable = int

Value inside the variable = 30

 

Now when we what to access the variable, we have to look for the variable name and not the variable value because there might be same value in multiple variables.

Variable names are always unique.

 

See How data types and variable is assigned in Java programming.

 

package JavaVariable;

 

public class JavaVariableClass {

 

    public static void main(String[] args) {

 

        byte x = 5;

        int age = 20;

        float percentageOfMarks = 10.5f;

        double rateOfGrowth = 20.3873;

        char alphabate = 'A';

        boolean isSingle = true;

       

        System.out.println(x);

        System.out.println(age);

        System.out.println(percentageOfMarks);

        System.out.println(rateOfGrowth);

        System.out.println(alphabate);

        System.out.println(isSingle);          

 

    }

 

}

 

Result in Console:

5

20

10.5

20.3873

A

true

 

 

 

 

 

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...