Skip to main content

Command Palette

Search for a command to run...

Data Types in JAVA

Published
•7 min read•View as Markdown

What are Data Types?

Here comes our first topic in this Series of learning Java The Data types which Signify the different sizes and values that can be stored in a variable. In this blog, we are going to see the following topics very deeply.

  1. Data Types and why we use data types?

  2. What are primitive data types?

Why do we use Data Types?

To represent the data in a specific format we use data types if it is a number we as humans know what it is but the computer can't understand what we wanted to say.

For example, if I am asking my friend to give me 1000 rupees in some language which he knows he will understand what I am saying and if he is having money he will give it but if I ask in some other language that he doesn't know then he will not be able to understand what I am saying and he will not give money to me in order to take 1000 rupees from my friend I have to speak with him in an understandable language and then if there is a possibility then he will give money to me. same like in this example we (programmers) and computers should have good communication and for this Data types will help the compilers to understand what we are trying to say if it is a number we should define it as an integer or some other data type if it is a name we use string or some other data type which we discuss further in this blog.

There are 2 types of data types in Java

  1. Primitive Data Type

  2. Non-Primitive Data Type.

We will discuss Primitive Data types in this post and later we will see the Non-Primitive Data types.

What are Primitive DataTypes?

Generally, primitive means The First. Java defines 8 Primitive types of data:

1)Byte 2) Short 3) int 4) Long 5) Float 6) Double 7) Char 8) boolean

According to the requirement in our code, we use these data types in different situations let's discuss these data types one by one

  1. We use Byte in a situation where we have a very short range of numbers which means the range is -128 to 127 ( -2^7 to 2^7-1 )

let's see the implementation:-

public class Main
{
     public static void main(String[] args)
     {
          Byte s,a;
          s=127;
          a=-128;
          System.out.println(s);
          System.out.println(a);
     }
}

Byte takes 8 bits of data (1 Byte = 8 bits) if we represent 128 in binary digits we will get 1 0 0 0 0 0 0 which is 8 bits of data. byte will be used when we are working with a stream of data from a network or file. if you try to print the value above the range (i.e.) value above 127 then compilers will throw an error to us stating

"incompatible types: int cannot be converted to Byte"

Because the value we had given is exceeding the range of Byte that's why the compiler shows int cannot be converted to Byte which means "int" is a higher range data type you will understand this error by the end of this post.

  1. Short is a signed 16-bit data type it ranges from -32768 to 32767 which is a 16-bit data. In Java Short data type is not used much because of the domination of int.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args)
         {
             Short s1=32767;
             Short s2=-32768;
             System.out.println("The range of short is "+s2+" to "+s1);
         }
     }
    

    Similar to Byte, if a value exceeds the maximum range of the data type short, an error will be generated.

    "incompatible types: int cannot be converted to Short"

  2. The integer is a signed 32-bit data type that ranges from -2,14,74,83,648 to 2,14,74,83,647. int is the most used data type in Java we use int data type very frequently if we are dealing with arrays or Collections for indexing or to retrieve some information from the arrays or Collections then this int will be used. you will get to know about these things in our future posts.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args) 
         {
             int a=-2147483648,b=2147483647;
             System.out.println("The range of int is: "+a+" to "+b);
         }
     }
    

    In the above code, we can see the range of integer data type if you want to try you can try giving different values to the variables a and b and then check the output if the value exceeds 2,14,74,83,647 then the compiler will throw the below error.

    "Error: integer number too large"

  3. Long is a signed 64-bit data type it is used in situations where we have larger values than the range of integer data type we discussed above the range of integer. the range of long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args) 
         {
             long a=-9223372036854775808L;
             long b=9223372036854775807L;
             System.out.println("the ranges of long is:"+a+" "+b);
         }
     }
    

    In the above code, we used the suffix "L" after initializing the integer value to the variables a and b if you remove that "L" then our compiler will throw us the same error as the above integer implementation code throws when it crosses its range. Even if you initialized the variable as long by default it takes an integer that suffix is the thing that makes the difference. Try removing that L in the above and see the output it will throw an error saying.

    Error: "integer number too large"

  4. Till now we discussed integer numbers but in the same numbers category, we have decimal values also. To use these decimal values in programming we use this float data type. There are two types of floating points float and double. float specifies the single precision value that uses 32-bit storage. variables of float are useful when we need a fractional value. the range of float data types is

    1.4e-0.45 to 3.4e+038.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args)
         {
             float a=12.2f;
             System.out.println(a);
         }
     }
    

    in the above code, we can see that when we declare a float value we are adding the suffix "f" the same as we used "L" for long.

  5. Continuation to the float we also use double while dealing with decimal values. double signifies the double precision value that uses 64-bit storage. double precision is faster than single precision. all mathematical functions such as trigonometric functions like sin(),cos() and other mathematical functions like sqrt() e.t.c returns double value. if you think your problem needs an accurate answer then double is the best choice. The range of double is 4.9e-324 to 1.8e+308.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args)
         {
             double a=102.67678;
             System.out.println(a);
         }
     }
    

    Above is the sample code which shows the declaration and initialization of a double Data type.

  6. The data type used to store characters is char. the size of the char data type is a 16-bit type. ranging from 0 up to 2^16 (i.e. 65536) The standard set of characters is known as ASCII Values. In c/c++ the size of char is only 8 bits. Since Java is designed for all programmers across the world. that's why JAVA uses Unicode to represent characters. Unicode is a fully international character set that represents all characters found in human language.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args)
         {
             char a='s';
             System.out.println(a);
         }
     }
    

    The above code is just a sample implementation of the char data type.

  7. Boolean is a 2-byte value used for logical values. logical values mean it can have any one value true (or) false. boolean is the value that is returned from many operators which we will discuss in later posts.

    let's see the implementation:-

     public class Main
     {
         public static void main(String[] args)
         {
             boolean s=true;
             boolean t=false;
             System.out.println(s);
             System.out.println(t);
         }
     }
    

In the above code, we can see the Initialization of the boolean data type. this takes only two values either true or false.

Conclusion

In this post, we learned about what are data types and why we use data types. And discussed the 8 primitive data types their ranges and the size of each data type. and implementation of each data type with a basic code. As time goes on, we'll utilize these data types more frequently, whether you're writing simple or complex code.

Master Java from Beginning (Zero to Hero)

Part 1 of 1

In this Series I am going to give you a complete and in depth analysis of Java and its concepts from Beginning.