Title: Chapter 3 Basic Java Programming
1Chapter 3 Basic Java Programming 3.0.
Identifiers, Reserved Words, and
Literals Identifiers. An identifier is a text
string used as a label. It can be any combination
of letters, digits, and _, of any length but
not start with a digit, or contain 1)
Examples total, label17, amount, main, public,
private, a, b, a17, b13 (Not identifier
4th_word, coin, Johnsmith) case sensitive
(total, Total, TOTAL) Reserved words. class,
public, static, void, with pre-defined meanings
2Convention. lowercase letters for variables,
methods names, and reserved words Title case for
class, and interface names (uppercase for the
first letter of each word) UPPERCASE for
constants Naming. Use meaning identifiers name,
age, don't use single letter n, a, Keep them to a
reasonable short length Use the _ to separate
words of an identifier first_name,
last_name Literals. Literals are explicit data
values that are used in the program strings
"Welcome to Java!" numeric data 25, 3.14, -0.4
33.1. Primitive data types A type is a
classification based on the nature of data. a)
Integer Types byte 8 bits, -128 to 127short 16
bits -32,768 to 32,767int 32 bits -2,147,483,648
to 2,147,483,647long 64 bits ... b) Real
Types float 32 bitsdouble 64 bits c)
Characters Unicode character set 16 bits 65,536
unique characters 'j', 'J', '1', '0', '\b'
backspace,'\t' tab \n' newline'\r' carriage
return '\"' double quote'\'' single
quote '\\' backslash
4d) Booleans can hold one of two valid values
true and false (reserved word)It is used to
indicate if a condition is true or false. e)
Wrappers Each primitive type has a corresponding
wrapper class. Byte byte Short shortInteger
int Long longFloat float Double
doubleCharacter char Boolean booleanVoid f)
String String is not a primitive type. It is a
class "John Smith"Welcome to Java!\n"\t \b \n
\" "
53.2. Variables and Assignment Variables.
Variables are named memory location capable of
containing data that can be modified during
program execution. 1) key points The value in the
location has to be of certain type integer,
float, etc. The same kinds of variables have the
same memory size, different kinds of variables
may have different memory size. variables have to
be declared explicitly int age, float salary, A
variable can store only one value of its declared
type
62) Variable declaration data-type
variable-namedata-type variable-name
initial-value int age 18 float cost
0 boolean married false String name "John
Smith" Note String is not a primitive data
type, it is a class. 3) Assignment
Statements variable-name expression used to
change the values in variables. age 30 cost
3.5 4.2 2 name "Clinton"
72) Variable declaration data-type
variable-namedata-type variable-name
initial-value int age 18 float cost
0 boolean married false String name "John
Smith" Note String is not a primitive data
type, it is a class. 3) Assignment
Statements variable-name expression used to
change the values in variables. age 30 cost
3.5 4.2 2 name "Clinton"
8class Types public static void main (String
args) byte b 100 short s 20000
int i 1000000 long l 400000000
char c 'a' float f .25f // f or F for
float double d .00001234 // otherwise,
double boolean bool true
System.out.println ("byte b " b)
System.out.println ("short s " s)
System.out.println ("int i " i)
System.out.println ("long l " l)
System.out.println ("char c " c)
System.out.println ("float f " f)
System.out.println ("double d " d)
System.out.println ("boolean bool " bool)
9Output byte b 100 short s 20000 int i
1000000 long l 400000000 char c a float f
0.25 double d 1.234e-05 boolean bool true
103.3 Input and Output Introduction A stream is a
flow of data. Input streams direct data from
input devices to the computer, and output stream
direct data toward output devices. There are
three predefined standard streams 1) System.in,
for reading input, usually from the keyboard. 2)
System.out, for writing output, usually to the
monitor. 3) System.err, for writing error
messages, usually to the monitor. A Sample
Program All I/O is performed with the help of
predefined class libraries that are defined in
the java.io package of the Java API. The
following is an example that sends output to the
monitor and read input string from the keyboard.
11//
// Echo.java Author Lewis/Loftus //
Demonstrates the use of the nextLine method of
the Scanner // class to read a string from the
user. //
import java.util.Scanner p
ublic class Echo public static void main
(String args) String message
Scanner scan new Scanner (System.in)
System.out.println ("Enter a line of text")
message scan.nextLine()
System.out.println ("You entered \"" message
"\"")
123.5 Making Decisions The if Statement if
(boolean_expression) statements Boolean
Expressions A boolean expression is an expression
that yields a Boolean value (true or false). int
total 15boolean answer3 (total
15)boolean answer4 (total ! 15) boolean
answer5 (total lt 15) boolean answer6
(total lt 15) boolean answer7 (total gt 15)
boolean answer8 (total gt 15) boolean
answer9 (total 15 / 3 2)
13Block Statements A block is a section of code
enclosed in a pair of braces, which signify that
the intervening code can be treated as if it were
a single statement. Ex. if (age1 10)
System.out.println ("The age1 is 10")
System.out.println ("The person is a
kid") key point A statement must end with a
A statement block in ... must not end with
a
14The if-else Statement Syntax if
(boolean_expression) statement1else
statement2 Ex. if (age 10)
System.out.println ("The age is 10")else
System.out.println ("The age is not 10")
15if (age1 10) System.out.println ("The age1
is 10") System.out.println ("The person is a
kid") else System.out.println ("The age1
is not 10") System.out.println ("The person
may not be a kid") if and if-else can be
nested.
163.6. Repetition The while Statement Syntax while
(boolean_expression) statement Ex. counter
1 while (counter lt 10) System.out.println(
"Welcome to Java!") counter counter 1
17class While public static void main (String
args) int counter 1 while (counter
lt 10) System.out.print(counter " ")
counter counter 1
System.out.println("All done.") Infinite
Loops counter 15 while (counter ! 10)
System.out.println("Welcome to Java!") counter
counter 1