0% found this document useful (0 votes)
3 views13 pages

L5 NormalSyntax

The document is a lecture on Java programming elements, covering topics such as Java tokens, keywords, identifiers, variables, named constants, and naming conventions. It explains how to write a program by translating algorithms into code, including the importance of variables and constants. The lecture also provides rules for defining identifiers and best practices for naming conventions in Java.

Uploaded by

lihoci1760
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)
3 views13 pages

L5 NormalSyntax

The document is a lecture on Java programming elements, covering topics such as Java tokens, keywords, identifiers, variables, named constants, and naming conventions. It explains how to write a program by translating algorithms into code, including the importance of variables and constants. The lecture also provides rules for defining identifiers and best practices for naming conventions in Java.

Uploaded by

lihoci1760
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

Java Programming Elements

Lecture 5

Department of Computer Science and Engineering, ITER


Siksha ’O’ Anusandhan (Deemed to be University), Bhubaneswar, Odisha, India.

1 / 13
Contents

1 Introduction

2 Java Tokens

3 Keywords

4 Identifiers

5 Variables

6 Named Constants

7 Naming Conventions

2 / 13
Introduction

Let’s first consider the simple problem of computing the area of a


circle.

Writing a program involves designing algorithms and


translating algorithms into programming instructions, or
code.

Algorithm 1 Calculating the area of a circle


1: Step 1: Read the radius of the circle.
2: Step 2: Compute the area using the following formula:
3: area = radius * radius * π
4: Step 3: Display the result.

3 / 13
Writing the Program

When you write a program, you translate an algorithm into a


program.
In this case, the program needs to read the radius entered by the
user from the keyboard.
This raises two important issues:
Reading the radius.
Storing the radius in the program.

In order to store the radius, the program needs to declare a


symbol called a variable.
Similarly, when you calculate the area you are going to use the
mutpilaction(*) operator.
These java constuct are collectively called java token. 4 / 13
Java Tokens

In a Java program, all characters are grouped into symbols


called tokens.
A token is the smallest element of a program that is
meaningful to the compiler. Tokens can be classified as follows:
Keywords
Identifiers
Constants
Special Symbols
Operators

5 / 13
Keywords

In Java, a keywords are the reserved word with a predefined


meaning in Java programming language syntax.
These keywords may not be used as identifiers for naming
variables, classes,methods or other entities.
There are 51 reserved keywords in the Java programming
language.
They are usually highlighted with different colors in most
integrated development environments (IDE).
Common Java keywords include abstract, assert, boolean,
break, byte, case, catch, char, class, const, continue, default,
do, for, while, switch, this and int.
6 / 13
Identifiers

A Java identifier is a name given to a package, class, interface,


method, or variable. It allows a programmer to refer to the item
from other places in the program.

p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
i n t a = 20;
}
}

In the above java code, we have 5 identifiers: Test, main, String,


args, and a.
7 / 13
Rules for defining Java Identifiers

An identifier is a sequence of characters that consists of letters,


digits, underscores ( ), and dollar signs ($).

An identifier must start with a letter, an underscore ( ), or a


dollar sign ($). It cannot start with a digit.

An identifier cannot be a reserved word.

An identifier cannot be true, false, or null.

There is no limit on the length of the identifier but it is advisable to


use an optimum length of 4 – 15 letters only.

8 / 13
Variables

A variable is a container which holds the value while the Java


program is executed.

The value stored in a variable can be changed during program


execution.
Each variable in Java has a specific type, which determines the
Size and layout of the variable’s memory.
The range of values that can be stored.
Set of operations that can be applied to the variable.

To use a variable, you declare it by telling the compiler its name


as well as what type of data it can store.

9 / 13
Declaring a Variable

The syntax for declaring a variable is:


datatype variableName;
Example of variable declaration:
double radius ;
If variables are of the same type, they can be declared together.
int i,j,k;
You can declare a variable and initialize it in one step.
int count = 1;
You can also use a shorthand form to declare and initialize
variables of the same type together.
int i = 1, j = 2;
10 / 13
Named Constants

A named constant is an identifier that represents a


permanent value.
For example the value of π is a constant.
The syntax for declaring a constant:
final datatype CONSTANTNAME = value;
A constant must be declared and initialized in the same
statement.
The keyword final is used for declaring a constant.
There are three benefits of using constants:
No repeate typing.
Easy of changing the value.
Makes the program easy to read.
11 / 13
Naming Conventions

Choose descriptive names with straightforward meanings for


the variables, constants, classes, and methods.
Use lowercase for variables and methods.
If a name consists of several words, concatenate them into one,
making the first word lowercase and capitalizing the first letter
of each subsequent word.

Capitalize the first letter of each word in a class name.

Capitalize every letter in a constant, and use underscores


between words.

12 / 13
References

Y Daniel Liang ‘Introduction to JAVA Programming, Comprehensive Version’,


Pearson, 2014.

13 / 13

You might also like