50
Rina Zviel-Girshin @AR C 1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Embed Size (px)

Citation preview

Page 1: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 1

System development with Java

Instructor: Rina Zviel-Girshin

Lecture 2

Page 2: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 2

Java types

• Primitive (basic) types• Boolean • String• Object references

Page 3: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 3

Basic types • Exist several basic types.• The difference between them is their size, and

therefore the values they can store:Type

byteshortintlong

floatdouble

Storage

8 bits16 bits32 bits64 bits

32 bits64 bits

Min Value

-128-32,768-2,147,483,648< -9 x 1018

+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digits

Max Value

12732,7672,147,483,647> 9 x 1018

Page 4: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 4

Boolean Literals

• The boolean type has two values, represented by the literals true and false

• A boolean literal is always of type boolean. • BooleanLiteral is one of:

true false  

Page 5: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 5

Character Literals

• A character literal is expressed as a character or an escape sequence, enclosed in single quotes.

• A character literal is always of type char.

  Examples of char literals:

'a' '%' '\t' '\\' '\u03a9'

'\uFFFF' '\177'

Page 6: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 6

Variable declaration

• Basic syntax:

(final) type Identifier; or

type Identifier1, Identifier2, .. IdentifierN;

Example:int counter;double num1, num2=5.234;char letter=‘s’;

Page 7: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 7

Basic assignment

• Syntax:

Identifier=Expression;

where = is assignment operator and result of the expression is stored in Identifier.

Example:

counter=0;

counter=counter+1;

value=(sum*1.05)/12

Page 8: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 8

Constant• A constant is an identifier that is similar to the

variable except that it holds one value for it's entire lifecycle.

• We use final modifier to declare a constant.

• Syntax:final type Identifier=value;

Example:final int maximal_number_of_students=30;

Page 9: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 9

String Literals

• A string literal consists of zero or more characters enclosed in double quotes (“).  

• A string literal is always of type String.• String is a Java class. So string literal is a java object.

Examples of string literals: "" // the empty string

"This is a string" // a string containing 16 characters

Page 10: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 10

Escape Sequences for Character and String Literals

  Escape Sequence:\ b /* \u0008: backspace BS */\ t /* \u0009: horizontal tab HT */\ n /* \u000a: linefeed LF */\ f /* \u000c: form feed FF */\ r /* \u000d: carriage return CR */\ " /* \u0022: double quote " */\ ' /* \u0027: single quote ' */\ \ /* \u005c: backslash \ */

Page 11: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 11

String Concatenation

• The ‘+’ operator between strings has the meaning of String concatenation.

“The international “ + “dialing code”

• When we apply ‘+’ upon a String and a value of another type, that value is first converted into a String and the result is the concatenation of the two Strings.

“for Israel is “ + 972

Page 12: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 12

String Concatenation

class IsraelCode { public static void main(String[] args) {

System.out.print(“The international “ + “dialing code”); System.out.println(“for israel is “ + 972); }}

Output: The international dialog code for Israel is 972

Page 13: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 13

The Null Literal

• The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters.

 • A null literal is always of the null type.

Page 14: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 14

Separators

The following nine ASCII characters are the separators (punctuators):

 ( )  { }  [ ]  ; , .

Page 15: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 15

Operators

The following 37 tokens are the operators, formed from ASCII characters:

= > < ! ~ ? :

== <= >= != && || ++

-- + - * / & |

^ % << >> >>> += -=

*= /= &= |= ^= %=

Page 16: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 16

Operators • Java operators can be either:

• Unary operator - takes a single value • Binary operator - takes two values

• Java binary operators are written in the infix notation:

operand1 operator operand2 that means - the operator is written inside (between)

the operands.

Page 17: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 17

Arithmetic operators

Arithmetic operators are:+ addition x+y- subtraction x-y* multiplication x*y/ division x/y% remainder x%y 

Also unary – for the negation.a = -34; 

Page 18: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 18

Arithmetic operators

• Arithmetic operators depends on types of the operands.

Example: 3.0 / 2.0 1.5 3 / 2.0 1.5 3.0 / 2 1.5 3 / 2 1 -3 / 2 -1

Page 19: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 19

Increment/decrement operators

• The ++ and -- are the increment and decrement operators.

• j++ is equivalent to j=j+1.•  The increment and decrement operators can be

• prefix – can appear before what they operate on

• postfix – can appear after what they operate on

Page 20: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 20

Increment/decrement operators

The usage of increment and decrement operators: 

Example:// usage of increment and decrement operatorsclass IncExample{public static void main(String args[])

{ int j=10; System.out.println(++j +“ “+ j++ +“ “+ j +“ “+ --j );}

} The output is:11 11 12 11 The first value j is printed preincremented to 11.The second value j is printed after the increment, but before it is postincremented to 12.The third value is printed after its postincrement from the middle term.The forth value is printed predecremented to 11.

Operator Use Description

++ j++ increments j by 1

evaluates to the value of j before it was incremented

++ ++j increments j by 1

evaluates to the value of j after it was incremented

-- j-- decrements j by 1

evaluates to the value of j before it was decremented

-- --j decrements j by 1

evaluates to the value of j after it was decremented

Page 21: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 21

Example

// usage of increment and decrement operatorsclass IncExample{ public static void main(String args[]) { int j=10; System.out.println(++j +“ “+ j++ +“ “+ j +“ “+ --

j ); }}

 The output is:

11 11 12 11

Page 22: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 22

Relational and Conditional operators

A standard set of relational and equality operators is:> greater than

>= greater than or equal to

< less than

<= less than or equal to

= = equal to

! = not equal to

 Example:

5 >= 2 3 + 6 = = 2 + 7 5 ! = 8 + 9

Page 23: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 23

Relational and Conditional operators

• The conditional operators can have two values:• true• false

•  Expressions that use conditional operators called boolean expressions.

Page 24: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 24

Binary conditional operators

 Operator Use Returns true if

&& op1 && op2 op1 and op2 are both true, conditionally evaluates op2

|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2

! ! op op is false

& op1 & op2 op1 and op2 are both true, always evaluates op1 and op2

| op1 | op2 either op1 or op2 is true, always evaluates op1 and op2

^ op1 ^ op2 if op1 and op2 are different--that is if one or the other of the operands is true but not both

Page 25: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 25

Flow control statements

• Control structures can be divided into two classes:• loop statements

forwhiledo-while

• branching statementsifswitch

Page 26: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 26

Blocks• Block is the simplest type of statement.Syntax:

{// statements}

• It implements grouping of a sequence of statements into a single statement.

•  Block can be empty - contain no statements. 

Page 27: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 27

Blocks

{ float k;

k = (k+5)/32;

System.out.println(k);

}• A variable k declared inside a block is completely

inaccessible and invisible from outside that block. • Such a variable is called local to the block.• It can be redefined later.

Page 28: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 28

If statement

• A choice between doing something and not doing it.

 Syntax:

if (boolean _condition)

statement;• Boolean_condition is a boolean statement that can

have true or false values.• If the value is true than the statement is performed.• If the value is false than the statement is skipped.

Page 29: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 29

Exampleclass FailTest {

public static void main(String[] args)

{

InputRequestor input = new InputRequestor();

int grade = input.requestInt(“Enter your grade:”);

System.out.println(“Your grade is: ”+grade);

if (grade < 60)

System.out.println(“You failed”);

}

}

Page 30: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 30

If .. else statement• A choice between doing two things.Syntax: if (boolean _condition)

statement1;else

statement2;

• First boolean _condition statement value is evaluated.• The value can be:

true and than statement1 is performed and statement2 is skipped

false and than statement1 is skipped and statement2 is performed.

Page 31: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 31

Exampleclass FailTest{ public static void main(String[] args)

{ InputRequestor input=new InputRequestor(); int grade=input.requestInt(“Enter your grade:”); System.out.println(“Your grade is: ”+grade); if (grade < 60) System.out.println(“You failed”); else

System.out.println(“You passed!!!”);}

}

Page 32: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 32

Switch statement • A choice between doing several things (usually

more then two things).• For each choice we have different set of

instructions to perform. Syntax:switch(x){ case value1: statement1; break;

… case valuen: statementn; break; default: default_ statement; break;}

Page 33: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 33

Switch statement

• x can be an expression but it must evaluate an integer value.

• statementN can be empty or can be a set of instructions.

• The break statement is used to terminate the statement list of each case.It allows you to directly exit a loop.

Page 34: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 34

Switch statement

• The choice is done by value of x.If the value of x equals to value1 the statement1

is performed.…If the value of x equals to valuen the statementn

is performed.If the value of x different from values of

value1,..,valuen then default_statement is performed.

Page 35: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 35

Example

switch(letter){case ‘a’: System.out.println(“The letter was a”);

add();break;

case ‘d’: System.out.println(“The letter was d”); delete();

break;default: System.out.println(“Illegal input”);}

Page 36: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 36

Loops

• Several control structures in Java are loop control structures.

• A loop is a repetition of certain pieces of the code several times.

•  Loop can be:– bounded – or “unbounded” depends on number of times we want to perform

it.

Page 37: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 37

For statement

Syntax:

for( start; limit; increment/decrement)

{ statement; }• Start is a statement that initializes the loop.• Limit is a boolean statement that determines when

to terminate the loop. • The limit value is evaluated during each iteration

of the loop.

Page 38: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 38

For statement

• There are two possible values:• true and the statement is performed• false and the loop is terminated

• Increment/decrement is an expression that is invoked after each iteration of the loop and also called step of the loop.

• The for loop often used for counting from start to limit by a step size.

 

Page 39: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 39

For example

class For_Example{ public static void main(String[] args)

{ int factorial=1; for( int k=1; k<5; k++) factorial*=k; System.out.println(“The factorial of 5 numbers is: “ +

factorial); }}

Page 40: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 40

The for Statement diagram

start

statement

increment/decrement

limitfalse

true

Page 41: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 41

While statement

Syntax:while( boolean_condition)

statement; • The value of boolean_condition can be:

– true and than the statement is performed– false and than loop terminates

• The statement is executed over and over until the boolean_condition becomes false.

Page 42: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 42

While statement

Example:int sum=0, k=0;

while(sum<100)

{ sum=sum+k;

k++;

System.out.print(“the sum of “ + k + ” elements is ” + sum);

}

Page 43: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 43

Do.. while statement

Syntax:do{ statement; } while (boolean_condition);•  The statement performed first of all. • Than the boolean_condition is checked. If it is true

the next iteration of the loop is performed. If it is false than the loop terminates. 

• The statement performed at least once.

Page 44: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 44

The do.. while statement diagram

statement

booleancondition

false

true

Page 45: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 45

Infinite loops

• If boolean_condition in one of loop structures is always true then loop will be executed forever- it is ‘unbounded’.

• Such a loop called infinite loop.• The infinite loop will execute until user interrupts

the program.

Page 46: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 46

Infinite loop example

// this program contains two infinite loopsclass Infinity{ public static void main(String[] args)

{ int count=0; for( ; ; ) System.out.print(“Infinity”); while(count<10) { System.out.println(“Another infinite loop”);

System.out.println(“The counter is “+counter); }}

}

Page 47: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 47

Programming style

• Java is a free-format language. • There are no syntax rules about how the program

has to be arranged on a page. You can write entire program in one line.

• But as a matter of good programming style, you should lay out your program on the page in a way that will make its structure as clear as possible.

Page 48: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 48

Programming style

• Some advises:• Put one statement per line. • Use indentation to indicate statements that

are contained inside control structures. • Write comments.• Give your variables names that make sense.

Page 49: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 49

Style ExampleBad:

public class

Stam

{ public static void

main(String args[]){System.out.println("Hello!“);}}

Better:

// style example – outputs “Hello!”

public class Hello

{

public static void main(String args[])

{

System.out.println("Hello!");

}

}

Page 50: Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2

Rina Zviel-Girshin @ARC 50

Any Questions?