Data Types

Objectives (topics)

  2 Basic data types: Objects or Primitives
  Primitive Bit Sizes
  Default data types for numbers
  Literal
  Default Types & Initial Values (Instance vs Local)
  Bases
  Exceptions
  Casting
  Wrapper Classes
  Arrays
  Special Escape Sequences

 

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

Primitive - a place in memory that stores a value like a variable in a procedural language.

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:  2^(#_of_bits - 1)
  • Max value:  2^(#_of_bits - 1) - 1

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
Literal 10L or 10l  = 10 as a long
10.0D or 10.0d = 10 as a double
10.0F or 10.0f = 10 as a float

Integers - default data types & casting:

  • 10 - is an int, unless casted.
  • (byte) 10 - is a byte
  • 10L - is a long

Floating Points - default data types & casting:

  • 10.0 - is a double, unless casted.
  • (float) 10.0 - is a float
  • 10.0F  - is a float
  • 10.0D - is a double

Exponents:

  • 10E3  = 10 * 1000 = 10,000  (10e3 - also works)
  • 10.5E4 = 10.5 * 10,000 = 105,000
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
Bases This course does not teach the fundamentals of bases.

Integer Bases:

  • 1 - Decimal-base10   (ex: int MyInt = 1;)
  • 01 - Octal-base8 (leading zero)  (ex: int MyInt = 01;)
  • 0x1 - Hexadecimal-base16 (leading 0x)  (ex: int MyInt = 0x1;)
  • '\u0000' - Unicode base 16
  • '\u000' - Unicode base 8

Example of:   ? base = base 2 = base 10

  • 10 base 10 = 1010 base 2 = 10 base 10
  • 010 base 8 = 1000 base 2 = 8 base 10
  • 0x10 base 16 = 0001 0000 base2 = 16 base 10

Legal statements:

  • int test = 10;                    //Base 10 to int
  • int test = 010;                  //Octal to int
  • int test =0x10                  //Hex to int
  • double d = 0x12345678;    //Hex to a double

Examples:

  int oi = 012; System.out.println(oi);  // shows 10.
Exceptions Integers - throw exceptions
Floating-point - never throw exceptions

Divide by 0:
Integer - throws ArithmeticException
Floating-point - returns 'infinity'.  Does not throw an exception.   (see pg 123 of Barry Boone for a chart)

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 down but you must explicitly cast up.

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
Wrapper classes A wrapper is a class that contains data or an object of a specific type and has the ability of performing special operations on the data or object.   Most common wrapper classes are for the primitives.

Reasons for wrapper around the primitives:

  • Converting from character strings to numbers and then to other primitive data types.
  • A way to store primitives in an object.

Primitive and the Wrapper Class: (ex: byte - Byte)
Integers: byte-Byte; short-Short; int-Integer; long-Long
Decimals: float-Float; double-Double;
Others: char-Character; boolean-Boolean

ex:  Double MyDouble = new Double("10.5");

Arrays Arrays
  • Arrays are objects.
  • Java initializes each element in an array to its default value when the array is created.  Object arrays are initialized with "null"
  • The first element in an array is element (index) 0.  The last element is the length - 1.
  • Can use {} to initialize an array only when the array is declared.
  • [][] declares a nested array.  The number of brackets determines the level of array nesting.
  • In the declaration, [] may come before or after the array's name. 
    Ex:  int [] myArrayA; int myArrayB [];
    Ex:  int [] [] myArrayC; int myArrayD [] [];
  • Determine the length of any array with:  array_name.length;
    Ex:  int [] myArray = new int[5]; 
    Ex:  System.out.println("Length = "+ myArray.length);  //shows Length = 5.
  • Once an array is created with the keyword "new" the array can not be resized.
Array, declaration Declaration Examples
int [] myIntArray;  //Array of int's.
MyObjects [] MyObjectArray;  //Array of objects
int [] myIntArray = new int[5];  //Initialize & create an array of integers with 5 Rows
int myInt = 5; int [] myIntArray = new int[myInt];  //5 Rows
int k[] = new int[] {0,1,2,3,4};
int k[] = {0,1,2,3,4};
String [] myString = {"Hello", "Michael"};  // 2 rows
String [] [] myString = { {"1","2"}, {"10","11"} }; // 2 by 2

Compile Errors:

  int k[]= new int[5] {0,1,2,3,4};  // see [5], error with {}.
Array declaration & initialization Declaration of a Array - to declare that an array will contain a certain data type.  The array does not exist in memory yet.

Initialization of an Array - to create an array object in memory.  If data is passed via the {} identifier then the array is populated with the elements passed, else the array is populated with a default value.  Default value for Data Types that extend Object is the null value.  Defaults for primative integers is 0, floating point is 0.00, boolean is false, and char is \u0000 .

Declaration only:  int k [];
Initialization only:   k = new int [3]
Initialization only:   k = new int [intSize]
Declaration & Initializationint k = {0,1,2}

Declaration must always come before initialization.

Remember this, you can not use the {} to load an array once the array is initialized.  That's why you can only use the {} in the initialization of the array.

Also, int k[]= new int[ 5 ] {0,1,2,3,4}; is a compile error because the array was initialized as of:  int k[]= new int[ 5 ]

Use the {} in the initialization of an array when you know what elements will reside in the array. 
Ex:  int k [] = {0,1,2,3,4}

Use the [] when you don't know how many elements the array will have at the time of declaration.  Ex:  int k [];   Later you can initialize the array with k = new int [intArraySize]

Use the [5] when you know how many elements the array will have at the time you declare and initialize the array in one statement.  Ex int k [] = new int [5]

Array, creating
  Use the keyword new to create an array.
Special Escape Sequences

Some of these may be on the test.

Escape code Desc Unicode
\n newline '\u000A'
\t tab '\u0009'
\b backspace '\u0008'
\r return '\u000D'
\f form feed '\u000C'
\\ backslash '\u005C'
\' single quote '\u0027'
\" double quote '\u0022'
'\ddd' octal char  
'\udddd' Unicode char  
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