You are on page 1of 9

SYNTAX AND GRAMMARS

Java Program
Java Coding Guidelines
Java Comments
Java Identifiers
Java Keywords
Java Literals
Java Data Types
Java Variables
Java Constant

*Property of STI

J0007

1 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

A minimum Java program has the following


format:

Java Program

*Property of STI

System.out.println(Hello,
World!);

J0007

2 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Java program files must
Have same name as public class
End with extension .java

Java Program

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

public class Hello


public static void main
(String[] args) {

public class <classname>

Java Coding
Guidelines

Use comments for documentation and


readability
White spaces are ignored

}
Indent for readability

*Property of STI

J0007

3 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

4 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

A block is formed by enclosing statements by curly


braces.
Block statements can be nested indefinitely.

A statement is one or more lines of code


terminated by a semicolon.

Java
Statements

Any amount of white space is allowed.

Java Blocks

Example:

public static void main (String[]


args) {
System.out.println(Hello);
System.out.print(World!);
}

System.out.print(Hello, World!);

*Property of STI

J0007

5 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Example:

*Property of STI

J0007

6 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Comment lines are ignored by the
compiler.

A comment is an optional statement used to


describe what a program or a line of program is
doing.

public class Hello {


/**

Java
Comments

// This is a comment
/* This is a comment */

Example:
Comments

* My First Java Program


*/
public static void main (String[] args) {
// print Hello, World! on screen

/** This is a special comment **/

System.out.println(Hello, World!);

/** for documentation **/

}
}

*Property of STI

J0007

7 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

8 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Rules:
Identifiers can use alphabetic characters of either case (az and
AZ), numbers (09), underscores ( _ ), and dollar signs ( $ ).

Identifiers used to label variables, methods,


classes, etc.
Case-sensitive

Java
Identifiers

May contain letters, digits, underscore and


dollar sign ($)

May not start with a digit

Identifiers cannot start with a number.

Java
Identifier
Rules
and
Guidelines

May not use Java keywords

*Property of STI

J0007

9 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Java
Identifier:
Example

*Property of STI

Keywords cannot be used as identifiers (for this reason keywords


are sometimes called reserved words).
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.

*Property of STI

J0007

10 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Java
Keywords

J0007

11 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

12 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

can be expressed in three different bases:

Literals are the representation of values.

Java Literals

\n
\t
\r
\
\
\\

*Property of STI

Octal (base 8)
Decimal (base 10)
Hexadecimal (base 16)

Integers
Floating Point Numbers
Booleans (true or false)
Strings (enclosed in )
Characters (enclosed in )
new line
tab
carriage return
double quote
single quote
backslash

J0007

13 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Integer Examples of int literals:


Literal
0

0xDadaCafe

1996

0372 0x00FF00FF

*Property of STI

J0007

14 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

appears in several forms


typical form makes use of digits and a decimal point
note that digits may appear before or after or before and
after the decimal point

Floating-Point
Literal

Examples of float literals:

Boolean
Literal

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

only two Boolean literals exist: true


and false, representing the Boolean
concepts of true and false,
respectively

Examples of double literals:


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

*Property of STI

J0007

15 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

16 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

sequence of characters within double quotes

the characters can be escape sequences

Character literals come in two forms. They both use the


single quote ( ) as a delimiter. The first form places the
literal character between single quotes. Examples include
'a', '+', and '$'.

Examples of string literals include:

String Literal

Character
Literal

"Hello, world
"One\tTwo"
"TE\u0041

Some characters, such as the newline character, don't


have visible literal representations. For these, an escape
sequence must be used, which consists of the backslash
("\") followed by a code.

"That'll cost you two-fifty \n"


"" // empty string

*Property of STI

J0007

17 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

18 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

There are two kinds of data types:

The Java programming language defines eight


primitive data types:

Simple

built-in or primitive Java data types

Data Types

Primitive Data
Types

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

*Property of STI

J0007

19 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

J0007

*Property of STI

boolean (for logical)


char (for textual)
byte
short
int
long (integral)
double
float (floating-point)

J0007

20 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

represents a 16-bit Unicode character

one-bit wide and takes on the values true or


false

must have its literal enclosed in single quotes ()

default value: false


cannot be cast to a number or any other type

Char Data uses the following notations


a
- the letter a
Type

Boolean
Data Type Example:

\t
\u????

- a tab
- a specific Unicode character, ????
is replaced with exactly four
hexadecimal digits (for example,
\u03A5 is the Greek letter phi, )

boolean result = true;

*Property of STI

J0007

21 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

22 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
has types: float (32 bits single precision)
and double (64-bit double precision)

Integer Data
Type

*Property of STI

Floating Point
Data Type

J0007

23 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

24 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

A variable is declared as follows:

A variable is an item of data used to store


the state of objects.

Java Variables

A variable is composed of:


Data type indicates the type of value that
the variable can hold
Name must follow rules for identifiers

*Property of STI

J0007

*Property of STI

25 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Identifier

Declaring
and
Initializing
Variables

int yourAge = 19;


Data type

*Property of STI

Declaring
and
Initializing
Variables

Semicolon
Optional: assigned value

J0007

27 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

<data type> <name> [=initial value];

Note: Values enclosed in <> are required values,


while those values in [] are optional.

J0007

26 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

1.Always initialize your variables as you


Guidelines in declare them.
Declaring
and
Initializing 2.Use descriptive names for your variables.
Variables
3.Declare one variable per line of code.

*Property of STI

J0007

28 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

To display the value of a certain variable, we use the following


commands:

public class VarSample {


public static void main(String[]
args)
{
boolean result;

Java Variable:
Example

char option;

option = C;
double grade = 0.0;

System.out.print()

System.out.println()
Example:

public class VarOutput {

Displaying
Variable
Data

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);


}
}

*Property of STI

J0007

29 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

30 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

System.out.print()
Does not append newline at the end of the data output
Example:
System.out.print(Hello);
System.out.print(World);

Primitive Variables

Output:
HelloWorld

Displaying
Variable Data

System.out.println()
Appends a newline at the end of the data output
Example:

Types of
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

System.out.println(Hello);
System.out.println(World);

Output:
Hello
World

*Property of STI

J0007

31 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*Property of STI

J0007

32 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

value never changes

Use the final type modifier in class definition

Constants

public class Variables {


public static void main(String[] args)
{
final double PI = 3.14;
}

*Property of STI

J0007

33 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

You might also like