Different Java Data Type – Primitive and Non-Primitive Data type

different java data types - place 4 java

Different Java Data Types

Java is rich in data types that allows programmer to use appropriate data types as he/she required in his application. In this article we discuss different java data types with it’s size, range and example. Let’s see.

Before we start we about data type we should know what is Variable ? So we start from Variable.

What is Variable ?

Variable is nothing but a name which is given to a memory location.

 

What is Data Type ?

Data type is representation of  different type of data that can be stored in variable. Like integral, floating, character, string, etc.

In java, There is two types of Data Type:

1) Primitive data type :

Java supports 8 primitive data types: byte, short, int, long, float, double, char and boolean.

2) Non-primitive data types :

String, Array, Classes, Interface, etc.

 different java data types

Primitive Data types :

A primitive type is predefined by the java language and is named by a reserved keyword. Primitive values do not share state with other primitive values. Java Programming Language have eight primitive data types

1) byte :

  • The byte data type is an 8-bit signed two’s complement integer.
  • It has a minimum value of -128 (27 ) and a maximum value of 127 (27 -1).
  • A size of byte data type is 8 bit or 1 byte.
  • Default value for byte data type is 0.
  • The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
  • The byte data type is can also be used instead of int data type if value is in range of byte.
  • Ex. byte b = 10;

2) short :

  • The short data type is a 16-bit signed two’s complement integer.
  • It has a minimum value of -32,768 (215) and a maximum value of 32,767 (215 -1).
  • A size of short data type is 16 bit or 2 bytes.
  • Default value for byte data type is 0.
  • As with byte data type we can use a short to save memory in large arrays, in situations where the memory savings actually matters.
  • The short data type is rarely used data type.
  • Ex. short s = 10000;

3) int :

  • The int data type is a 32-bit signed two’s complement integer.
  • It has a minimum value of -2147483648 (-231 ) and a maximum value of 2,147,483,647 (231-1).
  • A size of int data type is 32 bit or 4 bytes.
  • Default value for byte data type is 0.
  • Integer is generally used as the default data type for integral values unless there is a concern about memory.
  • Ex. int i = 100000;

4) long :

  • The long data type is a 64-bit two’s complement integer.
  • It has has a minimum value of -9,223,372,036,854,775,808(-263) and a maximum value of 9,223,372,036,854,775,807 (263 -1).
  • A size of long data type is 64 bit or 8 bytes.
  • Default value for byte data type is 0.
  • The long data type is used when a wider range than int is needed.
  • Ex. long l = 1000000000;

5) float :

  • The float data type is a single-precision 32-bit IEEE 754 floating point.
  • A size of float data type is 32 bit or 4 bytes.
  • Default value for byte data type is 0.0f.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Ex. float f = 10.5f;

6) double :

  • The double data type is a double-precision 64-bit IEEE 754 floating point.
  • A size of double data type 64 bit or 8 bytes.
  • Default value for byte data type is 0.0.
  • Decimal values, this data type is generally the default choice.
  • This data type should never be used for precise values, such as currency.
  • Ex. double d = 618.6;

7) boolean :

  • boolean data type represents one bit of information.
  • A size of boolean data type is 1 bit.
  • A default value for boolean type is false.
  • The boolean data type has only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions
  • Ex. boolean b = true;

8) char :

  • The char data type is a single 16-bit Unicode character.
  • A size of char data type is 16 bits or 2 bytes.
  • A default value for char type is’\u0000′.
  • It has a minimum value of ‘\u0000’ (0) and a maximum value of ‘\uffff’ (65,535).
  • A char is data type to store an any single character.
  • Ex. char c = ' D' ;

different java data types

 

Different Java Data Types Program :

class DataTypes
{
   public static void main(String args[])
   {

     byte byteVar = 10;
     short shortVar = 100;
     int intVar = 1000;
     long longVar = 10000;
     float floatVar = 10.5f;
     double doubleVar = 618.6;
     boolean booleanVar = true;
     char charVar ='D';
 
    System.out.println("Value Of byte Variable is " + byteVar);
    System.out.println("Value Of short Variable is " + shortVar);
    System.out.println("Value Of int Variable is " + intVar);
    System.out.println("Value Of long Variable is " + longVar);
    System.out.println("Value Of float Variable is " + floatVar);
    System.out.println("Value Of double Variable is " + doubleVar);
    System.out.println("Value Of boolean Variable is " + booleanVar);
    System.out.println("Value Of char Variable is " + charVar);
   }
}

Download First Java Hello World Program, click here.


I hope you got the information related to different java data types.

Thank You 🙂

Stay connected with Place4Java and Follow us on our Facebook Page.


 

1 Comment

2 Trackbacks / Pingbacks

  1. Different types of Variables in Java like Instance, Static, Local, etc.
  2. Swap two variable without using Third Variable using Arithmetic Operator

Leave a Reply

Your email address will not be published.


*