3강의 예제를 모아둔 것이다.
일부는 다른 글에서 설명하면서 사용할 것이고 일부는 넘어갈 거라서 만약 코드 적기가 귀찮다면 복사 붙여넣기해서 사용하자.
ctrl + F 한 후 class이름을 검색하면 쉽게 찾을 수 있다.
public class OperatorEx01 {
public static void main(String[] args) {
int i = 5;
i++; // i=i+1;
System.out.println(i);
i = 5;
++i;
System.out.println(i);
}
}
6
6
public class OperatorEx02 {
public static void main(String[] args) {
int i = 5, j = 0;
j = i++;
System.out.println("j=i++; 실행 후, i=" + i + ", j=" + j);
i = 5;
j = 0;
j = ++i;
System.out.println("j=++i; 실행 후, i=" + i + ", j=" + j);
}
}
j=i++; 실행 후, i=6, j=5
j=++i; 실행 후, i=6, j=6
public class OperatorEx03 {
public static void main(String[] args) {
int i=5, j=5;
System.out.println(i++);
System.out.println(++j);
System.out.println("i = " + i + ", j = " + j);
int x = 5;
x = x++ - ++x;
System.out.println(x);
byte b = -128;
b = (byte)-b;
System.out.println(-b);
}
}
5
6
i = 6, j = 6
-2
128
public class OperatorEx04 {
public static void main(String[] args) {
int i = -10;
i = +i;
System.out.println(i);
i = -10;
i = -i;
System.out.println(i);
}
}
-10
10
public class OperatorEx05 {
public static void main(String[] args) {
int a = 10;
int b = 4;
System.out.printf("%d + %d = %d%n", a, b, a + b);
System.out.printf("%d - %d = %d%n", a, b, a - b);
System.out.printf("%d * %d = %d%n", a, b, a * b);
System.out.printf("%d / %d = %d%n", a, b, a / b);
System.out.printf("%d / %f = %f%n", a, (float) b, a / (float) b);
}
}
10 + 4 = 14
10 - 4 = 6
10 * 4 = 40
10 / 4 = 2
10 / 4.000000 = 2.500000
66.66666666666667
66.67
66.66
66.66200
public class Exercise3_06 {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte c = a + b;
System.out.println(c);
}
}
30
public class Exercise3_07 {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte c = (byte) (a + b);
System.out.println(c);
}
}
44
public class OperatorEx08 {
public static void main(String[] args) {
int a = 1_000_000; // 1,000,000 1백만
int b = 2_000_000; // 2,000,000 2백만
// int c = a * b; // 2 * 10^12
long c = a* b; // a* b = 2,000,000,000,000 ?
System.out.println(c);
}
}
-1454759936
public class OperatorEx09 {
public static void main(String[] args) {
long a = 1_000_000 * 1_000_000;
long b = 1_000_000 * 1_000_000L;
System.out.println("a="+a);
System.out.println("b="+b);
}
}
a=-727379968
b=1000000000000
public class OperatorEx10 {
public static void main(String[] args) {
int a = 1000_000;
int result1 = a * a / a;
int result2 = a / a * a;
System.out.printf("%d * %d / %d = %d%n", a, a, a, result1);
System.out.printf("%d / %d * %d = %d%n", a, a, a, result2);
}
}
1000000 * 1000000 / 1000000 = -727
1000000 / 1000000 * 1000000 = 1000000
public class OperatorEx11 {
public static void main(String[] args) {
char a = 'a';
char d = 'd';
char zero = '0';
char two = '2';
System.out.printf("'%c' - '%c' = %d%n", d, a, d - a);
System.out.printf("'%c' - '%c' = %d%n", two, zero, two - zero);
System.out.printf("'%c' = %d%n", a, (int)a);
System.out.printf("'%c' = %d%n", d, (int)d);
System.out.printf("'%c' = %d%n", zero, (int)zero);
System.out.printf("'%c' = %d%n", two, (int)two);
// char c1 = 'A';
// char c2 = c1 + 1; // 변수와 리터럴의 연산이라 안됨 final로 리터럴로바꾸면 ㄱ나ㅡㅇ
// 상수 포함
char c2 = 'a' + 1; // 리터럴 간의 연산
char c3 = 0xAC00;
System.out.println(c2);
System.out.println(c3);
int sec = 60 * 60 * 24; //
System.out.println(sec);
}
}
'd' - 'a' = 3
'2' - '0' = 2
'a' = 97
'd' = 100
'0' = 48
'2' = 50
b
가
86400
public class OperatorEx12 {
public static void main(String[] args) {
char c1 = 'a'; // c1에는 문자 'a'의 코드값인 97이 저장된다.
char c2 = c1; // c1에 저장되어 있는 값이 c2에 저장된다.
char c3 = ' '; // c3를 공백으로 초기화 한다.
int i = c1 + 1; // 'a'+1 > 97+1 > 98
c3 = (char)(c1 + 1); // c1 + 1의 형태가 int 이므로 char로 변환
c2++;
c2++;
System.out.println("i=" + i);
System.out.println("c2=" + c2);
System.out.println("c3=" + c3);
}
}
i=98
c2=c
c3=b
public class OperatorEx13 {
public static void main(String[] args) {
char c1 = 'a';
// char c2 = c1 + 1; // 컴파일 에러
char c2 = 'a' + 1; //리터럴간의 연산이므로 에러 X
System.out.println(c1);
System.out.println(c2);
}
}
a
b
public class OperatorEx14 {
public static void main(String[] args) {
char c = 'a';
for (int i = 0; i < 26; i++) {
// 블럭{} 안의 문장을 26번 반복한다.]
System.out.print(c++);
} // 'a;부터 26개의 문자를 출력한다.
System.out.println(); // 줄바꿈을한다
c = 'A';
for (int i = 0; i < 26; i++) { // 블럭 {} 안의 문장을 26번 반복한다.
System.out.print(c++); //'A'부터 26개의 문자를 출력한다.
}
System.out.println();
c = '0';
for (int i = 0; i < 10; i++) // 블럭{} 안의 문장을 10번 반복한다.
System.out.println(c++); // '0'부터 10개의 문자를 출력한다.
System.out.println();
// 줄바꿈을한다
}
}
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0
1
2
3
4
5
6
7
8
9
import java.util.Scanner;
public class OperatorEx15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char lowerCase = 'x';
char upperCase = (char)(lowerCase - 32);
System.out.println(upperCase);
}
}
X
public class OperatorEx16 {
public static void main(String[] args) {
float pi = 3.141592f;
float shortPi = (int)(pi * 1000) / 1000f;
System.out.println(shortPi);
}
}
3.141
public class OperatorEx17 {
public static void main(String[] args) {
double pi = 3.141592;
double shortPi = (int)(pi * 1000 + 0.5) / 1000.0;
System.out.println(shortPi);
}
}
3.142
public class OperatorEx18 {
public static void main(String[] args) {
double pi = 3.141592;
double shortPi = Math.round(pi * 1000) / 1000.0;
System.out.println(shortPi);
}
}
3.142
public class OperatorEx19 {
public static void main(String[] args) {
int x = 10;
int y = 8;
System.out.printf("%d을 %d로 나누면, %n", x, y);
System.out.printf("몫은 %d이고, 나머지는 %d입니다.%n", x / y, x % y);
}
}
10을 8로 나누면,
몫은 1이고, 나머지는 2입니다.
public class OperatorEx20 {
public static void main(String[] args) {
System.out.println(-10%8);
System.out.println(10%-8);
System.out.println(-10%-8);
}
}
-2
2
-2
public class OperatorEx21 {
public static void main(String[] args) {
System.out.printf("10 == 10.0f \t %b%n", 10==10.0f);
System.out.printf("'0' == 0 \t %b%n", '0' == 0);
System.out.printf("'A' == 65 \t %b%n", 'A' == 65);
System.out.printf("'A' > 'B' \t %b%n", 'A' > 'B');
System.out.printf("'A'+1 != 'B' \t %b%n", 'A'+1 != 'B');
}
}
10 == 10.0f true
'0' == 0 false
'A' == 65 true
'A' > 'B' false
'A'+1 != 'B' false
public class OperatorEX22 {
public static void main(String[] args) {
float f = 0.1f;
double d = 0.1;
double d2 = (double) f;
System.out.printf("10.0==10.0f %b%n ", 10.0 == 10.0f);
System.out.printf("0.1==0.1f %b%n ", 0.1 == 0.1f);
System.out.printf("f=%19.17f%n ", f);
System.out.printf("d =%19.17f%n ", d);
System.out.printf("d2=%19.17f%n ", d2);
System.out.printf("d==f %b%n ", d == f);
System.out.printf("d==d2 %b%n ", d == d2);
System.out.printf("d2==f %b%n ", d2 == f);
System.out.printf("(float)d==f %b%n ", (float) d == f);
}
}
0.1==0.1f false
f=0.10000000149011612
d =0.10000000000000000
d2=0.10000000149011612
d==f false
d==d2 false
d2==f true
(float)d==f true
public class OperatorEx23 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
System.out.printf("\"abc\"==\"abc\" ? %b%n", "abc"=="abc");
System.out.printf(" str1==\"abc\" ? %b%n", str1=="abc");
System.out.printf("str1.equals(\"abc\") ? %b%n", str1.equals("abc"));
System.out.printf("str2.equals(\"abc\") ? %b%n", str2.equals("abc"));
System.out.printf("str2.equalsIgnotreCase(\"abc\") ? %b%n", str2.equalsIgnoreCase("abc"));
}
}
"abc"=="abc" ? true
str1=="abc" ? true
str1=="abc" ? false
str1.equals("abc") ? true
str2.equals("abc") ? true
str2.equalsIgnotreCase("abc") ? true
public class OperatorEx24 {
public static void main(String[] args) {
int x = 0;
char ch = ' ';
x = 15;
System.out.printf("x=%2d, 10 < x && x < 20 = %b%n", x, 10 < x && x < 20);
x = 6;
System.out.printf("x=%2d , x%%2==0 || x%%3==0 && x%%6!=0 = %b%n",x, x%2==0 || x%3==0&&x%6!=0);
System.out.printf("x=%2d , (x%%2==0 || x%%3==0) && x%%6!=0 = %b%n",x, (x%2==0 || x%3==0)&&x%6!=0);
ch='1';
System.out.printf("ch='%c', '0' <= ch && ch <= '9' =%b%n", ch, '0' <= ch && ch <='9');
ch='a';
System.out.printf("ch='%c', 'a' <= ch && ch <= 'z' =%b%n", ch, 'a' <= ch && ch <='z');
ch='A';
System.out.printf("ch='%c', 'A' <= ch && ch <= 'Z' =%b%n", ch, 'A' <= ch && ch <='Z');
ch='q';
System.out.printf("ch='%c', 'q' <= ch && ch <= 'Q' =%b%n", ch, 'q' <= ch && ch <='Q');
}
}
x=15, 10 < x && x < 20 = true
x= 6 , x%2==0 || x%3==0 && x%6!=0 = true
x= 6 , (x%2==0 || x%3==0) && x%6!=0 = false
ch='1', '0' <= ch && ch <= '9' =true
ch='a', 'a' <= ch && ch <= 'z' =true
ch='A', 'A' <= ch && ch <= 'Z' =true
ch='q', 'q' <= ch && ch <= 'Q' =false
import java.util.Scanner;
public class OperatorEx25 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char ch = ' ';
for (int i = 1; i <= 10; i--) {
System.out.printf("문자를 하나 입력하세요.>");
String input = scanner.nextLine();
ch = input.charAt(0);
if ('0' <= ch && ch <= '9') {
System.out.printf("입력하신 문자는 숫자입니다.%n");
}
if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'z')) {
System.out.printf("입력하신 문자는 영문자입니다.%n");
}
} // main
scanner.close();}
문자를 하나 입력하세요.>a
입력하신 문자는 영문자입니다.
문자를 하나 입력하세요.>1
입력하신 문자는 숫자입니다.
문자를 하나 입력하세요.>
public class OperatorEx26 {
public static void main(String[] args) {
int a = 5;
int b = 0;
System.out.printf("a=%d, b=%d%n", a, b);
System.out.printf("a!=0 || ++b!=0 = %b%n", a != 0 || ++b != 0);
System.out.printf("a=%d, b=%d%n", a, b);
System.out.printf("a==0 && ++b!=0 = %b%n", a == 0 && ++b != 0);
System.out.printf("a=%d, b=%d%n", a, b);
}
}
a=5, b=0
a!=0 || ++b!=0 = true
a=5, b=0
a==0 && ++b!=0 = false
a=5, b=0
public class OperatorEx27 {
public static void main(String[] args) {
boolean b = true;
char ch = 'c';
System.out.printf("b=%b%n", b);
System.out.printf("!b=%b%n", !b);
System.out.printf("!!b=%b%n", !!b);
System.out.printf("!!!b=%b%n", !!!b);
System.out.println();
System.out.printf("ch=%c%n", ch);
System.out.printf("ch < 'a' || ch > 'z'=%b%n", ch < 'a' || ch > 'z');
System.out.printf("!('a'<=ch && ch<='z'=%b%n", !('a' <= ch && ch <= 'z'));
System.out.printf("'a'<=ch && ch<='z' =%b%n", 'a' <= ch && ch <= 'z');
} // main의 끝
}
b=true
!b=false
!!b=true
!!!b=false
ch=c
ch < 'a' || ch > 'z'=false
!('a'<=ch && ch<='z'=false
'a'<=ch && ch<='z' =true
4
7
3
-2
24
1
11111111111111111111111111111010
11111111111111111111111111111101
1111111111111111111111111111101
2147483645
5
5
public class OperatorEx28 {
public static void main(String[] args) {
int x = 0xAB, y = 0xF;
System.out.printf("x = %#X \t\t%s%n", x, toBinaryString(x));
System.out.printf("y = %#X \t\t%s%n", y, toBinaryString(y));
System.out.printf("%#X | %#X = %#X \t%s%n", x, y, x | y, toBinaryString(x | y));
System.out.printf("%#X & %#X = %#X \t%s%n", x, y, x & y, toBinaryString(x & y));
System.out.printf("%#X ^ %#X = %#X \t%s%n", x, y, x ^ y, toBinaryString(x ^ y));
System.out.printf("%#X ^ %#X ^ %#X = %#X \t%s%n", x, y, y, x ^ y, toBinaryString(x ^ y ^ y));
} // main의 끝
static String toBinaryString(int x) { // 10진 정수를 2진수로 변환하는 메서드
String zero = "00000000000000000000000000000000";
String tmp = zero + Integer.toBinaryString(x);
return tmp.substring(tmp.length() - 32);
}
}
x = 0XAB 00000000000000000000000010101011
y = 0XF 00000000000000000000000000001111
0XAB | 0XF = 0XAF 00000000000000000000000010101111
0XAB & 0XF = 0XB 00000000000000000000000000001011
0XAB ^ 0XF = 0XA4 00000000000000000000000010100100
0XAB ^ 0XF ^ 0XF = 0XA4 00000000000000000000000010101011
public class OperatorEx29 {
public static void main(String[] args) {
byte p = 10;
byte n = -10;
System.out.printf(" p =%d |t%s%n", p, toBinaryString(p));
System.out.printf("~p =%d |t%s%n", ~p, toBinaryString(~p));
System.out.printf("~p+1 =%d |t%s%n", ~p+1, toBinaryString(~p+1));
System.out.printf("~~p =%d |t%s%n", ~~p, toBinaryString(~~p));
System.out.println();
System.out.printf(" n =%d%n", n);
System.out.printf("~(n-1)=%d%n", ~(n-1));
} // main의 끝
static String toBinaryString(int x) { // 10진 정수를 2진수로 변환하는 메서드
String zero = "00000000000000000000000000000000";
String tmp = zero + Integer.toBinaryString(x);
return tmp.substring(tmp.length() - 32);
}
}
p =10 |t00000000000000000000000000001010
~p =-11 |t11111111111111111111111111110101
~p+1 =-10 |t11111111111111111111111111110110
~~p =10 |t00000000000000000000000000001010
n =-10
~(n-1)=10
public class OperatorEx30 {
// 10진 정수를 2진수로 변환하는 메서드
static String toBinaryString(int x) {
String zero = "00000000000000000000000000000000";
String tmp = zero + Integer.toBinaryString(x);
return tmp.substring(tmp.length() - 32);
}
public static void main(String[] args) {
int dec = 8;
System.out.printf("%d >> %d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
System.out.printf("%d >> %d = %4d |t%s%n", dec, 1, dec >> 1, toBinaryString(dec >> 1));
System.out.printf("%d >> %d = %4d |t%s%n", dec, 2, dec >> 2, toBinaryString(dec >> 2));
System.out.printf("%d << %d = %4d |t%s%n", dec, 0, dec << 0, toBinaryString(dec << 0));
System.out.printf("%d << %d = %4d |t%s%n", dec, 1, dec << 1, toBinaryString(dec << 1));
System.out.printf("%d << %d = %4d |t%s%n", dec, 2, dec << 2, toBinaryString(dec << 2));
System.out.println();
dec = -8;
System.out.printf("%d >> %d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
System.out.printf("%d >> %d = %4d |t%s%n", dec, 1, dec >> 1, toBinaryString(dec >> 1));
System.out.printf("%d >> %d = %4d |t%s%n", dec, 2, dec >> 2, toBinaryString(dec >> 2));
System.out.printf("%d << %d = %4d |t%s%n", dec, 0, dec << 0, toBinaryString(dec << 0));
System.out.printf("%d << %d = %4d |t%s%n", dec, 1, dec << 1, toBinaryString(dec << 1));
System.out.printf("%d << %d = %4d |t%s%n", dec, 2, dec << 2, toBinaryString(dec << 2));
System.out.println();
dec = 8;
System.out.printf("%d >> %2d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
System.out.printf("%d >> %2d = %4d |t%s%n", dec, 32, dec >> 32, toBinaryString(dec >> 32));
} // main의 끝
}
8 >> 0 = 8 |t00000000000000000000000000001000
8 >> 1 = 4 |t00000000000000000000000000000100
8 >> 2 = 2 |t00000000000000000000000000000010
8 << 0 = 8 |t00000000000000000000000000001000
8 << 1 = 16 |t00000000000000000000000000010000
8 << 2 = 32 |t00000000000000000000000000100000
-8 >> 0 = -8 |t11111111111111111111111111111000
-8 >> 1 = -4 |t11111111111111111111111111111100
-8 >> 2 = -2 |t11111111111111111111111111111110
-8 << 0 = -8 |t11111111111111111111111111111000
-8 << 1 = -16 |t11111111111111111111111111110000
-8 << 2 = -32 |t11111111111111111111111111100000
8 >> 0 = 8 |t00000000000000000000000000001000
8 >> 32 = 8 |t00000000000000000000000000001000
public class OperatorEx31 {
public static void main(String[] args) {
int dec =1234;
int hex = 0xABCD;
int mask = 0xF;
System.out.printf("hex=%X%n", hex);
System.out.printf("%X%n", hex & mask);
hex = hex >> 4;
System.out.printf("%X%n", hex & mask);
hex = hex >> 4;
System.out.printf("%X%n", hex & mask);
hex = hex >> 4;
System.out.printf("%X%n", hex & mask);
}
}
hex=ABCD
D
C
B
A
public class OperatorEx32 {
public static void main(String[] args) {
int x, y, z;
int absX, absY, absZ;
char signX, signY, signZ;
x = 10;
y = -5;
z = 0;
absX = x >= 0 ? x : -x; // x의 값이 음수이면, 양수로 만든다.
absY = y >= 0 ? y : -y;
absZ = z >= 0 ? z : -z;
signX = x > 0 ? '+' : (x==0 ? ' ' : '-'); //조건 연산자를 중첩
signY = y > 0 ? '+' : (y==0 ? ' ' : '-');
signZ = z > 0 ? '+' : (z==0 ? ' ' : '-');
System.out.printf("x=%c%d%n", signX, absX);
System.out.printf("y=%c%d%n", signY, absY);
System.out.printf("z=%c%d%n", signZ, absZ);
}
}
x=+10
y=-5
z= 0