Previous
Next

Java Data Types - Data Types in Java, Primitive data types

Java Data Types Examples

What are Data Types in Java ?

Java Data type specifies the size and type of values that can be stored in an identifier . In other words Data type defines the values that a variable can take. There are two types of data types in Java :

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

In this Java Data Type Tutorial, you can learn about different data types that Java programming language supports for creating variables including Primitive Data Types and Non-Primitive Data Types .

What are Java Primitive data types ?

The Java primitive data types include boolean, char, byte, short, int, long, float and double . A primitive data type is pre-defined by the programming language. The size and type of variable values are specified. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.

What are Java Non-primitive data types ?

The Java Non-primitive data types are not actually defined by the programming language but are created by the programmer. They are also called Reference Variables or Object References since they reference a memory location which stores the data. The Java non-primitive data types include Classes, Interfaces, and Arrays .

What are the 8 Primitive Data Types in Java ?

There are 8 Primitive data types in Java . The following list of Java Primitive Data Types showing their Range , Memory Size in Bytes and Default Value of each Data Type. The eight primitive data types in Java are :

Data Type Range Memory
(in bytes)
Default
Value
boolean true or false 1 bit false
byte -128 to 127 1 0
char any character or 0 to 65535 2 '\u0000'
short -32768 to 32767 2 0
int -2147483648 to 2147483647 4 0
float 1.40129846432481707e-45 to 3.40282346638528860e+38 4 0.0f
long -9223372036854775808 to 9223372036854775807 8 0L
double 4.94065645841246544e-324d to 1.79769313486231570e+308d 8 0.0d

What is the difference between primitive and non primitive data types in Java ?

The difference between primitive and non-primitive data types are as follows :

Primitive Data Type Non-Primitive Data Type
Primitive data types are predefined data types in Java. Non-primitive data types are created by the user and is not defined by Java.
Primitive types cannot be used to call methods to perform certain operations. Non-primitive types can be used to call methods to perform certain operations.
The primitive data type has always a value. Non-primitive types can be null.
The primitive type starts with a lowercase letter as they are keywords in Java. The non-primitive types starts with an uppercase letter as they are objects in Java.
The size of a primitive type depends on the data type. The non-primitive types have all the same size. Since they reference a memory location which stores the data.

What is a boolean Data Type in Java ?

boolean is a primitive data type in Java . boolean means true or false i.e. logical true or false . It occupies 1 bit in memory . These values do not correspond to 1 or 0 as in C or C++ . The following example shows how to use boolean data type in Java .

The boolean Primitive Data Type in Java Programming Language

  1. The boolean data type represents only 1 bit in size.
  2. The boolean is a primitive data type in Java .
  3. The boolean data type is used for logical values .
  4. The boolean data type can have two possible values true or false .
  5. The boolean is the type returned by all relational operators
  6. The boolean is the type required by the conditional expressions used in control statements such as if, for and while so on .
  7. Boolean is wrapper class for boolean primitive data type .
  8. The boolean primitive data type is also used in creating boolean array .

How many ways to use boolean value in Java ?

Java boolean Data Type Example

/*  Java boolean Data Type Example
    Save with file name BooleanDataType.java  */
   
public class BooleanDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF boolean IS false
    boolean b;
    System.out.println("Java boolean Data Type Example");
    //ASSIGN A VALUE
    b = true;
    System.out.println("Assigned boolean Value : " + b);
  }
}

What is a byte Data Type in Java ?

byte is a primitive data type in Java . byte data type holds numeric value between -128 and 127 . Java Byte data type is used to save space in arrays . The following example shows how to use byte data type in Java .

Java byte Data Type Example

/*  Java byte Data Type Example
    Save with file name ByteDataType.java   */
   
public class ByteDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF byte IS  0
    byte b;
    System.out.println("Java byte Data Type Example");
    //ASSIGN A VALUE
    b = 77;
    System.out.println("Assigned byte Value : " + b);
  }
}

What is a char Data Type in Java ?

char is a primitive data type in Java . char data type holds any character or 0 to 65535 in it . char is short for character in Java . It is 16 bits in size . The actual data stored in the char data type does not take up more than 8 bits . The following example shows how to use char data type in Java .

Java char Data Type Example

/*  Java char Data Type Example
    Save with file name CharDataType.java   */
   
public class CharDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF char IS '\u0000'
    char c;
    System.out.println("char Example");
    //ASSIGN A VALUE
    c = 'A';
    System.out.println("Assigned char Value : " + c);
    //ASSIGN small a ascii value
    c = 97;
    System.out.println("Assigned char ASCII Value : " + c);
  }
}

What is a short Data Type in Java ?

short is a primitive data type in Java . short data type holds numeric value between -32768 and 32767 . The short data type is a 16-bit signed two's complement integer. You can use a Java short to save memory in large arrays, in situations where the memory savings actually matters.

In Java, the short data type is the smallest type compared to int and long . The following example shows how to use short data type in Java .

Java short Data Type Example

/*  Java short Data Type Example
    Save with file name ShortDataType.java  */
   
public class ShortDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF short IS 0
    short s;
    System.out.println("Java short Data Type Example");
    //ASSIGN A VALUE
    s = 123;
    System.out.println("Assigned short Value : " + s);
  }
}

What is a int Data Type in Java ?

int is a primitive data type in Java . int data type holds numeric value between -2147483648 and 2147483647 . The Java int data type is a 32-bit signed two's complement integer . Java int data type stores whole numbers, positive or negative such as 786 or -1234, without decimals. The following example shows how to use int data type in Java .

Java int Data Type Example

/*  Java int Data Type Example
    Save with file name IntDataType.java    */
   
public class IntDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF int IS 0
    int i;
    System.out.println("Java int Data Type Example");
    //ASSIGN A VALUE
    i = 5;
    System.out.println("Assigned int Value : " + i);
  }
}

What is a float Data Type in Java ?

float is a primitive data type in Java . float data type holds numeric value between 1.40129846432481707e-45 and 3.40282346638528860e+38 . The following example shows how to use float data type in Java .

Java float Data Type Example

/*  Java float Data Type Example
    Save with file name FloatDataType.java  */
   
public class FloatDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF float IS 0f
    float a;
    System.out.println("Java float Data Type Example");
    //ASSIGN A VALUE LIKE THIS COMPILER SHOW ERROR
    //POSSIBLE LOSS OF PRECISION FOUND DOUBLE REQUIRED FLOAT
    //a = 5.25; //GET COMPILATION ERROR COMPILER ASSUMES double
    a = 5.25f; //ASSIGN A float VALUE
    System.out.println("Assigned float Value : " + a);
  }
}

What is a long Data Type in Java ?

long is a primitive data type in Java . long data type holds numeric value between -9223372036854775808 and 9223372036854775807 . The following example shows how to use long data type in Java .

Java long Data Type Example

/*  Java long Data Type Example
    Save with file name LongDataType.java   */
   
public class LongDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF long IS 0L
    long a;
    System.out.println("long Example");
    //ASSIGN A  VALUE
    a = 45000L;
    System.out.println("Assigned long Value : " + a);
  }
}

What is a double Data Type in Java ?

double is a primitive data type in Java . double data type holds numeric value between 4.94065645841246544e-324d and 1.79769313486231570e+308d . The following example shows how to use double data type in Java .

Java double Data Type Example

/*  Java double Data Type Example
    Save with file name DoubleDataType.java */
   
public class DoubleDataType
{
  public static void main(String args[])
  {
    //TYPE DECLARATION DEFAULT VALUE OF double IS 0d
    double a;
    System.out.println("double Example");
    //ASSIGN A double VALUE
    a = 5.25d;
    System.out.println("Assigned double Value : " + a);
  }
}
Previous
Next