1)산술 연산자
| + | 더하기 |
| - | 빼기 |
| * | 곱하기 |
| / | 나누기 |
| % | 나머지 |
package org.opentutorials.javatutorials.operator;
public class ArithmeticDemo {
public static void main(String[] args) {
int result = 1 +2;
System.out.println(result); //3
result = result -1;
System.out.println(result); // 2
result = result +2;
System.out.println(result); // 4
result = result / 2;
System.out.println(result); // 2
result = result + 8;
System.out.println(result); // 10
result = result % 7;
System.out.println(result); // 3
}
}
package org.opentutorials.javatutorials.operator;
public class RemainerDemo {
public static void main(String[] args) {
int a = 3;
System.out.println(0%a); // 0
System.out.println(1%a); // 1
System.out.println(2%a); // 2
System.out.println(3%a); // 0
System.out.println(4%a); // 1
System.out.println(5%a); // 2
}
}
package org.opentutorials.javatutorials.operator;
public class ConcatDemo {
public static void main(String[] args) {
String firstString = "This is";
String secondString = " a concatenated string";
String thridString = firstString + secondString;
System.out.println(thridString); // This is a concatenated string
}
}
2)형변환
package org.opentutorials.javatutorials.operator;
public class DivisionDemo {
public static void main(String[] args) {
int a = 10;
int b = 3;
float c = 10.0F;
float d = 3.0F;
System.out.println(a/b); // 3
System.out.println(c/d); // 3.3333333
System.out.println(a/d); // 3.3333333
}
}
3)단항 연산자
package org.opentutorials.javatutorials.operator;
public class PrePostDemo {
public static void main(String[] args) {
int i = 3;
i++;
System.out.println(i); // 4
++i;
System.out.println(i); // 5
System.out.println(++i); // 6
System.out.println(i++); // 7
System.out.println(i); // 7
}
}
++i는 i의 값에 1이 더해진 값을 출력하는 것이고
i++는 이것이 속해있는 println에 일단 현재 i의 값을 출력하고
println의 실행이 끝난 후에 i의 값이 증가하는 특성이 있다.
출처 - 생활코딩
'자바' 카테고리의 다른 글
| 9) switch (0) | 2021.10.18 |
|---|---|
| 8) 비교와 Boolean (0) | 2021.10.18 |
| 6) 형변환 (0) | 2021.10.18 |
| 5) 데이터 타입 (0) | 2021.10.18 |
| 4) 주석과 세미콜론 (0) | 2021.10.18 |