Java Data Types

data types in java

Java Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:

Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.

Non-primitive data types:The non-primitive data types include Classes, Interfaces, and Arrays.


Java Primitive Data Types:

In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available in Java language.

There are 8 types of primitive data types:

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

  Data Type Default Value   Default size
booleanfalse1 bit
char‘\u0000’2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte


Primitive Data Types:

Byte:

  • Byte data type is an 8-bit signed two’s complement integer
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
  • Example: byte a = 100, byte b = -50

short:

i. Short data type is a 16-bit signed two’s complement integer
ii. Minimum value is -32,768 (-2^15)
iii. Maximum value is 32,767 (inclusive) (2^15 -1)
iv. Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
v. Default value is 0.
vi. Example: short  s = 10000, short r = -20000


Int:

  1. Int data type is a 32-bit signed two’s complement integer.
  2. Minimum value is – 2,147,483,648 (-2^31)
  3. Maximum value is 2,147,483,647(inclusive) (2^31 -1)
  4. Integer is generally used as the default data type for integral values unless there is a concern about memory.
  5. The default value is 0
  6. Example: int a = 100000, int b = -200000

Long:

i. Long data type is a 64-bit signed two’s complement integer
ii. Minimum value is -9,223,372,036,854,775,808(-2^63)
iii. Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
iv. This type is used when a wider range than int is needed
v. Default value is 0L
vi. Example: long a = 100000L, long b = -200000L


Float:

  • This data type is a single-precision 32-bit IEEE 754 floating point
  • Float is mainly used to save memory in large arrays of floating point numbers
  • Default value is 0.0f
  • Float data type is never used for precise values such as currency
  • Example: float f1 = 234.5f

Double:

i. double data type is a double-precision 64-bit IEEE 754 floating point
ii. This data type is generally used as the default data type for decimal values, generally the default choice
iii. Double data type should never be used for precise values such as currency
iv. Default value is 0.0d
v. Example: double d1 = 123.4


Boolean:

  • boolean data type represents one bit of information
  • There are only two possible values: true and false
  • This data type is used for simple flags that track true/false conditions
  • Default value is false
  • Example: boolean one = true

Char:

i. char data type is a single 16-bit Unicode character
ii.Minimum value is ‘\u0000’ (or 0)
iii.Maximum value is  ‘\uffff’ (or 65,535 inclusive)
iv.Char data type is used to store any character
v.Example: char letterA = ‘A’


Spread the love

Leave a Comment

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