JAVA의 정석(ch2, 3)

Du-Hyeon, KimΒ·2023λ…„ 7μ›” 13일
0

JAVA

λͺ©λ‘ 보기
3/11
post-thumbnail

πŸ’» 2. Variables

  1. kinds of print
System.out.println()	// 1 line print
System.out.print()		// print in a row

velog ν‘œ μž‘μ„±λ²• link

  1. data types
  • string : λ¬Έμžμ—΄ / char : λ¬Έμžν•˜λ‚˜ μ•„μŠ€ν‚€ μ½”λ“œ
  • λͺ¨λ“  μ •μˆ˜ μ‹€μˆ˜λŠ” λ³΄μˆ˜ν‘œκΈ°λ²• 따름 (MAX +1 = MIN)
int 	 x = 100;
double pi = 3.14;
char 	 ch = 'a';
String str = "abc";
types1byte2byte4byte8byte
λ…Όλ¦¬ν˜•boolean:-:----
λ¬Έμžν˜•--char----
μ •μˆ˜ν˜•byteshortintlong
μ‹€μˆ˜ν˜•--:-:floatdouble
  1. μƒμˆ˜ ν‘œκΈ°λ²•
    Cμ–Έμ–΄μ—μ„œλŠ” constant둜 ν‘œκΈ°ν•˜μ§€λ§Œ,
    JAVAμ—μ„œλŠ” μ„ μ–Έ μ•žμ— final을 λΆ™μ—¬μ„œ μ‚¬μš©
final int ONE_HUNDRED =100;
//constantλŠ” 늘 그렇듯이 이후에 κ°’ λ³€κ²½ λΆˆκ°€
// + λŒ€λ¬Έμžλ‘œ μ“°λŠ”κ²Œ 암묡적 λ£°
  1. printμ‹œ νŠΉμ΄ν•œ 방식듀
System.out.printf("d=%14.10f%n", d);	
//총 14자리둜 ν‘œν˜„ + 10μžμ§€λŠ” μ†Œμˆ˜μ  μ•„λž˜ μžλ¦¬μž„
System.out.printf("[%s]%n", url);
// %n은 enter μ—­ν• 
System.out.printf("[%20s]%n", url);
//string을 20μžλ¦¬μ— μž‘μ„±(였λ₯Έμ •λ ¬)
System.out.printf("[%-20s]%n", url);
// - 뢙이면 μ™Όμͺ½ μ •λ ¬
System.out.printf("[%.8s]%n", url);
// .8은 string의 μ™Όμͺ½μ—μ„œ 8μžλ¦¬κΉŒμ§€λ§Œ ν‘œμ‹œ
  1. scan function
    • library μΆ”κ°€
    • scan class function도 닀양함
import java.util.Scanner;
	//scanner library import
class Ex2_10 { 
	public static void main(String[] args) { 
		Scanner scanner = new Scanner(System.in);
        	// scanner class object generation
		System.out.print("λ‘μžλ¦¬ μ •μˆ˜ μž…λ ₯ν•˜μ„Έμš”: ");
		String input = scanner.nextLine();
        	//nextint, nextfloast을 ν™œμš©ν•΄μ„œ μžλ£Œν˜• λ³€ν™˜μ„ 단좕가λŠ₯ν•˜λ‹€.
            //μ•„λ‹ˆλ©΄ Integar.parseInt(num)같이 μž‘μ„±ν•΄μ•Όλ¨
		int num = Integer.parseInt(input);
		System.out.println("μž…λ ₯κ°’ :"+input);
		System.out.printf("num=%d%n", num);
	} 
}

🐬 3. Operator

  1. μ‚°μˆ μ—°μ‚°μž / λŒ€μž…μ—°μ‚°μž 의 μžλ™ν˜•λ³€ν™˜
    • μ‚°μˆ μ—°μ‚¬μž
      μžλ™ν˜•λ³€ν™˜ 일어남 -> 더 λ°μ΄ν„°λ²”μœ„κ°€ 큰 μͺ½μœΌλ‘œ
    • λŒ€μž…μ—°μ‚°μž
      (πŸ‘Œν‘œν˜„κ°€λŠ₯ν•œ μˆ«μžλ³΄λ‹€ 클 λ•Œ,) μžλ™ν˜•λ³€ν™˜ X
      -> compile error λ°œμƒ(imcompariable ~)
    class Ex3_8 {
    public static void main(String[] args) {
    	byte a = 10;
    	byte b = 30;
    	byte c = (byte)(a * b);		//μˆ˜λ™ ν˜•λ³€ν™˜
    	System.out.println(c);
    	}
    }
    watch out!!!
    	int a = 1_000_000;
    	int b = 2_000_000;
    	long c = a * b;
        //μ΄λ•Œ 이미 연산은 μ˜€λ²„ν”Œλ‘œμš°λœ int둜 μ €μž₯됨(음수)
        //λ”°λΌμ„œ 값은 음수인데 long에 λŒ€μž…λ λ•Œλ§Œ longν˜•μ‹μœΌλ‘œ λŒ€μž…λ¨
    	System.out.println(c);
  1. JAVAμ—λŠ” String obejct comparator operatorκ°€ μžˆλ‹€!
String str2 = new String("abc");
str1.equals("abc")
str2.equals("abc")
str2.equalsIgnoreCase("ABC")
  1. λŒ€μž… μ—°μ‚°μž μž‘λ™ μˆœμ„œ
    x = y= 3 μˆœμ„œ
    • y=3 λŒ€μž…
    • x=y μ‹€ν–‰
  1. bit operator (볡합 λŒ€μž… μ—°μ‚°μž version)
int i =3;
i &= 7;		//AND gate
i |= 7;		//OR gate
i ^= 4;		//XOR gate
System.out.println(i);

exponential operates with shift operator(>> or <<)

  1. % opearator
    λ‚˜λ¨Έμ§€ μ—°μ‚¬μžλ‘œμ„œ floatν˜•μ—μ„œλ„ 적용 κ°€λŠ₯
double j = 6.43;
double k = 4.23;
System.out.println(j % k);
//μ‹€μˆ˜μ—λ„ μ μš©κ°€λŠ₯(2.199999993)

😊END

0개의 λŒ“κΈ€