1 / 50

System development with Java

System development with Java. Instructor: Rina Zviel-Girshin Lecture 2. Java types. Primitive (basic) types Boolean String Object references. 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

heatha
Download Presentation

System development with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System development with Java Instructor: Rina Zviel-Girshin Lecture 2 Rina Zviel-Girshin @ARC

  2. Java types • Primitive (basic) types • Boolean • String • Object references Rina Zviel-Girshin @ARC

  3. 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 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 Basic types • Exist several basic types. • The difference between them is their size, and therefore the values they can store: 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 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' 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’; 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 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; 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 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 \ */ 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 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); } } The international dialog code for Israel is 972 Output: 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. Rina Zviel-Girshin @ARC

  14. Separators The following nine ASCII characters are the separators (punctuators): ( )  { }  [ ]  ; , . Rina Zviel-Girshin @ARC

  15. Operators The following 37 tokens are the operators, formed from ASCII characters: = > < ! ~ ? : == <= >= != && || ++ -- + - * / & | ^ % << >> >>> += -= *= /= &= |= ^= %= 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. 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;  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 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 Rina Zviel-Girshin @ARC

  20. 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. Rina Zviel-Girshin @ARC

  21. 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 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 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. Rina Zviel-Girshin @ARC

  24. Binary conditional operators Rina Zviel-Girshin @ARC

  25. Flow control statements • Control structures can be divided into two classes: • loop statements • for • while • do-while • branching statements • if • switch 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.  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. 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. Rina Zviel-Girshin @ARC

  29. 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”); } } 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. Rina Zviel-Girshin @ARC

  31. 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!!!”); } } 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; } 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. 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. 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”); } 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. 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. 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. 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); } } Rina Zviel-Girshin @ARC

  40. The for Statement diagram

  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. 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); } 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. Rina Zviel-Girshin @ARC

  44. The do.. while statement diagram 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. Rina Zviel-Girshin @ARC

  46. 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); } } } 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. 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. Rina Zviel-Girshin @ARC

  49. 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!"); } } Style Example Rina Zviel-Girshin @ARC

  50. Any Questions? Rina Zviel-Girshin @ARC

More Related