Java Primitive Data Types

8 Primitive Data Types in Java:
  • Integral - byte, short, int and long
  • Floating Point - float and double
  • Textual - char
  • Logical - boolean

Integral Primitive Types - No Decimal Points:

byte - normally used to save memory consumption:

  • Length is 8 bits
  • Range is -27 to 27 -1 (-128 to 127, or 256 possible values)

short - normally used to save memory consumption:

  • Length is 16 bits
  • Range is -215 to 215 -1 (-32,768 to 32,767, or 65,535 possible values)

int - default integral type:

  • Length is 32 bits
  • Range is -231 to 231 -1 (-2,147,483,648 to 2,147,483,647 or 4,294,967,296 possible values)

long - value ends with a capital L to indicate it is a long value:

  • Length is 64 bits
  • Range is -263 to 263 -1(-9,223,372,036854,775,808 to 9,223,372,036,854,775,807, or 18,446,744,073,709,551,616 possible values)

Floating Point Primitive Types - wtih Decimal Points:

float - value ends with a capital F to indicate it is a float value:

  • Length is 32 bits
  • Example is public float price = 0.0F;

double - default floating point type

  • Length is 64 bits
  • Example is public double price = 0.0;

Textual Primitive Types:

char - stores single-character value:

  • Length is 16 bits
  • Example is char ShoeColour = 'b';

Logical Primitive Type:

boolean - stores true or false

  • Used to store the Java programming language literals or the results of an expression

Choosing a Data Type:

  • Numeric Variables normally use int, long or double Data Types.

0 comments:

Post a Comment