Bit Operator
package sample07_BiOperator;
public class MainClass {
public static void main(String[] args) {
int number = 0x71 & 0x8a;
System.out.println("number = " + number);
int number1 = 0x71 | 0x8a;
System.out.println("number1 = " + number1);
System.out.printf("number1 16진수 = 0x%x \n", number1);
int number2 = 0x71 ^ 0x85;
System.out.println("number2 = " + number2);
System.out.printf("number2 16진수 = 0x%x \n", number2);
int number3 = 0xf4 ^ 0x85;
System.out.println("number3 = " +number3);
System.out.printf("number3 16진수 = 0x%x \n", number3);
short sh;
sh = 0x1 << 3;
System.out.println(sh);
sh = 0x8 >> 1;
System.out.println(sh);
byte by = ~0x55;
System.out.printf("%x \n", by);
}
}
Wrapper
package sample08_wrapper;
public class MainClass {
public static void main(String[] args) {
char c = 'a';
char cArr[] = { 'h', 'e', 'l', 'l', 'o'};
System.out.println(cArr);
int i = 123;
Integer iobj = 123;
Integer iobj2 = new Integer(123);
System.out.println("i = " + i);
System.out.println("iobj = " + iobj);
System.out.println("iobj2 = " + iobj2);
double d = 123.456;
Double dobj = 123.456;
System.out.println("d = " + d);
System.out.println("dobj = " + dobj);
String str = "hello world";
str = "I can do it";
System.out.println("str = " + str);
String numStr = "123";
int number = Integer.parseInt(numStr);
System.out.println("number = " + number);
Integer oNumber = 12345;
String str1 = oNumber + "";
System.out.println("str1 = " + str1);
int number1 = 67;
String str2 = Integer.toBinaryString(number1);
System.out.println("str2 = " + str2);
String str3 = Integer.toHexString(number1);
System.out.println("str3 = " + str3);
int num2 = Integer.parseInt(str2, 2);
System.out.println("num2 = " + num2);
String str4 = null;
System.out.println(str4);
String str5 = new String("hello");
String str6 = "world";
String str7 = str5 + str6;
System.out.println(str7);
String str8 = str5.concat(str6);
System.out.println(str8);
String str9 = "world";
String str10 = "world";
boolean b = str9.equals(str10);
System.out.println(b);
String str11 = "abcabcabc";
int n = str11.indexOf("c");
System.out.println("n = " + n);
int n1 = str11.lastIndexOf("c");
System.out.println("n1 = " + n1);
int len = str11.length();
System.out.println("len = " + len);
String str12 = "A*B*C*D";
String replaceStr = str12.replace("*", "-");
System.out.println(replaceStr);
String replaceStr1 = str12.replace("*", "");
System.out.println(replaceStr1);
String str12_1 = "새벽 눈에 종종걸음 출근길";
String replaceStr_1 = str12_1.replace(" ", "");
System.out.println(replaceStr_1);
String str13 = "홍길동-24-2001/10/16-서울시";
String subStr = str13.substring(4, 6);
System.out.println(subStr);
String subStr1 = str13.substring(7);
System.out.println(subStr1);
String split[] = str13.split("-");
System.out.println(split.length);
System.out.println(split[0]);
System.out.println(split[1]);
System.out.println(split[2]);
System.out.println(split[3]);
String str14 = " java java java ";
String trimStr = str14.trim();
System.out.println(trimStr);
String str15 = "가나다라마";
char c1 = str15.charAt(2);
System.out.println("c1 = " + c1);
String str16 = "서울시 강남구 역삼동";
boolean b1 = str16.contains("강남");
System.out.println(b1);
}
}
IF
package sample09_if;
public class MainClass {
public static void main(String[] args)
{
int number = 7;
if(number > 0) {
System.out.println("number은 0보다 큽니다.");
}
if(number == 7) {
System.out.println("number은 7 입니다.");
}
if(number < 6) {
System.out.println("number은 6보다 작습니다.");
}
if (number > 0 && number <= 10) {
System.out.println("number은 1~10 사이 어딘가~");
}
if (number > 0 || number <= 5){
System.out.println("뀨?");
}
int count = 8;
if (count < 6) {
System.out.println("number은 6보다 작습니다.");
}
else {
System.out.println("number은 6보다 크거나 같습니다.");
}
char c = 'A';
if (c == 'a') {
System.out.println("Nope!");
}
else {
System.out.println("c = 'a'가 아닙니다.");
}
String str = (c == 'a' ) ? "c= 'a'입니다":"c = 'a'가 아닙니다";
System.out.println(str);
count = 85;
if (count >= 90) {
System.out.println("A 학점입니다.");
}
else if (count >= 80) {
System.out.println("B 학점입니다.");
}
else if (count >= 70) {
System.out.println("C 학점입니다.");
}
else {
System.out.println("재시험 대상자입니다.");
}
boolean b;
b = true;
if (b == true) {
System.out.println("b == true 입니다.");
}
if (b) {
System.out.println("b == true 입니다.");
}
b = false;
if (b == false) {
System.out.println("b == false");
}
if (!b) {
System.out.println("b == false");
}
String str1 = "worl";
str1 = str1 + "d";
System.out.println("str1 = "+ str1);
if (str1 == "world") {
System.out.println("str1은 world 입니다.");
}
if (str1.equals("world") == true) {
System.out.println("equals str1은 world 입니다.");
}
count = 95;
if (count >= 90) {
if (count >=95) {
System.out.println("A+");
}
else {
System.out.println("A");
}
}
if(count >= 90 && count <= 100) {
System.out.println("A+");
}
else if (count >= 90 && count <95) {
System.out.println("A");
}
}
}
Switch
package sample10_switch;
public class MainClass {
public static void main(String[] args) {
int choice;
choice = 2;
switch (choice) {
case 1:
System.out.println("처리1");
break;
case 2:
System.out.println("처리2");
break;
case 3:
System.out.println("처리3");
break;
default:
System.out.println("1~3이 아닙니다.");
break;
}
}
}
for
package sample11_for;
import java.util.Iterator;
public class MainClass {
public static void main(String[] args) {
int i;
for (i=0 ; i<5 ; i++) {
System.out.println("for loop i = " + i);
}
System.out.println("i = " + i);
for (i = 0; i < 10; i = i + 2) {
System.out.println("for loop"+ i);
}
for ( i = 10; i > 0; i--) {
System.out.println("for loop "+ i);
}
for(int j=0 ; j<10; j++) {
System.out.println("for loop " + j);
}
for(int n = 0; n < 5; n++) {
System.out.println("for n = " + n);
for(int k=0; k<3; k++) {
System.out.println("\tfor k = "+ k);
}
}
int array[] = { 2, 4, 5, 6, 8 };
for(i = 0; i < 5; i++) {
System.out.println(array[i]);
}
for( i = 0; i < array.length; i++) {
if(array[i] == 6) {
System.out.println("6 찾았습니다.");
}
else {
System.out.println(array[i]);
}
}
for(i = 0; i < array.length; i++) {
if(array[i] % 2 ==0) {
System.out.println("홀수는 " + array[i] + "입니다.");
}
}
}
}
While
package sample12_while;
public class MainClass {
public static void main(String[] args) {
int w;
w = 0;
while(w < 5) {
System.out.println("while loop" + w);
w++;
}
for(int i = 0; i < 5; i++) {
System.out.println("i = " + i);
for(int j =0; j < 3; j++) {
System.out.println("\tj = "+ j);
}
}
int w1 , w2;
w1=0;
while( w1 < 5) {
System.out.println("w1 = " + w1);
w2=0;
while(w2 < 3) {
System.out.println("\tw2 = " + w2);
w2++;
}
w1++;
}
int w3;
w3 = 0;
do {
System.out.println("do while " + w3);
w3++;
}while(w3 < 5);
int array[] = { 2, 4, 5, 6, 7, 8}, i;
i=0;
while(i<array.length) {
System.out.println(" array = " + array[i]);
i++;
}
i=0;
while(i<array.length) {
if(array[i] % 2 == 1) {
System.out.printf("array[%x]의 값은 %x이므로 홀수입니다.\n", i, array[i]);
}
else if(array[i] % 2 == 0){
System.out.printf("array[%x]의 값은 %x이므로 짝수입니다.\n", i, array[i]);
}
i++;
}
}
}