DATA TYPES IN JAVA

A data type is a variable attribute that tells the compiler or interpreter how the programmer expects to make use of the variable. It specifies which operations can be carried out on the data and what kind of values can be stored

Table of Contents

[hide]
  • What is a Data Type in Java
  • Primitive Data Types
  • Non-Primitive Data Types

What is a Java Data Type

Every variable in Java has a Data type, every expression in Java also has a Data type. There is no logic to create a variable without the type. Each and every Data type in Java is strictly defined, so compiler checks for type compatibility while compiling the program.

For all the reasons specified above Java is known as Strongly Typed Programming Lanugage. You must specify the variable type before using it. Java is also called statically Typed language for the same reason.

Lets consider and example where a variable has been declared with the Data type as int.

int total;

Before a Java variable is usable, it must be assigned a memory location and initialized. You must specify the DATA TYPE to declare (create) a variable, give at least one space, then the name of the variable and end the line with a semicolon (;). Genral form for the declaration could be as :type variableName = expression;. To learn more about Java variables kindly visit variables in java where we have discussed java variables in detail.

Two types of Data Types are there in Java respectively.

  • 1. Primitive Data Types
  • 2. Non-Primitive Types

Let us Learn about them in detail in the upcoming section.

Primitive Data Types

Primitive Data Types are built in data types provided by Java. Built in data types are the basic building blocks of data manipulation.In simple terms, each and every data type is strictly pre-defined for which Java built-in support is provided.

There are 8 types of Primitive data types in Java.

  • 1. boolean data type
  • 2. byte data type
  • 3. short data type
  • 4. char data type
  • 5. int data type
  • 6. long data type
  • 7. float data type
  • 8. double data type

Data Types in Java

data-types-in-java

Primitive Data Types in Java

Data TypeDefault ValueDefault SizeRange of Values
booleanfalse1 bittrue, false
char\u00002 bytecharacter representaion of ASCII values 0 to 255
byte01 byte-128 to 127
short02 byte-32,768 to 32,767
int04 byte-2,147,483,648 to 2,147,483, 647
long0L8 byte-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float0.0f4 byteapprox (6-7 significant decimal digits)
double0.0d8 byteapprox (15 significant decimal digits)

Java boolean Data Type

boolean data type is a primitive data type in Java that can only have a value of either true or false. It representsf 1 bit of information. It is a reserved keyword that cannot be used for variable, class or object names.

It is used as a flag when a programmer performs some check to be true or false based on some condition. Example if the flag value is set to true then only display your age . We're just taking simple examples as we are on basic java concepts, once we will reach to more complex topics, examples bar would be raised high.

Example

copyimage
// Java program to illustrate boolean data type in Java
public class DataTypeExample{
      public static void main(String args[]){  
             int age =20;
             boolean name = true;
             if (name == true) {
             System.out.println("My age is : " + age);
           }
      }
}

OUTPUT: My age is : 20

You can try this example in your own system and can experiment with the boolean values. Just pass false to the boolean variable and see what happens.

Java char Data Type

A char (character) data type is a primitive data type that uses Unicode format. It occupies approximately 16 bits in size, double that of byte. A char data type is used to storing characters.

UNICODE

Unicode is a technique for computer encoding, assigning a unique number for each character without depending on the type of system and its platform. All characters in Unicode are represented by numeric values. For instance, 65 depicts letter A, 66 depicts B, and so on. The lower case letters start at 97.

Example 1

copyimage
// Java program to illustrate char data type in Java
public class DataTypeExample{
        public static void main(String args[]){  
              char value1 ='A';
              char value2 ='a';

              System.out.println("value 1 is : " + value1);
              System.out.println("value 2 is : " + value2);
       }
}

OUTPUT:

value 1 is : Avalue 2 is : a

This was one of the basic examples of how we can display characters in Java.

Example 2

copyimage
// Java program to display corresponding ASCII value for the given inputs
public class DataTypeExample{
       public static void main(String args[]){  
           char value1 = 65;
           char value2 = 97;

           System.out.println("value 1 is : " + value1);
           System.out.println("value 2 is : " + value2);
     }
}

OUTPUT:

value 1 is : Avalue 2 is : a

The outputs of both programs are same. Previous example just printed the char value as the input was a character. The later one typecasted the integer input value to char and thus displayed its corresponding ASCII value.

Java byte Data Type

byte data type is one of the primitive data type that can be used to save memory. If you are certain that the value will be within the byte range that is between -128 to 127 then its recommended to use byte instead of int or any other integer types.

Default value of this data type is 0 while default size is 1 byte. Let us see an example of the byte data type in java.

Example

copyimage
// Java program to illustrate byte data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        byte value = 118; // value 118 lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : 118

In the example given, we provided the value that is within the range of the byte data type, you try to specify the value that is outside the range, suppose 140, which will give an incompatible type error.

Java short Data Type

short is a primitive data type in Java that can store values within range -32768 to 32767 . It is also used to save memory in large arrays. Default value of this data type is 0 while default size is 2 byte (16 bit).

Example

copyimage
// Java program to illustrate short data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        short value = 656; // value 656 lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : 656

byte data type could not store this value, but the short type can store this value because of its wider range.

Java int Data Type

int data type is a primitive data type in java that is used when short is not large enough to hold the given value. Int data type is a 32-bit signed two's complement integer.

int data type is the preferred data type for integral values. The default value is 0. It ranges between -2,147,483,648 to 2,147,483,647 with a default size of 4 bytes.

Example

copyimage
// Java program to illustrate int data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        int value = 1542567; // value 1542567 lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : 1542567

int can store large values hence when there is no problem of memory, you can use int.

Java long Data Type

long data type is a primitive data type which is used when int is not large enough to hold a given value. You should end the value of a long data type with a "L".

The default value is 0. It ranges between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 with a default size of 8 bytes.

Example

copyimage
// Java program to illustrate long data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        long value =  1542567876567L; // value  1542567876567L lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : 1542567876567

int does not have that wide range that it can support the given value, hence long has been used in the given program.

Java float Data Type

float data type is a primitive data type that is sufficient for holding 6-7 decimal digits. The float data type in Java is a single-precision 32-bit IEEE 754 floating point.

If you need to conserve memory in huge arrays of floating point numbers, it is recommended to use a float (instead of double). You should end the value of a long data type with an "f".

Example

copyimage
// Java program to illustrate float data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        float value = 16.6f; // value 16.6f lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : 16.6

The default value is 0.0F and size is 4 bytes. It is recommended not to use float type with the exact values like runs.

Java double Data Type

double data type is a primitive data type that is sufficient for holding upto 15 decimal digits. The double data type in Java is a double-precision 64-bit IEEE 754 floating point.

double type is generally the default choice for decimal value. You should end the value of a long data type with an "d".

Example

copyimage
// Java program to illustrate double data type in Java
public class DataTypeExample{
   public static void main(String args[]){  
        double value = -6564566.6d; // value -6564566.6d lies within the byte range
        System.out.println("value is : " + value);
     }
}

OUTPUT:

value is : -6564567.6

The default value is 0.0d and size is 8 bytes. It is recommended not to use double type with the exact values like runs.

Non-Primitive Data Types

Non-Primitive datatypes in Java are created by the programmer while programming not like Primitive data types that are pre-defined. They are also referred to as reference variables or object variables because they referred to the location where data is stored.

Examples of Non-Primitive data type in java are :

Java Strings

Strings in java are basically sequence of characters. Once they've been created you can not change their values as strings in java are immutable.

Easiest way to create a string object is by creating a string type variable and assigning it with sequence of characters as shown below.

String str = "Learn to Code";

If you wish to learn more about Strings in java then you can refer to Java Strings.

Java Arrays

array in Java is an object that contains datatype which are similar in nature. They are also declared by the programmer. It stores data in a consecutive manner. Indexing of array always starts from 0.

The elements in the array are accessed by their index. They are used to store data in a consecutive manner and the size of the data must be specified by an int.

int arr[ ]; // array declaration

If you wish to learn more about arrays in java then you can refer to Java Arrays.

Java Classes

class in Java is a user-defined data type that is used to create objects of one type. It is a blueprint of an object. It describes the properties and methods of an object.

class can contain variables, fields, methods, blocks etc. A class can be public or can have a default modifier. A class body is surrounded by braces { }

public class CodeBator

{
....
body
}

If you wish to learn more about classes in java then you can refer to Java Classes.

Java Interface

Interface in Java is a way to achieve abstraction. By default interface methods are abstract and public. An interface cannot contain a constructor.

Interface in java is also used for security purpose, it can be used to hide certain details of an object that is not necessary to be shown.

public interface CodeBator{
final int b // public, static, final
void show( ) // public and abstract method
}

If you wish to learn more about Interfaces in java then you can refer to Java Interfaces.