Related post

Java data types | Zhullyblog

 

Data types in Java


Data types

In this tutorial you will learn about data type. 

What is data type?

As the name implies, data type defines the values a variable can hold. This means that it defines the range of value a variable can take.We discussed earlier about variables and learnt that in Java, a variable must be declare and the type of data must be specified. 


     Types of Data type


 •  Primitive data types

 •  Non-primitive data type



 PRIMITIVE DATA TYPES


Primitive data type specifies the size of value a variable can hold. Generally, there are eight primitive data types which are :

 • Byte

 • Short

 • Int

 • Long

 • Double

 • Char

 • Boolean

But primitive data types are sub-divided into and they are :

  • The integer types  -- are data types that stores whole number , positive number and negative number only.

  • The floating point types -- are data types that stores fractional numbers that is they take decimal numbers.

Now, Leta talk about each of them quickly.

 • Byte : This data type stores value from -128 to 127. This means that whatever number you want to store must be between -128 to 127. So, anything value larger than 127 cannot be stored in byte. Hence, it will output error.



public class sample {


  public static void main(String[] args) {

  byte num;

  num = 120;

  System.out.println(num);

  }


}



 • Short : This data type stores value between -32,768 to 32767. 


public class sample {


  public static void main(String[] args) {

  short num;

  num = 140;

  System.out.println(num);

  }


}



 • Int : This is the most commonly used data type. This data type stores value between -2,147,483,647 to 2,147,483,647.

 • Long : This stores value between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.




Double : This data type stores fractional numbers and can store 15 decimal digits.



 • Float : It can store 7 decimal degits.



 • Char : It hold characters




 • Boolean : Generally, Boolean holds either true or false.




Let's move


Break statement in Java


Continue statement in Java


Inheritance in Java



Please share 







Follow us on



Comments