You are on page 1of 36

SYNTAX AND

GRAMMAR
JAVA PROGRAM

► A minimum Java program has the following format:

public class <classname>


{
public static void main
(String[] args) {
<program statement>
}
}
JAVA PROGRAM

► Sample program:
Class name
Hello
public class
{
public static void main
(String[] args) {
System.out.println(“Hello, World!”);

}
} Statement
JAVA Program

► Java Coding Guidelines


► Java program files must have the same name as
public class and end with a .java extension
► Use comments for documentation and readability
► White spaces are ignored
► Indent for readability
JAVA STATEMENTS

► Statement
► specifies one or more actions to be performed during the
execution of a program
► Example:

System.out.print(“Hello, World!”);
Java statements

► A block is formed by enclosing statements by curly braces.


► Block statements can be nested indefinitely.
► Example:
public static void main (String[] args)
{
System.out.println(“Hello”);
System.out.print(“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

► Examples: End-of-line comment


// This is a comment Block comment
/* This is a comment */
JavaDoc
/** This is a special comment comment
**/
/** for documentation **/
Java statements

public class Hello {


/**
* My First Java Program
*/
public static void main (String[] args) {
// print “Hello, World!” on screen
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.
► 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:

1e1f 2.f .3f 0f 3.14f 6.022137e+23f


► Examples of double literals:

1e1 2. .3 0.0 3.14 1e-9d 1e137


JAVA 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

► Two kinds of data types:


► Simple
►built-in or primitive Java data types

► Composite
►created by the programmer using simple types,
arrays, classes, and interfaces
Data types

► Primitive data types in Java:


► boolean (for logical)
► char (for textual)
► byte
► short
► int
► long (integral)
► double
► float (floating-point)
Data types

► Boolean Data Type


► one-bit wide and takes on the values true or false
► default value: false
► cannot be cast to a number or any other type
► Example:

boolean result = true;


Data types

► Char Data Type


► represents a 16-bit Unicode character
► must have its literal enclosed in single quotes (‘’)
► uses the following notations:

‘a’ - the letter a


‘\t’ - tab
‘\u????’ - a specific Unicode character

- ???? is replaced with exactly four


hexadecimal digits
Data types

► Integer Data type


Type Minimum Value Maximum Value Size
in
Bytes
byte -128 127 1
short -32768 32767 2
int -2,147,483,648 2,147,483,647 4
long -9,223,372,036,854,775,808 9,223,372,036,854,775,807 8
Data types

► Floating Data Type


► Types:
►float (32 bits single precision)
►double (64-bit double precision)

Type Minimum Maximum Size in


Value Value Bytes
float -3.4 * 1018 3.4 * 1018 4

double -1.7 * 10308 1.7 * 10308 8


Java variables

► 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

► Declaration and Initialization of variables


► Variable declaration:
<data type> <name>;
► Variable initialization:
<data type> <name> =
[initial value];
Java variables
► Example:

int yourAge =
19; Identifi
er

Semicolo
in yourAg = 19; n

t e
Data assigned
type value
Java variables

► Guidelines in Declaring and Initializing Variables


► Always initialize your variables as you declare them.
► Use descriptive names for your variables.
► Declare one variable per line of code.
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
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

► its value never changes


► uses the final type modifier in class definition
► Example:
public class Variables {
public static void main(String[]
args) {
final double PI = 3.14;
}
}

You might also like