00 - Introduction To Java
00 - Introduction To Java
00 - Introduction To Java
Java as an Object-oriented
Programming Language
16/09/23 2
JDK Editions
16/09/23 3
Java IDE Tools
Text Pad
Net Bean
Eclipse
InteliJ
16/09/23 4
Your first Java program!
What does this code output (print to the user) when you
run (execute) it?
5
Compiling a program
6
The Java Virtual Machine
(JVM, or VM)
System.out.println();
Prints a blank line to the console.
8
A Java Program
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks
9
Class Name
Every Java program must have at least one class. Each class
has a name. By convention, class names start with an
uppercase letter. In this example, the class name is Welcome.
10
Main Method
Line 2 defines the main method. In order to run a class, the
class must contain a method named main. The program is
executed from the main method.
11
Statement
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome to
Java!") in the program in Listing 1.1 is a statement to
display the greeting "Welcome to Java!“.
12
Statement Terminator
Every statement in Java ends with a semicolon (;).
13
Reserved words
Reserved words or keywords are words that have a specific
meaning to the compiler and cannot be used for other
purposes in the program. For example, when the compiler
sees the word class, it understands that the word after class
is the name for the class.
14
Blocks
15
Special Symbols
Character Name Description
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
16
{ …}
Appropriate Comments
Naming Conventions
Proper Indentation and Spacing
Lines
Block Styles
22
Appropriate Comments
23
Naming Conventions
Choose meaningful and descriptive names.
Class names:
Capitalize the first letter of each word in the name.
For example, the class name
ComputeExpression.
24
Block Styles
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
25
Programming Errors
Syntax Errors
Detected by the compiler
Runtime Errors
Causes the program to abort
Logic Errors
Produces incorrect result
26
Syntax Errors
27
Runtime Errors
28
Logic Errors
29
Primitive data types,
expressions, and
variables
30
Primitive types
31
Other Primitive Data Types
Discrete Types
byte
Continuous Types
short float
int double
long
Non-numeric Types
boolean
char
32
Data Type Representations
Type Representation Bits Bytes #Values
boolean true or false 1 N/A 2
char ‘a’ or ‘7’ or ‘\n’ 16 2 216 = 65,536
byte …,-2,-1,0,1,2,… 8 1 28 = 256
short …,-2,-1,0,1,2,… 16 2 216 = 65,536
int …,-2,-1,0,1,2,… 4 > 4.29 million
long …,-2,-1,0,1,2,… 8 > 18 quintillion
float 0.0, 10.5, -100.7 32
double 0.0, 10.5, -100.7 64
Precision in real numbers
Example:
System.out.println(0.1 + 0.2);
The output is 0.30000000000000004!
34
Concatenation: Operating on strings
Examples:
"hello" + 42 is "hello42"
1 + "abc" + 2 is "1abc2"
"abc" + 1 + 2 is "abc12"
1 + 2 + "abc“ is "3abc"
"abc" + 9 * 3 is "abc27" (what happened here?)
"1" + 1 is "11"
4 - 1 + "abc“ is "3abc"
In the case of overflow, Java discards the high-order bytes, retaining only
the low-order ones
In this case, the low order bytes represent 1651507200, and since the right
most bit is a 1 the sign value is negative.
Declaring variables
Examples:
int x;
double myGPA;
int varName;
37
Boolean Arithmetic
38
The boolean Type
39
Relational expressions
40
Class Constants
To give it the right scope, simply declare it right inside the class:
42
Interactive programs
43
Scanner
Example:
Scanner console = new Scanner(System.in);
44
Scanner methods
45
Using a Scanner object
Example:
System.out.print("How old are you? "); // prompt
int age = console.nextInt();
System.out.println("You'll be 40 in " + (40 -
age)
+ " years.");
46
Input tokens
token: A unit of user input, as read by the Scanner.
Tokens are separated by whitespace (spaces, tabs, new lines).
How many tokens appear on the following line of input?
23 John Smith 42.0 "Hello world"
When the token doesn't match the type the Scanner tries to read,
the program crashes.
Example:
System.out.print("What is your age? ");
int age = console.nextInt();
Sample Run:
What is your age? Timmy
InputMismatchException:
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
...
47
Importing classes
48
code.ptit.edu.vn
49