Data Types and Operators
Data Types and Operators
and
Operators
PAGE 1
Identifiers
An identifier is a sequence of characters that consist of letters, digits,
underscores (_), and dollar signs ($).
An identifier must start with a letter, an underscore (_), or a dollar
sign ($). It cannot start with a digit.
An identifier cannot be a reserved word. (See Appendix A, “Java
Keywords,” for a list of reserved words).
An identifier cannot be true, false, or null.
An identifier can be of any length.
2
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
3
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
4
Declaring and Initializing in
One Step
int x = 1;
double d = 1.4;
5
Named Constants
6
Naming Conventions
Class names:
Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.
Constants:
Capitalize all letters in constants, and use underscores to
connect words.
For example, the constant PI and MAX_VALUE
8
Primitive Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
9
Numeric Operators
+ Addition 34 + 1 35
% Remainder 20 % 3 2
10
Integer Division
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
11
Remainder Operator
Remainder is very useful in programming. For example, an even
number % 2 is always 0 and an odd number % 2 is always 1. So you
can use this property to determine whether a number is even or odd.
Suppose today is Saturday and you and your friends are going
to meet in 10 days. What day is in 10 days? You can find that
day is Tuesday using the following expression:
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
12
Introducing Programming with an
Example
Computing the Area of a Circle
This program computes the area of the circle.
13
Trace a Program
Execution allocate memory
public class ComputeArea { for radius
/** Main method */
public static void main(String[] args) { radius no value
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
} 14
Trace a Program
Execution
public class ComputeArea {
memory
/** Main method */
public static void main(String[] args) { radius no value
double radius;
area no value
double area;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
} 15
Trace a Program
Execution
public class ComputeArea { assign 20 to radius
/** Main method */
public static void main(String[] args) {
radius 20
double radius;
double area; area no value
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
16
Trace a Program
Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
radius 20
double radius;
double area; area 1256.636
// Assign a radius
radius = 20;
compute area and assign it
to variable area
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
17
Trace a Program
Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
radius 20
double radius;
double area; area 1256.636
// Assign a radius
radius = 20;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
18
Variables
20
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
double area;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius "
+ radius + " is " + area);
}
} 22
Problem: Displaying
Time
Write a program that obtains minutes and remaining
seconds from seconds.
23
program that obtains minutes and
remaining seconds from seconds
public class DisplayTime {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter an integer for seconds: ");
int seconds = input.nextInt();
int minutes = seconds / 60; // Find minutes in seconds
int remainingSeconds = seconds % 60; // Seconds remaining
System.out.println(seconds + " seconds is " + minutes +
" minutes and " + remainingSeconds + " seconds");
}
}
PAGE 24
Number Literals
25
Integer Literals
An integer literal can be assigned to an integer variable as long as it can
fit into the variable. A compilation error would occur if the literal were
too large for the variable to hold. For example, the statement byte b =
1000 would cause a compilation error, because 1000 cannot be stored
in a variable of the byte type.
An integer literal is assumed to be of the int type, whose value is
between -231 (-2147483648) to 231–1 (2147483647). To denote an
integer literal of the long type, append it with the letter L or l. L is
preferred because l (lowercase L) can easily be confused with 1 (the
digit one).
26
Floating-Point Literals
Floating-point literals are written with a decimal point. By default,
a floating-point literal is treated as a double type value.
For example, 5.0 is considered a double value, not a float value.
You can make a number a float by appending the letter f or F, and
make a number a double by appending the letter d or D.
For example, you can use 100.2f or 100.2F for a float number, and
100.2d or 100.2D for a double number.
27
double vs. float
The double type values are more accurate than the float type values. For example,
16 digits
28
Arithmetic Expressions
is translated to
29
How to Evaluate an
Expression
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
Though Java has its own way to evaluate an 3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
expression behind the scene, the result of a Java 3 + 16 + 5 * 7 – 1
(3) multiplication
expression and its corresponding arithmetic 3 + 16 + 35 – 1
expression are the same. Therefore, you can (4) addition
19 + 35 – 1
safely apply the arithmetic rule for evaluating a (5) addition
54 - 1
Java expression. (6) subtraction
53
30
Problem: Converting
Temperatures
31
Problem: Converting
Temperatures
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
// Convert Fahrenheit to Celsius
double celsius = (5.0 / 9) * (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is " +
celsius + " in Celsius");
}
}
PAGE 32
Augmented Assignment
Operators
33
Increment and Decrement
Operators
34
Increment and Decrement
Operators, cont.
35
Numeric Type
Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
36
Conversion Rules
37
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
38
Type Casting
PAGE 39
Non primitive data type
Java Strings
Strings are used for storing text.
40