Table 1: Java Primitive Data Types
Table 1: Java Primitive Data Types
• 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; float f = 2.75f; byte b;
int a = 10, b = 5 + 3; double grade = 2.75; int size; size = 30;
int x = a + 5; char letter = ‘A’;
03 Handout 1 *Property of STI
Page 1 of 3
IT1708
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.
The conversion is done from higher-order data type to lower-order data type as follows:
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, 7th edition. California: Pearson Education,
Inc.