Download presentation
Presentation is loading. Please wait.
Published byShanon Hodge Modified over 9 years ago
1
Rina Zviel-Girshin @ARC1 System development with Java Instructor: Rina Zviel-Girshin Lecture 2
2
Rina Zviel-Girshin @ARC2 Java types Primitive (basic) types Boolean String Object references
3
Rina Zviel-Girshin @ARC3 Basic types Exist several basic types. The difference between them is their size, and therefore the values they can store: Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 10 18 +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 10 18
4
Rina Zviel-Girshin @ARC4 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
5
Rina Zviel-Girshin @ARC5 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'
6
Rina Zviel-Girshin @ARC6 Variable declaration Basic syntax: (final) type Identifier; or type Identifier1, Identifier2,.. IdentifierN; Example: int counter; double num1, num2=5.234; char letter=‘s’;
7
Rina Zviel-Girshin @ARC7 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
8
Rina Zviel-Girshin @ARC8 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;
9
Rina Zviel-Girshin @ARC9 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
10
Rina Zviel-Girshin @ARC10 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 \ */
11
Rina Zviel-Girshin @ARC11 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
12
Rina Zviel-Girshin @ARC12 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
13
Rina Zviel-Girshin @ARC13 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.
14
Rina Zviel-Girshin @ARC14 Separators The following nine ASCII characters are the separators (punctuators): () {} [] ;,.
15
Rina Zviel-Girshin @ARC15 Operators The following 37 tokens are the operators, formed from ASCII characters: = ><!~?: == =!=&&||++ --+-*/&| ^% >>>>+=-= *=/=&=|=^=%=
16
Rina Zviel-Girshin @ARC16 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.
17
Rina Zviel-Girshin @ARC17 Arithmetic operators Arithmetic operators are: + additionx+y - subtractionx-y * multiplicationx*y / divisionx/y % remainderx%y Also unary – for the negation. a = -34;
18
Rina Zviel-Girshin @ARC18 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
19
Rina Zviel-Girshin @ARC19 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
20
Rina Zviel-Girshin @ARC20 Increment/decrement operators The usage of increment and decrement operators: Example: // usage of increment and decrement operators class 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. OperatorUseDescription ++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
21
Rina Zviel-Girshin @ARC21 Example // usage of increment and decrement operators class IncExample{ public static void main(String args[]) { int j=10; System.out.println(++j +“ “+ j++ +“ “+ j +“ “+ --j ); } } The output is: 11 11 12 11
22
Rina Zviel-Girshin @ARC22 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 >= 23 + 6 = = 2 + 75 ! = 8 + 9
23
Rina Zviel-Girshin @ARC23 Relational and Conditional operators The conditional operators can have two values: true false Expressions that use conditional operators called boolean expressions.
24
Rina Zviel-Girshin @ARC24 Binary conditional operators OperatorUseReturns true if &&op1 && op2op1 and op2 are both true, conditionally evaluates op2 ||op1 || op2either op1 or op2 is true, conditionally evaluates op2 !! opop is false &op1 & op2op1 and op2 are both true, always evaluates op1 and op2 |op1 | op2either op1 or op2 is true, always evaluates op1 and op2 ^op1 ^ op2if op1 and op2 are different--that is if one or the other of the operands is true but not both
25
Rina Zviel-Girshin @ARC25 Flow control statements Control structures can be divided into two classes: loop statements for while do-while branching statements if switch
26
Rina Zviel-Girshin @ARC26 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.
27
Rina Zviel-Girshin @ARC27 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.
28
Rina Zviel-Girshin @ARC28 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.
29
Rina Zviel-Girshin @ARC29 Example class 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”); }
30
Rina Zviel-Girshin @ARC30 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.
31
Rina Zviel-Girshin @ARC31 Example class 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!!!”); }
32
Rina Zviel-Girshin @ARC32 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; }
33
Rina Zviel-Girshin @ARC33 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.
34
Rina Zviel-Girshin @ARC34 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.
35
Rina Zviel-Girshin @ARC35 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”); }
36
Rina Zviel-Girshin @ARC36 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.
37
Rina Zviel-Girshin @ARC37 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.
38
Rina Zviel-Girshin @ARC38 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.
39
Rina Zviel-Girshin @ARC39 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); }
40
40 The for Statement diagram
41
Rina Zviel-Girshin @ARC41 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.
42
Rina Zviel-Girshin @ARC42 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); }
43
Rina Zviel-Girshin @ARC43 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.
44
Rina Zviel-Girshin @ARC44 The do.. while statement diagram
45
Rina Zviel-Girshin @ARC45 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.
46
Rina Zviel-Girshin @ARC46 Infinite loop example // this program contains two infinite loops class 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); } }
47
Rina Zviel-Girshin @ARC47 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.
48
Rina Zviel-Girshin @ARC48 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.
49
Rina Zviel-Girshin @ARC49 Style Example Bad: 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!"); }
50
Rina Zviel-Girshin @ARC50 Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.