Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 2.37 KB

07. Data Types ( Primitive ).md

File metadata and controls

56 lines (42 loc) · 2.37 KB

07. Data Types ( Primitive )

Eight Primitive Data types:

  • Integers ( byte, short, int, long )
  • Floating point ( double, float )
  • Characters ( char )
  • Boolean ( boolean )

4 bit = 1 Nibble, 8 bit = 2 Nibble = 1 Byte

Type Storage (byte) Range ( Inclusive ) specifier Default value
byte 1 -2^7 to 2^7 – 1 (-128 to 127) 0
short 2 -2^15 to 2^15-1 (-32768 to 32767) 0
int 4 -2^31 to 2^31 -1 (– 2,147,483,648 to 2,147,483,647 ) 0
long 8 -2^63 to 2^63-1 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) L or l 0
float 4 1.40239846 x 10^-45 to 3.40282347 x 10^38
Real numbers up to 7 decimal digits. f or F 0.0f
double 8 4.9406564584124654 x 10^-324 to 1.7976931348623157 x 10^308
Real numbers up to 15 decimal digits. d or D 0.0
char 2 (16 bits) ‘\u0000’ (0) to ‘\uffff’ (65535) \u000 (0)
boolean true , false false
  1. byte byte b;
  2. short short s;

The byte and short types are automatically promoted to int type when the expressions are evaluated.

  1. int int a;

  2. long long t;

    • to specify long literals, explicitly tell the compiler that the literal value is of type long by appending an upper or lower case L.
  3. float float temp;

    • it is useful when you need a fractional component, but do not require a large degree of precision. for example, float can be useful when representing dollars and cents.

    we should always end the float type value with f or F, otherwise, the compiler considers it as a double value. E.x: float myFloat = 256.8f ;

  1. double double pi;
    • it is useful when you need to maintain accuracy over many iterative calculations, or are manipulating large valued numbers.

Floating point numbers (floats and doubles) are represented internally in Java in binary. The result is that many (base-10) decimals are not precisely represented as a float or double.

For this reason, it is never safe to test whether two floating point numbers are equal.

  1. Characters char code;

    • Java uses unicode to store characters. Unicode defines fully international character set that can represent all of the characters found in all human languages. Unicode requires 16 bits thus char is a 16-bit type.
  2. Boolean boolean isSet;