You are on page 1of 33

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
 A minimum Java program has the following
format:

public class <classname>


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

*Property of STI J0007


public class Hello
{
public static void main
(String[] args) {
Java Program System.out.println(“Hello,
World!”);
}
}

*Property of STI J0007


 Java program files must
 Have same name as public class
 End with extension .java

 Use comments for documentation and


Java Coding readability
Guidelines
 White spaces are ignored

 Indent for readability

*Property of STI J0007


 A statement is one or more lines of code
terminated by a semicolon.

Java
Statements Example:

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

*Property of STI J0007


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

 Block statements can be nested indefinitely.

 Any amount of white space is allowed.

Java Blocks Example:

public static void main (String[]


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

*Property of STI J0007


 A comment is an optional statement used to
describe what a program or a line of program is
doing.

Java
Comments // This is a comment
/* This is a comment */
/** This is a special comment **/
/** for documentation **/

*Property of STI J0007


 Comment lines are ignored by the
compiler.

public class Hello {


/**
Example: * My First Java Program

Comments */
public static void main (String[] args) {
// print “Hello, World!” on screen
System.out.println(“Hello, World!”);
}
}

*Property of STI J0007


 Identifiers used to label variables, methods,
classes, etc.

 Case-sensitive
Java  May contain letters, digits, underscore and
Identifiers dollar sign ($)

 May not start with a digit

 May not use Java keywords

*Property of STI J0007


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.
Java  Keywords cannot be used as identifiers (for this reason keywords
are sometimes called reserved words).
Identifier Guidelines:
Rules  Name your identifiers close to its functionality.
and  Method and variable names start in lowercase while classes start
Guidelines 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


Java
Identifier:
Example

*Property of STI J0007


Java
Keywords

*Property of STI J0007


 Literals are the representation of values.

 Integers
 Floating Point Numbers
 Booleans (true or false)
 Strings (enclosed in “ “)
Java Literals  Characters (enclosed in ‘ ‘)

\n new line
\t tab
\r carriage return
\” double quote
\’ single quote
\\ backslash

*Property of STI J0007


 can be expressed in three different bases:
 Octal (base 8)
 Decimal (base 10)
 Hexadecimal (base 16)

Integer Examples of int literals:


Literal
0 0xDadaCafe
2 1996
0372 0x00FF00FF

*Property of STI J0007


 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 Examples of float literals:


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

Examples of double literals:

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

*Property of STI J0007


only two Boolean literals exist: true
Boolean and false, representing the Boolean
Literal concepts of true and false,
respectively

*Property of STI J0007


 sequence of characters within double quotes
 the characters can be escape sequences

Examples of string literals include:

String Literal "Hello, world“


"One\tTwo"
"TE\u0041“
"That'll cost you two-fifty \n"
"" // empty string

*Property of STI J0007


 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 '$'.

Character
Literal  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.

*Property of STI J0007


There are two kinds of data types:

Simple
 built-in or primitive Java data types
Data Types
Composite
 created by the programmer using simple
types, arrays, classes, and interfaces

*Property of STI J0007


 The Java programming language defines eight
primitive data types:

 boolean (for logical)


 char (for textual)
Primitive Data  byte
Types  short
 int
 long (integral)
 double
 float (floating-point)

*Property of STI J0007


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

boolean result = true;

*Property of STI J0007


 represents a 16-bit Unicode character

 must have its literal enclosed in single quotes (‘’)

Char Data  uses the following notations


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

*Property of STI J0007


Integer Data
Type

*Property of STI J0007


 has types: float (32 bits single precision)
and double (64-bit double precision)

Floating Point
Data Type

*Property of STI J0007


 A variable is an item of data used to store
the state of objects.

 A variable is composed of:


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

*Property of STI J0007


 A variable is declared as follows:

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


Declaring
and
Initializing
Variables  Note: Values enclosed in <> are required values,
while those values in [] are optional.

*Property of STI J0007


Identifier

Declaring
and int yourAge = 19;
Initializing
Variables Semicolon
Data type Optional: assigned value

*Property of STI J0007


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


public class VarSample {
public static void main(String[]
args)
{
boolean result;
Java Variable: char option;
Example option = „C‟;
double grade = 0.0;
}
}

*Property of STI J0007


 To display the value of a certain variable, we use the following
commands:
System.out.print()
System.out.println()
Example:

public class VarOutput {

Displaying public static void main(String[] args) {

Variable int value = 10;


char x;
Data
x = „A‟;

System.out.println(value);
System.out.println(“The value of x = ” + x);
}
}

*Property of STI J0007


 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

 System.out.println()
Displaying  Appends a newline at the end of the data output
Variable Data  Example:
System.out.println(“Hello”);
System.out.println(“World”);
 Output:
Hello
World

*Property of STI J0007


 Primitive Variables
 variables with primitive data types
 stores data in the actual memory location where the
variable is
Types of
Variables  Reference Variables
 variables that stores the address in the memory location
 points to another memory location where the actual data is

*Property of STI J0007


 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

You might also like