08/13/14 Võ Phương Bình - ITFAC - DLU 24Compiling and Running a Program Where are the files stored in the directory?. 08/13/14 Võ Phương Bình - ITFAC - DLU 27Package The second line
Trang 108/13/14 Võ Phương Bình - ITFAC - DLU 1
Chapter 1: Introduction to Java
Chapter 2: Primitive Data Types and
Trang 208/13/14 Võ Phương Bình - ITFAC - DLU 2
Chapter 1:
Introduction to Java
What Is Java?
Getting Started With Java Programming
Create, Compile and Running a Java
Application
Trang 308/13/14 Võ Phương Bình - ITFAC - DLU 3
Trang 408/13/14 Võ Phương Bình - ITFAC - DLU 4
Trang 508/13/14 Võ Phương Bình - ITFAC - DLU 5
Trang 608/13/14 Võ Phương Bình - ITFAC - DLU 6
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
Trang 7Java2 Micro Edition (J2ME™) Java 2 Platform
Standard desktop &
workstation applications Heavy duty server systems constrained devices Small & memory
Trang 808/13/14 Võ Phương Bình - ITFAC - DLU 8
Java IDE Tools
IBM Visual Age for Java
Forte by Sun Microsystems
Trang 908/13/14 Võ Phương Bình - ITFAC - DLU 9
Trang 1008/13/14 Võ Phương Bình - ITFAC - DLU 10
Console
Trang 1108/13/14 Võ Phương Bình - ITFAC - DLU 11
GUI (with JFC)
Trang 1208/13/14 Võ Phương Bình - ITFAC - DLU 12
Applet & Web
Trang 1308/13/14 Võ Phương Bình - ITFAC - DLU 13
Database & Web
Trang 1408/13/14 Võ Phương Bình - ITFAC - DLU 14
Mobile
Trang 1508/13/14 Võ Phương Bình - ITFAC - DLU 15
Compare to C/C++/C#
Similar to C/C++/C#
Difference:
Compiler
Trang 1608/13/14 Võ Phương Bình - ITFAC - DLU 16
Traditional Compiler (C/C++)
Trang 1708/13/14 Võ Phương Bình - ITFAC - DLU 17
Trang 1808/13/14 Võ Phương Bình - ITFAC - DLU 18
Trang 1908/13/14 Võ Phương Bình - ITFAC - DLU 19
Getting Started with Java
Programming
A Simple Java Application
Compiling Programs
Executing Applications
Trang 2008/13/14 Võ Phương Bình - ITFAC - DLU 20
public class Welcome {
public static void main(String[] args) { System.out.println("Welcome to
Java!");
}
}
Trang 2108/13/14 Võ Phương Bình - ITFAC - DLU 21
Creating and Compiling Programs
On command line
javac file.java
Source Code Create/Modify Source Code
Compile Source Code i.e javac Welcome.java
Bytecode
Run Byteode i.e java Welcome
Result
If compilation errors
If runtime errors or incorrect result
Trang 2208/13/14 Võ Phương Bình - ITFAC - DLU 22
on Sun Solaris
Java Interpreter
on Linux
Bytecode
Trang 23
08/13/14 Võ Phương Bình - ITFAC - DLU 23
Example
javac Welcome.java
java Welcome
output:
Trang 2408/13/14 Võ Phương Bình - ITFAC - DLU 24
Compiling and Running a Program
Where are the files stored in the directory?
Java source files and class files for Chapter 2
chapter19 Java source files and class files for Chapter 19
Welcome.java~
Trang 2508/13/14 Võ Phương Bình - ITFAC - DLU 25
Anatomy of a Java Program
Trang 2608/13/14 Võ Phương Bình - ITFAC - DLU 26
Comments
Similar to C/C++
In Java, comments are preceded by two
slashes (//) in a line, or enclosed
between /* and */ in one or multiple lines.
When the compiler sees //, it ignores all text after // in the same line When it sees /*, it scans for the next */ and ignores any text between /* and */
Trang 2708/13/14 Võ Phương Bình - ITFAC - DLU 27
Package
The second line in the program
(package chapter1;) specifies a package name, chapter1, for the class Welcome.
Trang 2808/13/14 Võ Phương Bình - ITFAC - DLU 28
Reserved Words
Trang 2908/13/14 Võ Phương Bình - ITFAC - DLU 29
Trang 3008/13/14 Võ Phương Bình - ITFAC - DLU 30
Blocks
A pair of braces in a program forms a block that groups components of a
program
public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!");
} }
Class block Method block
Trang 3108/13/14 Võ Phương Bình - ITFAC - DLU 31
Classes
Java construct.
blueprint for objects To
program in Java, you must
understand classes and be
able to write and use them
Trang 3208/13/14 Võ Phương Bình - ITFAC - DLU 32
Trang 3308/13/14 Võ Phương Bình - ITFAC - DLU 33
main Method
The main method provides the control of
program flow The Java interpreter executes the application by invoking the main method
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
Trang 3408/13/14 Võ Phương Bình - ITFAC - DLU 34
Displaying Text in a Message
Dialog Box
showMessageDialog method in the JOptionPane class.
many predefined classes in
the Java system.
Trang 3508/13/14 Võ Phương Bình - ITFAC - DLU 35
Trang 3608/13/14 Võ Phương Bình - ITFAC - DLU 36
The exit Method
and stop all threads.
thread is spawned to run the program When the showMessageDialog is
invoked, a separate thread is spawned
to run this method The thread is not terminated even you close the dialog box To terminate the thread, you
have to invoke the exit method.
Trang 3708/13/14 Võ Phương Bình - ITFAC - DLU 37
Trang 3808/13/14 Võ Phương Bình - ITFAC - DLU 38
Chapter 2: Primitive Data Types and
Operations
Introduce Programming with an Example
Identifiers, Variables, and Constants
Primitive Data Types
byte, short, int, long, float, double, char,
Getting Input from Input Dialog Boxes
Case Studies (Computing Mortgage, and Computing
Changes)
Style and Documentation Guidelines
Syntax Errors, Runtime Errors, and Logic Errors
Trang 3908/13/14 Võ Phương Bình - ITFAC - DLU 39
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
An identifier cannot be true , false , or
An identifier can be of any length.
Trang 4008/13/14 Võ Phương Bình - ITFAC - DLU 40
area + " for radius "+radius);
// Compute the second area
Trang 4108/13/14 Võ Phương Bình - ITFAC - DLU 41
// character variable;
Trang 4208/13/14 Võ Phương Bình - ITFAC - DLU 42
Trang 4308/13/14 Võ Phương Bình - ITFAC - DLU 43
Declaring and Initializing
in One Step
int x = 1;
double d = 1.4;
float f = 1.4;
Trang 4408/13/14 Võ Phương Bình - ITFAC - DLU 44
Trang 4508/13/14 Võ Phương Bình - ITFAC - DLU 45
Numerical Data Types
Trang 4608/13/14 Võ Phương Bình - ITFAC - DLU 46
Operators
+, -, *, /, and %
5/2 yields an integer 2.
5.0/2 yields a double value 2.5
5 % 2 yields 1 (the remainder of the division)
Trang 4708/13/14 Võ Phương Bình - ITFAC - DLU 47
NOTE
• Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy.
Trang 4808/13/14 Võ Phương Bình - ITFAC - DLU 48
Number Literals
• A literal is a constant value that
appears directly in the program
• For example, 34, 1,000,000, and 5.0 are literals in the following statements:
int i = 34;
long l = 1000000;
double d = 5.0;
Trang 4908/13/14 Võ Phương Bình - ITFAC - DLU 49
Integer Literals
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.
would cause a compilation error, because
1000 cannot be stored in a variable of the byte type.
denote an integer literal of the long type, append it with the letter L or l.
Trang 5008/13/14 Võ Phương Bình - ITFAC - DLU 50
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
Trang 5108/13/14 Võ Phương Bình - ITFAC - DLU 51
Scientific Notation
specified in scientific notation.
equivalent to 0.0123456
it can be either in lowercase or uppercase
Trang 5208/13/14 Võ Phương Bình - ITFAC - DLU 52
Arithmetic Expressions
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x +
9*(4/x + (9+x)/y)
Trang 5308/13/14 Võ Phương Bình - ITFAC - DLU 53
Trang 5408/13/14 Võ Phương Bình - ITFAC - DLU 54
Increment and Decrement Operators
Trang 5508/13/14 Võ Phương Bình - ITFAC - DLU 55
Increment and Decrement Operators, cont.
int newNum = 10*i;
Equivalent to
Trang 5608/13/14 Võ Phương Bình - ITFAC - DLU 56
Increment and Decrement Operators, cont.
Using increment and decrement
operators makes expressions short, but
it also makes them complex and
difficult to read
Avoid using these operators in
expressions that modify multiple
variables, or the same variable for
multiple times such as this: int k = +
+i + i
Trang 5708/13/14 Võ Phương Bình - ITFAC - DLU 57
Assignment Expressions and
Assignment Statements
Prior to Java 2, all the expressions can be used as statements
Since Java 2, only the following types
of expressions can be statements:
variable op= expression; // Where op is +, -, *, /, or %
++variable;
variable++;
variable;
variable ;
Trang 5808/13/14 Võ Phương Bình - ITFAC - DLU 58
Numeric Type Conversion
Consider the following statements:
Trang 5908/13/14 Võ Phương Bình - ITFAC - DLU 59
Trang 6008/13/14 Võ Phương Bình - ITFAC - DLU 60
Type Casting, cont.
Trang 6108/13/14 Võ Phương Bình - ITFAC - DLU 61
Character Data Type
• char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
char letter = '\u0041'; Unicode) char numChar = '\u0034';(Unicode)
• Special characters
char tab = ‘\t’;
Trang 6208/13/14 Võ Phương Bình - ITFAC - DLU 62
Trang 6308/13/14 Võ Phương Bình - ITFAC - DLU 63
Casting between char and
Numeric Types
• int i = ' a ' ; // Same as
int //i = (int) ' a ' ;
• char c = 97; // Same as char //c = (char)97;
Trang 6408/13/14 Võ Phương Bình - ITFAC - DLU 64
The boolean Type and Operators
• boolean lightsOn = true;
• boolean lightsOn = false;
• boolean b = (1 > 2);
&& (and) (1 < x) && (x < 100)
|| (or) (lightsOn) || (isDayTime)
! (not) !(isStopped)
Trang 6508/13/14 Võ Phương Bình - ITFAC - DLU 65
Trang 6608/13/14 Võ Phương Bình - ITFAC - DLU 66
Trang 6708/13/14 Võ Phương Bình - ITFAC - DLU 67
Truth Table for Operator !
Truth Table for Operator !
Operand !Operand
Trang 6808/13/14 Võ Phương Bình - ITFAC - DLU 68
Truth Table for Operator &&
Operand1 Operand2 Operand1 && Operand2
Trang 6908/13/14 Võ Phương Bình - ITFAC - DLU 69
Truth Table for Operator ||
Operand1 Operand2 Operand1 ||
Operand2
Trang 7008/13/14 Võ Phương Bình - ITFAC - DLU 70
Truth Table for Operator ^
Operand1 Operand2 Operand1 ^
Operand2
Trang 7108/13/14 Võ Phương Bình - ITFAC - DLU 71
The & and | Operators
&&: conditional AND operator
&: unconditional AND operator
Trang 7208/13/14 Võ Phương Bình - ITFAC - DLU 72
The & and | Operators
If x is 1, what is x after this
Trang 7308/13/14 Võ Phương Bình - ITFAC - DLU 73
Operator Precedence
How to evaluate
3 + 4 * 4 > 5 * (4 + 3) - ++i
Trang 7408/13/14 Võ Phương Bình - ITFAC - DLU 74
* , / , % (Multiplication, division, and modulus)
+ , - (Binary addition and subtraction)
&& (Conditional AND) Short-circuit AND
|| (Conditional OR) Short-circuit OR
= , += , -= , *= , /= , %= (Assignment operator)
Trang 7508/13/14 Võ Phương Bình - ITFAC - DLU 75
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))
Trang 7608/13/14 Võ Phương Bình - ITFAC - DLU 76
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
Trang 7708/13/14 Võ Phương Bình - ITFAC - DLU 77
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;
Trang 7808/13/14 Võ Phương Bình - ITFAC - DLU 78
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.
Trang 7908/13/14 Võ Phương Bình - ITFAC - DLU 79
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”
Trang 8008/13/14 Võ Phương Bình - ITFAC - DLU 80
Convertting Strings to Doubles
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”.
Trang 8108/13/14 Võ Phương Bình - ITFAC - DLU 81
Programming Style and
Trang 8208/13/14 Võ Phương Bình - ITFAC - DLU 82
Appropriate Comments
Include a summary at the beginning of the program to explain what the program does, its key features, its supporting
techniques it uses
program
Trang 8308/13/14 Võ Phương Bình - ITFAC - DLU 83
If the name consists of several words,
concatenate all in one, use lowercase
for the first word, and capitalize the
first letter of each subsequent word in
the name
For example, the variables radius and
area , and the method computeArea
Trang 8408/13/14 Võ Phương Bình - ITFAC - DLU 84
Naming Conventions, cont.
Class names:
Capitalize the first letter of
each word in the name For
example, the class name
ComputeArea
Constants:
Capitalize all letters in
constants For example, the
constant PI
Trang 8508/13/14 Võ Phương Bình - ITFAC - DLU 85
Proper Indentation and Spacing
Trang 8608/13/14 Võ Phương Bình - ITFAC - DLU 86
Block Styles
Use end-of-line style for braces.
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
} }
public class Test { public static void main(String[] args) { System.out.println("Block Styles");
} }
End-of-line style
Next-line style
Trang 8708/13/14 Võ Phương Bình - ITFAC - DLU 87
Trang 8808/13/14 Võ Phương Bình - ITFAC - DLU 88
Compilation Errors
public class ShowSyntaxErrors {
public static void main(String[] args) {
i = 30;
System.out.println(i+4);
}
}
Trang 8908/13/14 Võ Phương Bình - ITFAC - DLU 89
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
}
}
Trang 9008/13/14 Võ Phương Bình - ITFAC - DLU 90
Logic Errors
public class ShowLogicErrors {
// Determine if a number is between 1 and 100 inclusively
public static void main(String[] args) {
// Prompt the user to enter a number
String input = JOptionPane.showInputDialog(null,
"Please enter an integer:",
"ShowLogicErrors", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input);
// Display the result
System.out.println("The number is between 1 and 100, "