There are eight primitive data types in Java:byte, short, int, long, char, float, double, and boolean. These can be put in four groups:

  1. Integers : This group includes byte, short,int, and long, which are for whole-valued signed numbers.
  2. Floating-point numbers : This group includes float and double, which represent numbers with fractional precision.
  3. Characters : This group includes char, which represents symbols in a character set, like letters and numbers.
  4. Boolean : This group includes boolean, which is a special type for representing true/false values.

Primitive Data Types in Java

Integers

Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers. Many other computer languages support both signed and unsigned integers.

  1. byte : The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127. Variables of type byte are especially useful when you’re working with a stream of data from a network or file. ex : byte b, c;
  2. **short **: short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used Java type. ex : short sh;
  3. int : The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. ex : int a, b;
  4. long : long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. This makes it useful when big, whole numbers are needed. ex : long timestamp;

Floating Point Types

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision.

  1. float : The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. ex: float temperature;
  2. double : Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. ex : double largePrecisionValue;

Characters

In Java, the data type used to store characters is char.

  1. 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’ ex : char a, b;

Booleans

Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false.

  1. The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined. ex : boolean a, b;

Default Values for Primitive Data Types is Java

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. Generally speaking, this default will be zero or null, depending on the data type.

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