2 Syntax_and_Expressions
2 Syntax_and_Expressions
❑ Java Program
❑ Java Statements
❑ Java Identifiers
❑ Java Keywords
❑ Java Literals
❑ Data Types
❑ Java Variables
❑ Constants
JAVA PROGRAM
Class name
public class Hello
{
public static void main
(String[] args) {
} System.out.println(“Hello, World!”);
}
Statement
JAVA PROGRAM
Statement
specifies one or more actions to be performed
during the execution of a program
Example:
System.out.print(“Hello, World!”);
JAVA STATEMENTS
Comment
is an optional statement used to describe what a
program or a line of program is doing
System.out.println(“Hello, World!”);
}
}
JAVA IDENTIFIERS
Identifiers
used to label variables, methods, classes, etc.
Case-sensitive
may contain letters, digits, underscore and dollar sign ($)
may not start with a digit
may not use Java keywords
JAVA IDENTIFIERS
Rules:
Identifiers can use alphabetic characters of
either case (a–z and A–Z), numbers (0–9),
underscores ( _ ), and dollar signs ( $ ).
Identifiers cannot start with a number.
Keywords cannot be used as identifiers (for
this reason keywords are sometimes called
reserved words).
JAVA IDENTIFIERS
Guidelines:
Name your identifiers close to its functionality.
Method and variable names start in lowercase
while classes start in uppercase.
For multi-word identifiers, either use
underscores to separate the words, or
capitalize the start of each word (camel
casing)
Avoid starting the identifiers using the
underscore.
JAVA IDENTIFIERS
Examples:
VALID INVALID
Student
pieMaster
pie_Master 4pie
pieMaster pie-pie
pie4 “pie,pie”
pie2pie pie/cake
dollar$man void
$dollarman
pieMethod
JAVA KEYWORDS
abstract extends new do this
boolean false null double threadsafe
break final package else throw
byte finally private interface transient
byvalue float protected super true
case for public switch while
catch goto import synchronized try
char if instanceof return void
class implements int short long
const continue default static native
JAVA LITERALS
Literals
are the representation of values
Integers
Floating Point Numbers
Booleans (true or false)
Strings (enclosed in “ “)
Characters (enclosed in ‘ ‘)
JAVA LITERALS
Integer Literals
are used to represent specific integer values
can be expressed in three different bases:
Octal (base 8)
Decimal (base 10)
Hexadecimal (base 16)
Examples:
0 0xDadaCafe
2 1996
0372 0x00FF00FF
JAVA LITERALS
Floating-Point Literal
appears in several forms
typical form makes use of digits and a decimal point
Examples of float literals:
Boolean Literal
only two Boolean literals exist: true and false,
representing the Boolean concepts of true and
false, respectively
JAVA LITERALS
String Literal
sequence of characters within double quotes
the characters can be escape sequences
Examples:
“Hello, world”
“That'll cost you two-fifty \n”
""
JAVA LITERALS
Character Literal
come in two forms which both use the single quote (‘ ’) as
a delimiter
Its first form places the literal character between single
quotes
Examples:
'a’
'+’
'$'
DATA TYPES
Composite
created by the programmer using simple types, arrays, classes,
and interfaces
DATA TYPES
Variable
is an item of data used to store the state
of objects
is composed of:
Data type
indicates the type of value that the variable can hold
Name
follows rules for identifiers
JAVA VARIABLES
Semicolon
int yourAg = 19 ;
e
Data type assigned value
JAVA VARIABLES
Example:
public class VarSample {
public static void main(String[]
args)
{
boolean result;
char option;
option = ‘C’;
double grade = 0.0;
}
}
JAVA VARIABLES
Displaying Variable values
Commands used:
System.out.print()
System.out.println()
Example:
public class VarOutput {
public static void main(String[] args) {
int value = 10;
char x;
x = ‘A’;
System.out.println(value);
System.out.println(“The value of x = ” + x);
}
}
JAVA VARIABLES
System.out.print()
does not append newline at the end of the data output
Example:
System.out.print(“Hello”);
System.out.print(“World”);
Output:
HelloWorld
JAVA VARIABLES
System.out.println()
Appends a newline at the end of the data output
Example:
System.out.println(“Hello”);
System.out.println(“World”);
Output:
Hello
World
CONSTANTS