CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05
Variables - Reminders Variables are Case-Sensitive It must be a legal identifier - May only begin with a letter, the underscore or a dollar sign. - Consist of letters, digits, underscores, and the dollar sign. - Must not start with a number!
Variables - Reminders It must not be one of Java’s reserved keywords. (See Appendix C for a complete list) It must not be a boolean literal – true or false It must not be the reserved word null It can be any length
Built-in Data Types char – used for storing single characters boolean – has two possible values, true or false Integer types: - byte – 8 bits - short – 16 bits - int – 32 bits - long – 64 bits
Data-Types continued Floating point types (for storing decimals) - float – 32 bits - double – 64 bits
Declaring Variables int x; //don’t forget the ; char grade = ‘A’; You can also declare more than one variable in a line. Ex… float x=3.4, y=9.7, z; When declaring on one line, separate by a comma.
Control Structures Provide a method by which you can test variables and conditions in your program Provide a method of flow control and execution if statements if/else statements while loops
Logical Operators Two types – Relational and Conditional Relational : Compares two values and determines the relationship between them Examples : > , < , >= , ==
Relationship operators Use Description > op1 > op2 Returns true if op1 is greater than op2 >= op1 >= op2 Returns true if op1 is greater than or equal to op2 < op1 < op2 Returns true if op1 is less than op2 <= op1 <= op2 Returns true if op1 is less than or equal to op2 == op1 == op2 Returns true if op1 and op2 are equal != op1 != op2 Returns true if op1 and op2 are not equal
Conditional Operators Used with relational operators Combines multiple relational operators with a condition such as (and) or (or)
Conditional Operators Use Description && Op1 && op2 Returns true if both op1 and op2 are true || Op1 || op2 Returns true if either op1 or op2 is true ! !op Returns true if op is false
If statements Execute if the statement returns true int x = 5; if (x == 5) { System.out.print(“x = 5”); } { } are optional if only 1 line under if
If/Else The else executes only when the if statement fails. if (x == 5) System.out.println(“x=5”); else System.out.println(“x != 5”);
Dangling-else if (x > 5) if (y > 5) System.out.println(“x and y are > 5”); else System.out.println(“x is <= 5”); What is the output if… x=10 and y=10? If x=10 and y=2?
Dangling Else p2 Compiler actually sees… if (x > 5) if (y > 5) System.out.println(“x and y are > 5”); else System.out.println(“x is <= 5”); To solve this problem, use { }
While Loops while executes until false. int product = 3; while (product <= 100 ) product = 3 * product; product is initially 3, then it goes to 9, 27, 81, 243. Once it gets to 243, it stops because 243 is not <= 100.
More while loops { } must be used after the while statement if there is more than 1 line. While loops are extremely useful when you want to obtain correct input from the user. Make user keep entering the information until it is correct.
Questions…. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/relational.html