Primitive Data Types in Java

A primitive type is predefined by a programming language and is named by a reserved keyword. The primitive types represent single values not an object and primitive values do not share state with other primitive values. There are eight primitive data types in Java which are as follows-

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

These primitive types in Java can be classified into four groups-

  1. Integers– Primitive types that fall with in this group are byte, short, int, and long. All of these primitive types represent whole-valued signed numbers.
  2. Floating-point numbers– Primitive types included with in this group are float and double representing number with fractions.
  3. Characters– This group includes char whose values are 16-bit Unicode characters.
  4. Boolean– This group includes boolean primitive type with value as either true or false.

Primitive data types in Java

byte– The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays.

Output

short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

int: The int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1 (i.e. -2,147,483,648 to 2,147,483,647). In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Wrapper class Integer provide methods to use int data type as an unsigned integer.

long: The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int.

float: The float data type is a single-precision 32-bit IEEE 754 floating point.

With floating data type numbers you need to use suffix f or F otherwise number would be considered as double.

This data type should never be used for precise values, such as currency. Fields of type float can be used when you need a fractional component, but large degree of precision is not required.

Output

As you can see value has lost some precision.

double: The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. Like float this data type should never be used for precise values, such as currency.

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions.

Output

char: The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).

To define a char you can either use a character enclosed in single quote or you can provide the unicode of the character.

For example, in the following program both char variables are assigned the char ‘a’.

Default values for primitive types

It’s not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler.
The following chart summarizes the default values for the primitive data types in Java.

Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char ‘\u0000’
boolean false

Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

That’s all for the topic Primitive Data Types in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.