July 1, 2022

Java Primitive Data Types

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.
Java Primitive Data Types

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.

public class Test {
  public static void main(String[] args) {
    //Error - range is -128 to 127	
    //byte b = 145;
    byte b = 124;
    System.out.println("Value- " + ++b);
  }
}
Output
Value- 125

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.

float f = 4.678f;
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.
public class Test {
  public static void main(String[] args) {		
    float f = 0.72f;
    float result = 1 - f;
    System.out.println("Value- " + result);	
  }
}
Output
Value- 0.27999997

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.

public class Test {
  public static void main(String[] args) {	
    boolean b = false;
    if(!b){
      System.out.println("I am here!");
    }
  }
}
Output
I am here!

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'.

public class Test {
  public static void main(String[] args) {
    char c = 'a';
    char ch = '\u0061';		
    System.out.println("Value- " + c);
    System.out.println("Value- " + ch);
  }
}

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 Java Primitive Data Types. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment