Java Data Types

A variable in java must be specified with data type that can be store different sizes and values. In Java there are two types of data types.

  • Primitive Data Types
  • Non-Primitive Data Types

Primitive Data Types

The Primitive Data type are predefined data types. There are 8 types of primitive data types,

Data TypeSizeDefault ValueMin and Max value
byte1 byte0-128 to 127
short2 byte0-32,768 to 32,767
int4 byte0-2,147,483,648 to 2,147,483,647
long8 byte0L-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
char2 byte‘\u0000’single character/letter or ASCII values
float4 byte0.0fupto 7 decimal digits
double8 byte0.0dupto 16 decimal digits
boolean1 bitfalsetrue or false
Primitive Data types

Byte

Instead of integer data type, byte data type is used to save memory. Byte data type is an 8 bit signed two’s complement integer. The minimum value of byte data type is -128 and maximum value is 127.

Example:

class byte_example
{
public static void main(String[] args)
{
byte val;
val = 110;
System.out.println(val);
}
}

Output:

110

Short

Short data type is an 16 bit signed two’s complement integer. The maximum value for short data type is 32767 and the minimum value is -32768. The default value is 0.

Example:

class short_example
{
public static void main(String[] args)
{
short val;
val = 2000;
System.out.println(val);
}
}

Output:

2000

int

Integer data type is a 32-bit signed two’s complement integer. The maximum value for integer data type is 2,147,483,647 and the minimum value is -2,147,483,648.

Example:

class int_example
{
public static void main(String[] args)
{
int val;
val = 300000;
System.out.println(val);
}
}

Output:

300000

long

long data type is a 64-bit signed two’s complement integer. The maximum value for long data type is 9,223,372,036,854,775,807 and the minimum value is -9,223,372,036,854,775,808.

Example:

class long_example
{
public static void main(String[] args)
{
long val;
val = 900000000;
System.out.println(val);
}
}

Output:

900000000

Char

char data type is a single 16 bit Unicode character/letter or ASCII values.

Example:

class char_example
{
public static void main(String[] args)
{
char val;
val = 'B';
System.out.println(val);
}
}

Output:

B

Float

float data type is a single-precision 32 bit floating point. It can have upto 7 decimal digits.

Example:

class float_example
{
public static void main(String[] args)
{
float val;
val = 12.787783f;
System.out.println(val);
}
}

Output:

12.787783

double

double data type is a double-precision 64 bit floating point. It can have values upto 16 decimal digits.

Example:

class double_example
{
public static void main(String[] args)
{
double val;
val = 23.778675587783d;
System.out.println(val);
}
}

Output:

23.778675587783

boolean

boolean data type represents only one bit of information. It has two possible values that’s is either true or false.

Example:

class boolean_example
{
public static void main(String[] args)
{
boolean val = true;
System.out.println("Java is used to develop Android Applications:" + " " + val);
}
}

Output:

Java is used to develop Android Applications: true

Non-Primitive Data Type

Non-Primitive Data Type is also known as Reference Data Types. It contain memory address of variable value because it won’t store variable values directly in memory.

non-primitive types starts with an uppercase letter. Reference Data Type is used to access objects. The default value is null.

Example of non-primitive data type is strings, objects, arrays, Classes, Interface, etc.

More Reading

Post navigation

14 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *