Primitive Data Types

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

Objectives (topics)

Topic Notes
Data Types Java has 2 categories of data types:  Objects or Primitives

Primitive - a place in memory that stores a value.
Object
- a place in memory that can store data & methods.  An object is an instance of a class.  Use the keyword "new" to create an object from a class.

Everything in Java is either an object or a primitive.

Primitive - Bit Sizes

 

Formula for Integers Min/Max values.  (On the test!)
  • Min value:  -1 * ( 2^(#_of_bits - 1) )
    Ex: byte's Minimum value 
    -1 * ( 2^(8-1) )
    -1 * ( 2^7 ) 
    -1 * 128
    -128
  • Max value:  2^(#_of_bits - 1) - 1
    Ex: byte's Maximum value 
    ( 2^(8-1) ) -1
    ( 2^7 ) -1
    128 -1
    127

The info in the table is on the test.  Remember byte, char, & boolean min/max value!  You can skip the # values for short through double. 

Primitive Bits Type Min/Max
byte 8
signed integer -128 
127
short 16 signed integer -32,768
32,767
int 32 signed integer -2,147,483,648
2,147,483,647
long 64 signed integer -9,223,372,036,854,775,808L
9,223,372,036,854,775,807L
float 32 signed floating-point 3.4 E 38
(6-7 digits of accuracy)
double 64 signed floating-point 1.7 E 308
(14-15 digits of accuracy)
char 16 Unicode '\u0000'
'\uffff'
(ASCII - '\u0000' to '\u00ff')
boolean 1 logical false
true

 

Default data types for numbers Default data types:
integers - int
floating points - double
Data Type initial values. Instance variables - get initialized with default values.
Static variables - get initialized with default values.
Local variables - must be initialized before they are used.

Instance & Static Variables default values.

Data Type Instance default initial value
byte 0
short 0
int 0
long 0L
float 0.0F
double 0.0D
char '\u0000'  (note: not a space ('\u0020') !)
boolean false
object reference null
Exceptions Integers - throw exceptions
Floating-point - never throw exceptions

Divide by 0:
Integer - throws ArithmeticException
Floating-point - returns 'infinity'.  Does not throw an exception.   

Any arithmetic expression involving a NaN yields NaN.   NAN = "not-a-number"

Casting Casting is used to force an explicit conversion of data from one type to another. 

2 types of casting

  • Implicit - to imply a cast is made.
  • Explicit - to explicitly show the cast. Use "()".

Remember:  When casting up and down the hierarchy, you can implicitly cast up but you must explicitly cast down.

You can not implicitly coerce a data type with greater accuracy into a data type with less accuracy.  Therefore, if loss of data is possible when you are converting to another data type, then you must insert a cast or the compiler will complain.

Legal statements

  • float myFloat = 40.0F;  // (or (float) 40.0 )
  • char myChar = (char) 65;  // cast integer to char
  • int myInt = (int) 'A';   // cast char to int
  • int myInt = 10; byte myByte = (byte) myInt;

Illegal statements

  • float myFloat = 40.0;        // double is default!
  • boolean b = (boolean) 1;   //cannot cast a boolean
Not on the Test
  • infinity - 10.0/0 = infinity
  • NaN - "not-a-number".  Any arithmetic expression involving a NaN yields NaN. 
    Ex: 0.0/0.0 = NaN