02 Java Syntax Basics Part1
02 Java Syntax Basics Part1
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
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
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
15
6
2025/3/5
16
17
7
2025/3/5
18
19
8
2025/3/5
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
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
26
11
2025/3/5
27
28
12
2025/3/5
30
31
13
2025/3/5
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
• 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