03.05. Primitive data types

Primitive Data Types

  • Eclipse: Oxygen
  • Java: 1.8

Java supports eight primitive data types.  These are the basic and predefined data type of the programming language.  Java determines the size of each primitive data types, it cannot be changed. Primitive data types are represented by reserved keywords.

The primitive data type is divided into the following categories:

  • Integer data type.
  • Floating Point data type.
  • Boolean data type.
  • Char data type.

Integer data type

Integer data type is used to store integer numbers (number without a decimal point). The minimum and maximum value that a datatype can store depends on the size of the data type.

The range of Integer data types

The following table lists represent the all integer data types, their storage requirements in bytes and the numeric range they support.

Types Size Minimum value Maximum Value
byte 8 bit/1byte -128 127
short 16bit/2byte -32768 32767
int 32 bit/4byte -2,147,483,648 2,147,483,647
long 64 bit/8byte -9,223,372,036,854,775,808 9,223,372,036,854,775,807
  • Depending upon the requirement we must choose the appropriate data type.

byte

The size of the byte type is 8bit/1byte. The minimum value of the byte is -128 and the maximum value is 127.

short

The short data type is integer compatible data type and used to contain integer value or an integer number. The size of the short type is 16bit/2byte. The minimum value of the short is -32768 and the maximum value is 32767.

int

The size of the int value is 32bit/4byte. The minimum value of int is -2147483648 and the maximum range is 2147483647.

long

The size of the long data type is 64bit/8byte. While declaring long data type always use suffix “L” by default Java considered it as an integer. Java is a case-sensitive language so it is recommended use “L” in upper case.

 Initialization of long variable:

long longMax = 9223372036854775807L;

long longMin = -9223372036854775808L;

  • L specifies that value is a long.

Floating Point data type

Floating point variables are used to deal with decimal value. There are two types of floating point data types. The following table gives you details about floating point types with storage sizes and ranges of values with their precision.

Type        Storage size Value range Precision
Float 4 byte 1.2E-38 to 3.4E+38 7 decimal places
Double 8 byte 2.3E-308 to 1.7E+308 16 decimal places

 

In the program, whenever we want to store a floating-point value in a variable, we can use one of these data types accordingly.

Floatfloat is used for floating-point (decimal) values, it occupies 32-bit memory

     Initialization of float variable:

  • f specifies that value is a float.

Precision in float:

Output:

1.6666666

Double – double is used for high precision floating-point values, it occupies 64-bit memory

Initialization of double variable:

  • d specifies that the value is double.

Precision in double data type is more than a float data type.

 Precision in double:

Output:

1.6666666666666667

Boolean

Boolean is a data type that represents one bit of information either true or false.

Output: true

Char

A char data type is a single-length entity. This could be an alphabet, a digit, or a symbol. It is also used to hold the Unicode for symbols.  Value for char should be with single quotes.

Output:

§

A

Note: We must only use the single quotes pointing to left

  • The length should be one.
  • Single quote point to the left should be used
  • Any character on the keyboard is allowed

Example: ‘A’,’*’,’1’

Contributed by: Poonam Tomar

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments