You are on page 1of 4

Primitive Data Types

Data type – used to specify the set of values and their operations. The values have particular data types because they are
stored in memory in the same format and have the same operations defined for them.
Primitive data types – the fundamental data types in Java. These are predefined data types by the language and named by
a keyword.
Java support eight (8) built-in primitive data types shown in Table 1. The hierarchy of integer data type and floating-point
numbers data type are from the lowest precision to highest precision.
Table 1: Java Primitive Data Types
Keyword Description Range of Values Default
byte 8-bit signed integer -128 to 127 0
short 16-bit signed integer -32,768 to 32,767 0
int 32-bit signed integer -231 to 231-1 0
long 64-bit signed integer -263 to 263-1 0L
float Single-precision 32-bit IEEE 1.40239846x10-45 to 3.40282347x1038 0.0f
754 floating-point number
double Double-precision 64-bit IEEE 4.9406564581246544x10-324 to 1.79769313486231570 0.0d
754 floating-point number x10-308
boolean One (1) bit of information true or false false
char Single 16-bit Unicode ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive) ‘\u0000’
character (UTF-16 Table)

 byte – The byte data type can be useful for saving memory space in large arrays. Example variable declaration:
byte b = 100;
 short – The short data type is also useful for saving memory space in large arrays. Example variable declaration:
short s = 1400;
 int – This data type is generally used as the default data type for integral values unless there is a concern about
memory space. Example variable declaration: int a = 12400;
 long – This data type is used when a wider range than int is needed. Example declaration: long a = 124000L;
 float – The float data type is also used to save memory in large arrays of floating-point numbers.
Example variable declaration: float f1 = 234.5f;
 double – For decimal values, this data type is generally the default data type. Example variable declaration:
double d1 = 234.5d; or double d1 = 234.5;
 boolean – The boolean data type has only two (2) possible values: true or false. This is used for simple
flags that track true/false conditions. Example variable declaration: boolean isCorrect = true;
 char – The char data type is used to store a single character. In Java, the character is enclosed in single
quotes. Example variable declaration: char letter = ‘A’;
Java programming language also provides special support for character strings using the java.lang.String class. The
String class is not technically a primitive data type.
Constants and Variables
Variable is a name for a memory location that stores a specific value, such as numbers and letters. A variable is an
identifier. It can hold only one (1) value at a time, but its value may change during program execution. For example, a
created variable named score holds a value of 0 when the program starts and was altered during program execution to
hold the value of
350. To use a variable, it must be declared first. The basic form of a variable declaration is:
data_type variable_name [= value];
The data_type is one (1) of Java’s data types and variable_name is the name of the variable. To declare more than one
(1) variable of the specified type, use a comma-separated list. The following are some valid examples of variable
declaration and initialization in Java:
int a, b, c; int x = a + 5; float f = 2.75f; double grade
int a = 10, b = 5 + 3; = 2.75; char letter = ‘A’;
byte b;
int size; size = 30;
Java variables are implemented as memory locations. Each variable is assigned one (1) memory location. When the variable
is given a value, the value is enclosed as a string of 0s and 1s and is placed in the variable’s memory location.
Constant – it is a memory location whose value cannot be changed during program execution.
When a created variable is stored in the memory, Java uses a named constant to instruct a program to mark the memory
location as constant throughout program execution. To allocate memory, use Java’s declaration statements. The syntax to
declare a named constant is:
final data_type variable_name = value;
The final is a Java reserved word. This keyword is used to specify that the value stored in the variable_name is fixed
and cannot be changed. The following are some valid examples of named constant declaration and initialization in Java:
final double PI = 3.14159;
final int SQUARE_SIDES = 4;
final char BLANK_SPACE = ‘ ’;
final int HOURS_PER_DAY; HOURS_PER_DAY = 24;
A named constant can be assigned a value only once.
Type Casting
Type casting – this refers to converting a value from a specific type to a variable of another type (note: booleans cannot be
converted to numeric types).
There are two (2) types of conversion:
 Widening conversion (implicit casting) – the conversion of the lower precision data type to a value of a higher
precision data type. This causes no loss of information, and the casting will be performed by the Java Virtual
Machine (JVM) implicitly or automatically.

Figure 1: Order of lowest precision to highest precision (Savitch, 2014)


For example, an int variable x with value 4 to a higher-order double data type without loss of information
int x = 4;
double a = x; //the value of variable a is 4.0
In Java, the following widening conversions are possible:
o From byte to short, int, long, float, or double
o From short to int, long, float, or double
o From char to int, long, float, or double
o From int to long, float, or double
o From long to float or double
o From float to double
 Narrowing conversion (explicit casting) – the conversion of a higher precision data type into a value of a lower
precision data type. This will typically involve loss of information.
The following is an example of Java’s strict type checking. The code will not compile, and an error will be generated:
float x = 6.82f;
int a = x; //cannot be converted or assignment is illegal
The casting is not done by JVM and should be made explicit by the programmer through a cast operator. The cast
operator takes the following form:
(data_type_name) expression.
First, the expression is evaluated. Its value is then treated as a value of the type specified by data_type_name.
The following is the example of explicit casting:
float x = 6.82f;
int a = (int) x; //the value of variable a is 6
The conversion is done from higher-order data type to lower-order data type as follows:

Figure 2: Order of highest precision to lowest precision (Savitch, 2014)


In Java, the following narrowing conversions are possible:
o From byte to char
o From short to byte, or char
o From char to byte or short
o From int to byte, short, or char
o Form long to byte, short, char, or int
o From float to byte, short, char, int, or long
o From double to byte, short, char, int, long, or float
When using the cast operator to treat a floating-point number as an integer, the decimal part of the floating-point
number is dropped. The following examples show how cast operators work:
int a = (int) 7.9; //the return value is 7
int b = (int) 3.3; //3
double c = (double) 25; //25.0
double d = (double) (5 + 3) //8.0

REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7 th edition. California: Pearson Education,
Inc.

You might also like