Byte, Short, Int, Long, Float, Double, Char, Boolean
Byte, Short, Int, Long, Float, Double, Char, Boolean
Operations
Introduce Programming with an Example
Identifiers, Variables, and Constants
Primitive Data Types
– byte, short, int, long, float, double, char, boolean
Expressions
Operators, Precedence, Associativity, Operand
Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^,
&, |, +, -,
Getting Input from Input Dialog Boxes
Case Studies (Computing Mortgage, and Computing Changes)
Style and Documentation Guidelines
Syntax Errors, Runtime Errors, and Logic Errors
Introducing Programming with an
Example
Example 2.1 Computing the Area of a
Circle
This program computes the area of the
circle.
ComputeArea Run
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.
Variables
// Compute the first area
radius = 1.0;
area = radius*radius*3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
Operators
+, -, *, /, and %
is translated to
byte i = 100;
long k = i*3+4;
double d = i*3.1+k/2;
int x = k; //(Wrong)
long k = x; //(fine,implicit casting)
Type Casting
double
float
long
int
short
byte
Type Casting, cont.
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
Special characters
char tab = ‘\t’;
Unicode Format
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000a
Carriage return \r \u000d
Appendix B: ASCII Character Set
ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
ASCII Character Set, cont.
ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
Casting between char and
Numeric Types
int i = 'a'; // Same as int i = (int)'a';
Operand !Operand
true false
false true
Truth Table for Operator &&
Operand1 Operand2 Operand1 && Operand2
false false false
false true false
true false false
true true true
Truth Table for Operator ||
Operand1 Operand2 Operand1 || Operand2
false false false
false true true
true false true
true true true
Truth Table for Operator ^
Operand1 Operand2 Operand1 ^ Operand2
false false false
false true true
true false true
true true false
The & and | Operators
&&: conditional AND operator
&: unconditional AND operator
||: conditional OR operator
|: unconditional OR operator
3 + 4 * 4 > 5 * (4 + 3) - ++i
Operator Precedence
var++, var--
+, - (Unary plus and minus), ++var,--var
(type) Casting
! (Not)
*, /, % (Multiplication, division, and modulus)
+, - (Binary addition and subtraction)
<, <=, >, >= (Comparison)
==, !=; (Equality)
& (Unconditional AND)
^ (Exclusive OR)
| (Unconditional OR)
&& (Conditional AND) Short-circuit AND
|| (Conditional OR) Short-circuit OR
=, +=, -=, *=, /=, %= (Assignment operator)
Operator Associativity
When two operators with the same precedence
are evaluated, the associativity of the operators
determines the order of evaluation. All binary
operators except assignment operators are left-
associative.
a – b + c – d is equivalent to ((a – b) + c) – d
Assignment operators are right-associative.
Therefore, the expression
a = b += c = 5 is equivalent to a = (b += (c = 5))
Operand Evaluation Order
The precedence and associativity rules
specify the order of the operators, but do not
specify the order in which the operands of a
binary operator are evaluated. Operands are
evaluated from left to right in Java.
The left-hand operand of a binary operator is
evaluated before any part of the right-hand
operand is evaluated.
Operand Evaluation Order, cont.
If no operands have side effects that change the value
of a variable, the order of operand evaluation is
irrelevant. Interesting cases arise when operands do
have a side effect. For example, x becomes 1 in the
following code, because a is evaluated to 0 before +
+a is evaluated to 1.
int a = 0;
int x = a + (++a);
But x becomes 2 in the following code, because ++a
is evaluated to 1, then a is evaluated to 1.
int a = 0;
int x = ++a + a;
Getting Input from Input Dialog Boxes
String string = JOptionPane.showInputDialog(
null, “Prompt Message”, “Dialog Title”,
JOptionPane.QUESTION_MESSAGE));
where x is a string for the prompting message and
y is a string for the title of the input dialog box.
Convertting Strings to Integers
The input returned from the input dialog box is a string. If
you enter a numeric value such as 123, it returns “123”.
To obtain the input as a number, you have to convert a
string into a number.
To convert a string into an int value, you can use the
static parseInt method in the Integer class as follows:
int intValue = Integer.parseInt(intString);
where intString is a numeric string such as “123”.
Convertting Strings to Doubles
To convert a string into a double value, you can use the
static parseDouble method in the Double class as follows:
double doubleValue =Double.parseDouble(doubleString);
where doubleString is a numeric string such as “123.45”.
Example 2.2
Entering Input from Dialog Boxes
This program first prompts the user to enter a
year as an int value and checks if it is a leap
year, it then prompts you to enter a double
value and checks if it is positive.
A year is a leap year if it is divisible by 4 but
not by 100, or it is divisible by 400.
InputDialogDemo Run
Example 2.3
Computing Mortgages
This program lets the user enter the interest
rate, number of years, and loan amount and
computes monthly payment and total
payment.
ComputeChange Run
Programming Style and
Documentation
Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles
Appropriate Comments
Include a summary at the beginning of the
program to explain what the program does, its
key features, its supporting data structures, and
any unique techniques it uses.
Constants:
– Capitalize all letters in constants.
For example, the constant PI.
Proper Indentation and Spacing
Indentation
– Indent two spaces.
Spacing
– Use blank line to separate segments of the code.
Block Styles
Use end-of-line style for braces.
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Programming Errors
Syntax Errors
– Detected by the compiler
Runtime Errors
– Causes the program to abort
Logic Errors
– Produces incorrect result
Compilation Errors