Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Basic Language Elements

Shakir Hussain
Identifiers
y A name in a program is called an
Identifier.
y Used to denote classes, methods,
variables and labels.

y Each character can be either or


a
character in an Identifier must be
a (or underscore or currency
symbols like $, but never recommended)
2
Keywords

y Reserved words that are predefined in


the language and can not be used to
denote other entities.
y All keywords are in .
y A reserved word can not be used as an
Identifier.

3
4
Literals
y Denotes a constant value, the value that a
literal represents remains unchanged in
the program.

y Example :

5
Primitive Types and Variables

y boolean, char, byte, short, int, long, float, double etc.


y These basic (or primitive) types are the only types that
are not objects (due to performance issues).
y This means that you don’t use the new operator to create
a primitive variable.
y Declaring primitive variables:

6
Initialisation
y If no value is assigned to local variable prior to
use, then the compiler will give an error
y Java sets primitive variables to zero or false in
the case of a boolean variable[non-local
variable]
y All object references are initially set to null
y An array of anything is an object
◦ Set to null on declaration

7
Declarations

y 1.2f is a float value accurate to 7 decimal places.


y 1.2 is a double value accurate to 15 decimal places.

8
Assignment
y All Java assignments are right associative

y What is the value of a, b & c


y Done right to left:

9
Basic Mathematical Operators

are the mathematical operators


have a higher precedence than + or -

y Is the same as:

10
Statements & Blocks

y A simple statement is a command terminated by a


semi-colon:

y A block is a compound statement enclosed in curly


brackets:

y Blocks may contain other blocks

11
Flow of Control
y Java executes one statement after the other in
the order they are written
y Many Java statements are flow control
statements:

12
If – The Conditional Statement
y The if statement evaluates an expression and if that
evaluation is true then the specified action is taken

y If the value of x is less than 10, make x equal to 10


y It could have been written:

y Or, alternatively:

13
Relational Operators

== Equal (careful)
!= Not equal
>= Greater than or equal
<= Less than or equal
> Greater than
< Less than

14
If… else
y The if … else statement evaluates an expression and
performs one action if that evaluation is true or a
different action if it is false.

15
Nested if … else

16
else if
y Useful for choosing between alternatives:

17
A Warning…
WRONG! CORRECT!

18
The Statement

19
The loop
y Loop n times

y Nested for:

20
loops

What is the minimum number of times the loop is executed?


What is the maximum number of times?

21
loops

What is the minimum number of times the loop is executed?


What is the maximum number of times?

22
Break

y A break statement causes an exit from the


innermost containing while, do, for or switch
statement.

23
Continue
y Can only be used with while, do or for.
y The continue statement causes the innermost loop to
start the next iteration immediately

24
25

You might also like