03-Data Types
03-Data Types
Objectives
Students should
• Be familiar with the eight primitive data types and non-primitive data types.
• Understand memory allocations and value assignments of variables.
• Be able to use operators and some methods to do some computations.
2. Classes
Primitive data types are data types considered basic to Java. They cannot be added or
changed. There are eight primitive data types in Java including 4 types for integer values, 2
types for floating-point values, 1 type for characters, and 1 type for logical values (true/false).
Classes are more complex data types. There are classes that are standard to Java such as
String, Rectangle, etc. Also, new classes can be defined by programmers when needed.
A byte occupies 1 byte of memory. It can represent one of 28 (256) integers in the range -128,
-127, …, 0, 1, …, 126, 127.
A short occupies 2 bytes of memory. It can represent one of 216 (65,536) integers in the range
-32,678, …, 0, 1, …, 32,767.
An int occupies 4 bytes of memory. It can represent one of 232 (4,294,967,296) integers in the
range -2,147,483,648, …, 0, 1, …, 2,147,483,647.
A long occupies 8 bytes of memory. It can represent one of 264 integers in the range -263, …, 0,
1, …, 264-1.
The default primitive type for integer value is int. Still, choosing to use suitable data types for
your integer value leads to efficient data processing and memory usage.
A float occupies 4 bytes of memory. Its range is from -3.4 × 1038 to 3.4 × 1038, with typical
precision of 6-9 decimal points.
A double occupies 8 bytes of memory. Its range is from -1.8 × 10308 to 1.8 × 10308, with typical
precision of 15-17 decimal points.
There are two ways to write floating-point values. They can be written in either decimal or
scientific notation. 123.45, 0.001, 1.0, 8.357, 1., and .9 are in decimal notation. 1.2345E2, 10E-4,
1E0, 0.8375E1, 1000E-3, and 9.0E-1 are in scientific notation representing 1.2345×102, 10×10-4,
1×100, 0.8375×101, 1000×10-3, and 9.0×10-1 respectively. The character E can also be replaced
with e.
Characters include alphabets in different languages (‘a’, ‘b’, ‘ก’), numbers in different
languages (‘1’, ‘2’, ‘๑’), symbols (‘%’, ‘#’, ‘{‘), and escape sequences (‘\t’, ‘\n’, ‘\”’);
In fact, the underlying representation of a char is an integer in the Unicode character set
coding. Characters are encoded in 2 bytes using the Unicode character set coding.
Unicode character set encoding guarantees that ‘0’ < ’1’ < ’2’ < … < ’9’, ‘a’ < ’b’ < ‘c’ < … < ‘z’,
and ‘A’ < ‘B’ < ‘C’ < … < ‘Z’. Also,
In Java, 0 does not mean ‘false’ and 1 does not mean ‘true’. Logical values and numeric values
cannot be interchangeably.
String
String is an example of a data type that is not a primitive data type. String is a standard class
in Java. It is used for representing a sequence of one or more characters. Double quotes are
used to designate the String type. For example, “ISE” is a data of String type. Be aware that
“100” is not the same as the integer 100, and “Q” is not the same as the character ‘Q’.
Variable types are specified when the variables are declared, using the name of the desired
data types followed by the name of the variables. Do not forget semicolons at the end.
For each variable whose type is one of the primitive data types, memory space of the size
corresponding to its type is allocated. The allocated space is used for storing the value of that
type. However, it works differently for a variable of a non-primitive type. The memory space
allocated for such a variable is for what is called a reference. When assigned with a value, the
reference points, or refers, to the memory location that actually stores that value.
We have discussed briefly about how to declare variables and assign values to them in the
last chapter. Here, we revisit it again with another example. This time, attention should be
paid on the data types.
To better illustrate the memory allocation and value assignments of variables, we add to the
following example code segment with some illustration of the allocated memory. Here, we
represent a variable by a box captioned with its name. When a value is assigned to a variable,
we write the value in side the box associated with that variable.
int x; x - 1
double d; x - d - 2
char c; x - d - c - 3
boolean b; x - d - c - b - 4
String s; x - d - c - b - s - 5
x = 256; x 256 d - c - b - s - 6
d = 1.5; x 256 d 1.5 c - b - s - 7
c = ‘Q’; x 256 d 1.5 c - b - s - 8
b = true; x 256 d 1.5 c ‘Q’ b true s - 9
s = “Computer” x 256 d 1.5 c ‘Q’ b true s 10
“Computer”
On the first four lines of the above code segment, a variable x of type int, a variable d of type
double, a variable c of type char, and a variable b of type boolean are created and allocated
with memory space of the size according to the type. On the fifth line, String s is the
declaration of a variable s of String type. On this line, a reference to String is created but it has
not referred to anything yet. Variables are assigned with values of the corresponding data
types on the last five lines. For the last line, the reference in s is made to point to the String
“Computer” located somewhere else in the memory.
int x,y = 8;
double z = 0.6, w;
double k=3.0;
w = k;
x = y;
The first two lines show how to declare two variables of the same type in one statement. Also,
they show how to assign values to variables at the same time as the variables are declared.
Note that, the integer value 8 is only assigned to y, not x, on the first line. On second line from
the bottom, the double value stored in k is assigned to the variable w. On the last line, the int
value stored in y is assigned to the variable x.
int i;
int k = 2;
int i = 6; // declaration of redundant variable i
Final Variables
Sometimes, we want to store a value that cannot or will not be changed as long as the
program has not terminated in a variable. The variable with such an unchangeable value is
referred to as a final variable. The keyword final is used when a variable is declared so that
the value of that variable cannot be changed once it is initialized, or assigned for the first
time. Programmers usually use all-uppercase letters for the identifiers of final variables, and
use underscore (_) to separate words in the identifiers (E.g.: YOUNG_S_MODULUS,
SPEED_OF_LIGHT). Attempting to change the value of a final variable after a value has been
assigned results in an error.
For example:
The following code segment will produce an error due to the assignment in the last line.
final int C;
int k = 6;
C = 80;
C = k + 300;
Un-initialized Variables
When a variable is declared, a space in the memory is reserved for the size of the type of that
variable. However, as long as it is not assigned with a value, the variable does not contain
any meaningful value, and it is said that the variable has not been intitialized. If the value of
an un-initialized variable is used, an error occurs.
The following code segment will produce an error due to the attempt to use an un-initialized
variable.
int x, y = 2;
System.out.println(x+y);
Operators
Values can be manipulated using built-in arithmetic and logic operators (such as +, -, *, /, %),
or using available methods (such as abs(), sqrt(), exp(), floor(), log(), getTime()). Many
methods are defined in some standard classes such as the Math class. Although you are
expected to be able to use the methods that have previously seen in this class, such as print()
and println(), we will defer the detailed discussion on using methods for now. In this chapter,
we will mostly discuss about using built-in operators and some small number of selected
methods to manipulate data.
Arithmetic operators that you should know is addition (+), subtraction (-), multiplication (*),
division (/), modulo (%), for which a%b returns the remainder if a÷b, and negation (-).
Logic operators that you should know is “logic and” (&&), “logic or” (||), and negation (!).
Portions of code that are either constant values, variables, methods, or any combinations of
the mentioned items from which some values can be computed are called expressions.
Operators that require two operands are called binary operators, while operators that require
only one operand are called unary operators.
Parentheses, (), are called grouping operators. They indicate the portions of the calculation that
have to be done first.
Values can be compared using relational equality operators, == and !=, which return either one
of the boolean values depending on the value of the operand. a==b yields true if and only if a
and b have the same logical value. a!=b yields true if and only if a and b have different logical
value. Comparison can also be done using <, >, <=, and >=.
Below are two examples showing how values of variables change due to value assignments
and the use of some operators.
int i = 2, j, k; i 2 j - k -
j = 3; i 2 j 3 k -
k = i + j; i 2 j 3 k 5
i = i + k; i 8 j 3 k 5
Whenever one of the operands of the addition operator is a String, the operator performs the
String concatenation. Thus, if a String is added with values of different data types, the
compiler will try to convert the values that are not String to String automatically. Good news
is that the automatic conversion usually returns the String that makes very much sense! E.g.:
numeric values will be converted to the Strings whose contents are consistent with the
original numbers.
int myNumber = 3 + 2 * 6;
Each operator is assigned a precedence level. Operators with higher precedence levels are
executed before ones with lower precedence levels. Associativity is also assigned to operators
with the same precedence level. It indicates whether operators to the left or to the right are to
be executed first, in the case of equal precedence levels.
Expressions in parentheses () are executed first. In the case of nested parentheses, the
expression in the innermost pair is executed first.
is equivalent to:
Examples
The distance, d, between two points in the three-dimensional space (x1,y1,z1) and (x2,y2,z2) can
be computed from:
d = ( x1 − x2 ) 2 + ( y1 − y2 ) 2 + ( z1 − z2 ) 2
The following program computes and shows the distance between (2,1,3) and (0,0,6).
Exercise
1. What are the differences between primitive data types and class?
2. How many different things can be represented using n bytes?
3. Which of the eight primitive data types can store a numeric value 236?
4. Give the boolean values of the following expressions.
a. 01<2e-1
b. 8+0.0 >= 8.0
c. ‘a’>’b’
d. 2+3*2-6 == ((2+3)*2)-6
e. ‘a’>’$’||’b’<’$’
f. !(true||'6'>'#')&&!false
5. Determine the resulting values of x and y in the following code.
public class Ex3_5
{
public static void main(String[] args)
{
int x=0,y=0;
x = y + 1;
y = x + 1;
}
}
6. Give the reason why the following code will not be compiled successfully.
public class Ex3_6
{
public static void main(String[] args)
{
int x, y, z =3;
y = x;
z = y;
}
}
7. Write a Java program that calculates and shows these values on screen.
a. (6+5)(7-3)
− ( 6.5 ) 2 + ( 3.7 ) 2
b. e
c. 3
sin(1.2)
d. The floating point result of 8
1 + 10
e. The biggest integer that is less than 3.75
f. The smallest integer that is bigger than 3.75
3
g. ∑i
i =0
( i +1)
8. Write a Java program that shows the truth values of the following statements on
screen for all possible combination of p and q. (i.e. (true,true), (true,false),
(false,true), and (false,false))
a. p and q
b. p or q
c. either p or q
d. either p or its negation
9. Modify Distance3D.java so that the distance d is calculated from:
wx ( x1 − x2 ) 2 + w y ( y1 − y 2 ) 2 + wz ( z1 − z 2 ) 2
2 2 2
d=
wx + w y + wz
2 2 2