Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Data Types and Casting 

 

Every variable must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it.

 

The Java programming language has two categories of data types: primitive and reference.

 

Primitive Data Types (ones we will use in the course)

 

Data Type

Minimum Value

Maximum Value

short

-32768

32767

int

-2147483648

2147483647

long

-9223372036854775808

-9223372036854775807

float

-3.4e+38

3.4e+38

double

-1.8e+308

1.8e+308

char

 

Reference Data Types

 

Objects, instantiations of classes, are reference data types.

 

Casting

 

This is a technique to change the data type of a primitive variable for the current operation only.

 

Example 1:


       double height = 60.45;

       System.out.println((int) height); would be used to print the integer portion of the height 

 

Example 2:

 

      int a = 10;

      int b = 3;

      System.out.println (a/b):       will print 3

      System.out.println ((double)a/b) will print 3.3333 

 

Example 3: