0% found this document useful (0 votes)
10 views

2 Syntax_and_Expressions

The document provides an overview of Java programming syntax and grammar, covering essential elements such as Java programs, statements, identifiers, keywords, literals, data types, variables, and constants. It includes examples of a simple Java program, coding guidelines, and rules for naming identifiers. Additionally, it explains the different types of literals and data types available in Java, along with best practices for declaring and initializing variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

2 Syntax_and_Expressions

The document provides an overview of Java programming syntax and grammar, covering essential elements such as Java programs, statements, identifiers, keywords, literals, data types, variables, and constants. It includes examples of a simple Java program, coding guidelines, and rules for naming identifiers. Additionally, it explains the different types of literals and data types available in Java, along with best practices for declaring and initializing variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

SYNTAX AND GRAMMAR

❑ Java Program
❑ Java Statements
❑ Java Identifiers
❑ Java Keywords
❑ Java Literals
❑ Data Types
❑ Java Variables
❑ Constants
JAVA PROGRAM

A minimum Java program has the following format:

public class <classname>


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

‘public’ means that the method is


accessible from everyone
‘static’ means that you can call the
method without creating an object
‘void’ means that the method has
no return value
JAVA PROGRAM
Sample program:

Class name
public class Hello
{
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:

// This is a comment End-of-line comment


/* This is a comment */ Block comment
/** This is a special comment **/ JavaDoc
/** for documentation **/ comment
JAVA STATEMENTS
public class Hello {
/**
* My First Java Program
*/
// print “Hello, World!” on screen

public static void main (String[] args) {

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:

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”
“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

Floating Data Type


Types:
float (32 bits single precision)
double (64-bit double precision)
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;
Identifier

Semicolon
int yourAg = 19 ;
e
Data type assigned 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
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