2025/3/5
Unit objectives
Aftercompleting this unit, you should be
able to:
Outline naming conventions used by Java
programs
Construct a valid identifier
Describe the Java primitive data types, and
explain how and why each one is used
Declare and initialize Java variables and arrays
Identify reserved words
Identifiers
Identifiers are:
Text strings that represent variables, methods,
classes or labels
Case-sensitive
Characters can be digit, letter, '$' or '_'
Identifiers cannot:
Begin with digit
Be the same as a reserved word
1
2025/3/5
Java is case-sensitive
Java is case-sensitive
yourname, yourName, Yourname, YourName are
four different identifiers
•Conventions:
Package: all lower case
theexample
Class: initial upper case, composite words with
upper case
TheExample
Method/field: initial lower, composite words with
upper case
theExample
Constants: all upper case
THE_EXAMPLE
Reserved words
A literal is the source code
Literals representation of a value of a
null true false primitive type, the String
type,or the null type.
Keywords
Reserved for future use
const goto
2
2025/3/5
Java Types
There are two kinds of types
PrimitiveType
Integer
Float
Character
Boolean
ReferenceType
ClassOrInterfaceType
TypeVariable
ArrayType
Java primitives
Every variable must have a data type
Primitive data types contain a single value
The size and format of a primitive data type are
suited to its type
Java has four categories of primitives
3
2025/3/5
Primitives:integers
Signed whole numbers
Initialized to zero
10
Primitives:floating points
“General” numbers (can have fractional parts)
Initialized to zero
11
4
2025/3/5
Primitives: characters
Anyunsigned Unicode character is a char
primitive data type
A character is a single Unicode character
between two single quotes
Initialized to zero (\u0000)
12
Primitives: booleans
boolean values are distinct in Java
An int value can NOT be used in place of a boolean
A boolean can store either true or false
Initialized to false
13
5
2025/3/5
Literals
A literal is a value
Six kinds:
integer
floating point
boolean
character
String
null
14
Primitive literals: integers
Octals are prefixed with a zero
032
Hexadecimals are prefixed with a zero and an
x
0x1A
Follow a literal with “L” to indicate a long
26L
Upper and lower case are equivalent
15
6
2025/3/5
Primitive literals: floating point
float literals end with an f (or F)
7.1f
double literals end with a d (or D)
7.1D
e (or E) is used for scientific notation
7.1e2
A floating point number with no final letter is
a double
7.1 is the same as 7.1d
Upper and lower case are equivalent
16
Primitive literals: escape sequences
Somekeystrokes can be simulated with an escape
sequence
\b (backspace BS, Unicode \u0008)
\t (horizontal tab HT, Unicode \u0009)
\n (linefeed LF, Unicode \u000a)
\f (form feed FF, Unicode \u000c)
\r (carriage return CR, Unicode \u000d)
Some characters may need to be escaped when used
in string literals
\" (double quote “, Unicode \u0022)
\’ (single quote ’ , Unicode \u0027)
\\ (backslash \, Unicode \u005c)
Hexadecimal Unicode values can also be written
‘\uXXXX’
17
7
2025/3/5
Casting primitive types
Java is a strictly typed language
Assigning the wrong type of value to a variable
could result in a compile error or a JVM exception
Castinga value allows it to be treated as
another type
TheJVM can implicitly promote from a
narrower type to a wider type
Tochange to a narrower type, you must cast
explicitly
18
Implicit versus explicit casting
Casting is automatically done when no loss
of information is possible
Anexplicit cast is required when there is a
"potential" loss of accuracy
19
8
2025/3/5
Declarations and initialization
Variables must be declared before they can be
used
Singlevalue variables (variables that are not
arrays) must be initialized before their first use
in an expression
Declarations and initializations can be combined
Use = for assignment (including initialization)
Examples:
21
Arrays
Arrays must also be declared before use
Have fixed size
May be specified by a literal, by an expression, or
implicitly
May be optionally initialized
Have default values depending on their type
Are always zero-based (array[0] is the first element)
Examples:
22
9
2025/3/5
Operators and precedence
Operators are the “glue” of expressions
Precedence – which operator is evaluated first
– is determined explicitly by parentheses or
implicitly as follows:
23
Comments
Java supports three kinds of comments:
24
10
2025/3/5
Statements
Statements are terminated by a semicolon
Several statements can be written on one
line
A statement can be split over several lines
25
Conditional statement: if-else
Conditional expression must evaluate to a
boolean
The else clause is optional
Braces are not needed for single statements
but are highly recommended for clarity
26
11
2025/3/5
Shortcut for if-else: the ternary
operator
Shortcut for if-else statement:
(<boolean-expr> ? <true-choice> : <false-choice>)
Can result in shorter code
Make sure code is still readable
27
Conditional statement: switch
Testsa single variable for several alternative
values and executes the corresponding case
Any case without break will “fall through”
Next case will also be executed
default
clause handles values not explicitly
handled by a case
28
12
2025/3/5
Looping statements: while and
do…while
Executes a statement or block as long as
the condition remains true
while() executes zero or more times
do...while() executes at least once
30
Looping statement: for
A for loop executes the statement or block { } that
follows it
Evaluates "start expression" once
Continues as long as the "test expression" is true
Evaluates "increment expression" after each iteration
A variable can be declared in the for statement
Typically used to declare a "counter" variable
Typically declared in the “start” expression
Its scope is restricted to the loop
31
13
2025/3/5
for versus while
These statements provide equivalent
functionality
Each can be implemented in terms of the other
These looping structures are typically used in
different situations
while tends to be used for open-ended looping
for tends to be used for looping over a fixed number
of iterations
32
Branching statements
break
Can be used outside a switch statement
Terminates a for, while or do...while loop
Two forms:
Labeled: execution continues at next statement after labeled loop
Unlabeled: execution continues at next statement outside loop
continue
Like break, but merely skips the remainder of this iteration of the
loop, then continues by evaluating the boolean expression of the
innermost loop
Labeled and unlabeled forms
return
Exits the current method
May include an expression to be returned
Type must match method’s return type
A void return type means no value can be returned
33
14
2025/3/5
Sample branching statements
• break
• Can be used outside a switch statement
• Terminates a for, while or do...while loop
• Two forms:
• Labeled: execution continues at next statement after labeled loop
• Unlabeled: execution continues at next statement outside loop
35
Scope
A variable's scope is the region of a program
within which the variable can be referenced
Variables declared in a method can only be
accessed in that method
Variables declared in a loop or a block can only be
accessed in that loop or block
37
15