01 Syntax and Grammars - PPSX
01 Syntax and Grammars - PPSX
01 Syntax and Grammars - PPSX
GRAMMAR
JAVA PROGRAM
► Sample program:
Class name
Hello
public class
{
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
► Types of comments in Java
► Block comments
► End-of-line comments
► JavaDoc comments
Java statements
► 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.
► 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”
“One\tTwo”
“TE\u0041”
“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
int yourAge =
19; Identifi
er
Semicolo
in yourAg = 19; n
t e
Data assigned
type value
Java variables
}
}
Java variables
► Displaying Variable values
► Commands used:
System.out.print()
System.out.println()
► Example:
► 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
Java variables
► Types of Variables
► Primitive Variables
►variables with primitive data types
►stores data in the actual memory location where
the variable is
► Reference Variables
►variables that stores the address in the memory
location
►points to another memory location where the
actual data is
constants