728x90
연산자
-산술연산자
- 실수하기 쉬운 문제들..
5 / 2 = 2 , 5/2. = 2.5 , 3%7 = 3
int =5;
int b = 3;
int c = ++a + b++; // c의 값은 9이다. 왜냐면 b는 계산이 다 끝나고 1증가하기때문에..
++a는 전위연산 b++는 후위연산 //후위연산은 한 라인 계산이 다 끝나고 증가한다.
int a = 5;
int b = ++a + ++a;
=>b의 값은 13이다.. // C언어 에서는 14 가 나온다. (레지스터 하나 사용) 변수a의 기억장소가 같다.
-관계연산자
a > b // a를 기준으로 한다.
> < >= <= == !=
초과 미만 이상 이하 같다 같지않다 // 실수조심 =<, => 이런 표현은 오류..
-논리연산자
비트논리연산자 -> & (and) , | (or) , ^ (xor) , ~(not) // xor - 1의 개수가 짝수개면 0 홀수개면 1 이다.
일반논리연산자 -> && (and) , || (or) , ! (not)
관계연산자와 논리연산자를 같이 사용하면 관계연산자가 먼저 연산된다.
=> 5>3 && 10>7 true
switch ~ case문
----------------------------------------------------------------------------
class Switch_1
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
switch(num%2) //변수나 수식만 사용
{
case 1: //정수값만 사용해야됨 If문처럼 논리식 쓰면 안됨.
System.out.println(num + "는 홀수입니다.");
break;
case 0:
System.out.println(num + "는 짝수입니다.");
break;
}
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
/*
제어문 예제 switch ~case
월(달)을 입력받아 그달의 날짜 출력하기
*/
import java.io.*;
-산술연산자
- 실수하기 쉬운 문제들..
5 / 2 = 2 , 5/2. = 2.5 , 3%7 = 3
int =5;
int b = 3;
int c = ++a + b++; // c의 값은 9이다. 왜냐면 b는 계산이 다 끝나고 1증가하기때문에..
++a는 전위연산 b++는 후위연산 //후위연산은 한 라인 계산이 다 끝나고 증가한다.
int a = 5;
int b = ++a + ++a;
=>b의 값은 13이다.. // C언어 에서는 14 가 나온다. (레지스터 하나 사용) 변수a의 기억장소가 같다.
-관계연산자
a > b // a를 기준으로 한다.
> < >= <= == !=
초과 미만 이상 이하 같다 같지않다 // 실수조심 =<, => 이런 표현은 오류..
-논리연산자
비트논리연산자 -> & (and) , | (or) , ^ (xor) , ~(not) // xor - 1의 개수가 짝수개면 0 홀수개면 1 이다.
일반논리연산자 -> && (and) , || (or) , ! (not)
관계연산자와 논리연산자를 같이 사용하면 관계연산자가 먼저 연산된다.
=> 5>3 && 10>7 true
-SHIFT연산자
<< : left shift (한 비트씩 좌로 이동, 우측은 0으로 채움 ---> X*2^n
>> : right shift (한 비트씩 우로 이동, 좌측은 sing bit로 채움 // sing bit 양수는 0 , 음수는 1로 채움 .---> X/2^n
>>>: 한 비트씩 우로 이동 , 좌측은 무조건 0으로 채움 ---> X/4^n
-대입연산자
연산자 표현 의미
+= a+=2 a=a+2
-= a-=2 a=a-2
*= a*=2 a=a*2
/= a/=2 a=a/2
%= a%=2 a=a%2
&= a&=2 a=a&2 - 대입연산 가능
<<= a<<=2 a=a<<2 - 시프트연산 가능
-형변환연산자
---------------------------------------------------------------------------
int a=5, b=3;
System.out.println("a/j=>"+(a/b));
System.out.println("(float)a/b)" +(float)a/b); //a만 float형으로 형변환
System.out.println("(float)(a/b)" +(float)(a/b)); // a/b연산 결과를 float으로 형변환
System.out.println((int)3.789);
System.out.println((char)65.7);
---------------------------------------------------------------------------
결과값
(float)a/b)1.6666666
(float)(a/b)1.0
<< : left shift (한 비트씩 좌로 이동, 우측은 0으로 채움 ---> X*2^n
>> : right shift (한 비트씩 우로 이동, 좌측은 sing bit로 채움 // sing bit 양수는 0 , 음수는 1로 채움 .---> X/2^n
>>>: 한 비트씩 우로 이동 , 좌측은 무조건 0으로 채움 ---> X/4^n
-대입연산자
연산자 표현 의미
+= a+=2 a=a+2
-= a-=2 a=a-2
*= a*=2 a=a*2
/= a/=2 a=a/2
%= a%=2 a=a%2
&= a&=2 a=a&2 - 대입연산 가능
<<= a<<=2 a=a<<2 - 시프트연산 가능
-형변환연산자
---------------------------------------------------------------------------
int a=5, b=3;
System.out.println("a/j=>"+(a/b));
System.out.println("(float)a/b)" +(float)a/b); //a만 float형으로 형변환
System.out.println("(float)(a/b)" +(float)(a/b)); // a/b연산 결과를 float으로 형변환
System.out.println((int)3.789);
System.out.println((char)65.7);
---------------------------------------------------------------------------
결과값
(float)a/b)1.6666666
(float)(a/b)1.0
3
A
-조건연산자
조건 ? A : B // 조건이 참이면 A를 실행 , 조건이 거짓이면 B를 실행
---------------------------------------------------------------------------
int a=5, b=3;
System.out.println( 5>3 ? 100 : 50);
System.out.println(10<7 ? 70 : 80);
---------------------------------------------------------------------------
실행결과 100
80
비교
== : 기본 데이터형 비교에 사용한다.
equals() : 문자열 비교에 사용한다.
instanceOf : 객체를 비교에 사용한다.
-문자열 비교
String str = "hello";
System.out.println(str.equals("hello"); // true
System.out.println(str.equals("Hello"); // false
System.out.println(str.equalsIgnoreCase("Hello"); // true equalsIgnoreCase은 대소문자 구별안함..
-객체 비교
객체 instanceof 클래스 오른쪽이 상위이거나 같을때 참
str < Object -> true
str = String -> true
obj > String ->false (객체 obj가 클래스 String보다 상위라서 false)
----------------------------------------------------------------------------
String str = new String("MBC");
Object obj = new Object();
System.out.println(str instanceof Object);
System.out.println(str instanceof String);
System.out.println(obj instanceof String);
A
-조건연산자
조건 ? A : B // 조건이 참이면 A를 실행 , 조건이 거짓이면 B를 실행
---------------------------------------------------------------------------
int a=5, b=3;
System.out.println( 5>3 ? 100 : 50);
System.out.println(10<7 ? 70 : 80);
---------------------------------------------------------------------------
실행결과 100
80
비교
== : 기본 데이터형 비교에 사용한다.
equals() : 문자열 비교에 사용한다.
instanceOf : 객체를 비교에 사용한다.
-문자열 비교
String str = "hello";
System.out.println(str.equals("hello"); // true
System.out.println(str.equals("Hello"); // false
System.out.println(str.equalsIgnoreCase("Hello"); // true equalsIgnoreCase은 대소문자 구별안함..
-객체 비교
객체 instanceof 클래스 오른쪽이 상위이거나 같을때 참
str < Object -> true
str = String -> true
obj > String ->false (객체 obj가 클래스 String보다 상위라서 false)
----------------------------------------------------------------------------
String str = new String("MBC");
Object obj = new Object();
System.out.println(str instanceof Object);
System.out.println(str instanceof String);
System.out.println(obj instanceof String);
----------------------------------------------------------------------------
실행결과 true
true
false
제어문
if문
----------------------------------------------------------------------------
class IfTest_2
{
public static void main(String args[])
{
int a =Integer.parseInt(args[0]);
if(a%2 == 0)
{
System.out.println(a + "는 짝수입니다");
}else
{
System.out.println(a + "는 홀수입니다.");
}
}
실행결과 true
true
false
제어문
if문
----------------------------------------------------------------------------
class IfTest_2
{
public static void main(String args[])
{
int a =Integer.parseInt(args[0]);
if(a%2 == 0)
{
System.out.println(a + "는 짝수입니다");
}else
{
System.out.println(a + "는 홀수입니다.");
}
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
switch ~ case문
----------------------------------------------------------------------------
class Switch_1
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
switch(num%2) //변수나 수식만 사용
{
case 1: //정수값만 사용해야됨 If문처럼 논리식 쓰면 안됨.
System.out.println(num + "는 홀수입니다.");
break;
case 0:
System.out.println(num + "는 짝수입니다.");
break;
}
}
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
/*
제어문 예제 switch ~case
월(달)을 입력받아 그달의 날짜 출력하기
*/
import java.io.*;
class Switch_5
{
public static void main( String [] args ) throws IOException
{
String monthStr;
int month;
int days=0; // days를 초기화를 안해주면 값이 아에 안들어갈 수도 있기 때문에(switch문안에 값을받음)컴파일 에러
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("월(달) 을 입력하세여 :");
monthStr = br.readLine();
month = Integer.parseInt(monthStr);
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days=31;
break;
case 2:
days=28;
case 4: case 6: case 9: case 11:
days=30;
break;
}
System.out.println(month +"월(달)은 " + days + "까지 있습니다.");
}// end main
{
public static void main( String [] args ) throws IOException
{
String monthStr;
int month;
int days=0; // days를 초기화를 안해주면 값이 아에 안들어갈 수도 있기 때문에(switch문안에 값을받음)컴파일 에러
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("월(달) 을 입력하세여 :");
monthStr = br.readLine();
month = Integer.parseInt(monthStr);
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days=31;
break;
case 2:
days=28;
case 4: case 6: case 9: case 11:
days=30;
break;
}
System.out.println(month +"월(달)은 " + days + "까지 있습니다.");
}// end main
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------