◆ Statement Types
- Simple statement
balance = balance - amount;
- Compound statement
if(condition) statement1;
- Block Statement
{
double newBalance = balance - amount;
balance = newBalance;
}
◆ The if/else statement
* if( condition ~ true / false)
statement1;
◆ Comparing Values : Relational Operators
- Only for comparing NUMBER
◆ Comparing Floating-point Numbers
- To avoid roundoff errors, don't use == to compare floating-point numbers
( ==는 정수부만 비교하기 때문에 잘못된 결과를 만들 수 있음)
- To compare floating-point numbers test whether they are close enough : |x-y| ≤ ε
final double EPSILON = 1E-14;
if(Math.abs(x-y) <= EPSILON) // x is approximately equal to y
◆ Comparing Strings
- Use equals method
if(string1.equals(string2))...
- Don't use == for strings!
변수명(문자열 변수명)을 비교함.
- == tests identity , equals tests eqaul contents
- Case insensitive test :
if(string1.equalsIgnoreCase(string2))...
◆ Comparing Objects
- == tests for identity, equals for identical contest
★ 이해한다면 좋은 예제
ex.
Rectangle box1 = new Rectangle(1,4,2,3);
Rectangle box2 = box1;
Rectangle box3 = new Rectangle(1,4,2,3);
box1 != box3 but box1.equals(box3)
box1 == box2
- object가 refer하는 것이 같으면 == (true), 그렇지 않으면 != (false)
- Caveat : equals must be defined for the class
◆ Testing for null
- null reference refers to no object
- use ==, not eqauls, to test for null
- null is not the same as empty string
◆ Multiple Alternative : Sequences of Comparisons
if(condition1)
statement1;
else if(condition2)
statement2;
...
else
statement#;
- don't omit else
◆ Using Boolean Expression
◇ The boolean Type
- George Boole(1815-1864) : pioneer in the study of logic
- value of expression amount<1000 is true or false
- boolean type : one of these 2 truth value
◇ Predicate Method
- returns a boolean value
- useful in Character class
◆ while Loop / for Loop
- Iterator를 사용한 For Loop ( Enhanced for loop )
class EnhancedForDemo { |
Count is: 1 |
numbers이라는 array의 처음부터 끝까지 탐색함
이때의 int item은 int형 array인 numbers의 element 하나씩을 받아들여 오는 것이다.
cf. String의 length() = string의 길이 (String의 length 는 존재하지 않는다.) Array나 Arraylist, Vector 등의 length = 칸의 길이 |
'Computers > Language java' 카테고리의 다른 글
week8. Graphic User Interface (0) | 2011.04.10 |
---|---|
week6. Arrays and Array List (0) | 2011.04.10 |
week4. Fundamental Data Types (0) | 2011.04.10 |
week3. Implementing Classes (Basic Java Concept part2) (0) | 2011.04.10 |
week2. Using Objects (Basic Java Concept part1) (0) | 2011.04.10 |