Java Fundamentals  «Prev  Next»
Lesson 4Examining Java Data Types
ObjectiveUse data types to represent different types of information in Java.

Examining Java Data Types

Variables are locations in memory that are used to store information.
Java supports a variety of data types, which dictate the type of information that can be stored in a variable.
You create a variable in Java by specifying the type of the variable and an identifier that uniquely identifies the variable. Let us revisit an earlier example:

int age = 55;
String name = "Ernie";

The first line of code declares a variable named age whose data type is int, which is an integer (whole number) data type. The second line of code declares a variable named name whose data type is String, which holds text. Both of these variable declarations set aside memory for each variable. The data type determines the amount of memory set aside. For example, a Java int is a 32-bit number, so 32 bits of memory are set aside for an int variable. Java data types can be grouped into two major categories:
  1. simple: a core data type that is not derived from any other type and represents a single piece of information (also called a primitive data type)
  2. composite: a data type based on simple types that is used to represent more complex information

Figure 6.4:  Categorization of primitive data types
Figure 6.4: Categorization of primitive Data Types


The simple (primitive) Java data types include
  1. Integer numbers,
  2. Floating-point numbers,
  3. booleans, and
  4. char Literal

  1. Integer numbers: Whole numbers without fractional parts (byte, short, int, long)
  2. Floating-point numbers: Numbers with fractional parts (float, double)
  3. Booleans: Values with one of two possible states: true or false
  4. Characters : Individual text characters (char)

Floating-point numbers: float and double

You need floating-point numbers where you expect decimal numbers. For example, can you define the probability of an event occurring as an integer? No, probability is expressed as a real value between 0 and 1, (0,1).
Table 6.4 lists probable scenarios in which the corresponding data is stored as a floating-point number.

Data that is stored as floating-point numbers
Figure 6:4: Data that is stored as floating-point numbers

Store Decimal Numbers

In Java, you can use the float and double primitive data types to store decimal numbers. float requires less space than double, but it can store a smaller range of values than double. float is less precise than double and cannot accurately represent certain numbers, even if they arein range. The same limitation applies to double, even if it is a data type that offer more precision.
Figure 6.5 lists the sizes and ranges of values for float and double.

Data that is stored as floating-point numbers
Figure 6.5: Range of values for decimal numbers


Sample code of Float assignment:

float average = 20.123F;
float orbit = 1763.67f;
double inclination = 119.1750;

Integral and Floating Point Data Types

1) Integer numbers
1) Integer numbers: Whole numbers without fractional parts (byte, short, int, long)

2)Floating-point numbers: Numbers with fractional parts (float, double)
2) Floating-point numbers: Numbers with fractional parts (float, double)

3) Booleans: Values with one of two possible states: true or false
3) Booleans: Values with one of two possible states: true or false

4)Characters : Individual text characters (char)
4) Characters: Individual text characters (char)

Java Basics - Quiz


Take a quiz to test your knowledge of Java programming basics.
Java Basics - Quiz