Presentation is loading. Please wait.

Presentation is loading. Please wait.

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study.

Similar presentations


Presentation on theme: "S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study."— Presentation transcript:

1 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators junyoung@sparcs.kaist.ac.kr SPARCS JAVA Study

2 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Simpler print statements import java.lang.*; … int a=Math.abs(-123); import static java.lang.Math.*; … int a=abs(-123); import static net.mindview.util.Print.*; … print(“Hello, it’s : “);

3 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Precedence ( ) → * and / → + and – System.out.println(“a = ” + a + “ b = ” + b); + : String concatenation (convert non-String into String) a, b : int → String String 과 + 로 이으면 ! String 으로 type casting!! pubic class Precedence { public static void main (String[] args) { int x = 1, y = 2, z = 3; int a = x + y – 2/2 + z; int b = x + (y-2)/(2+z); System.out.println(“a = “ + a + “ b = “ + b); }

4 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Assignment(1) “Copy the right-value into the left-value” left-value must be a physical space to store the value a = 4; ( o ) // 4 = a; ( x ) a = b;  a, b : primitive – 간단히 b 의 값을 a 로 복사  a, b : reference variable –Reference 만 복사 – 즉, b 가 참조하는 object 를 a 도 참조하도록 한다. –a 와 b 가 참조하는 object 는 같게 된다.

5 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Assignment(2) Number n1=new Number(); Number n2=new Number(); n1.i=9; n2.i=47; n1=n2; n1.i=27; // 결과 : n1.i 와 n2.i 는 모두 27! i=47 => 27 n2 n1 i=9

6 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Aliasing during method calls public class PassObject { static void f(Letter y){ y.c = ‘z’; } public static void main(String[] args){ Letter x = new Letter(); x.c = ‘a’; System.out.println(x.c); f(x); System.out.println(x.c); } → ‘a’ ‘z’

7 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Mathematical operators +, -, *, /, % (modulus) shorthand notation x += 4; x = x + 4; Auto increment and decrement x++; ++x; x--; --x; i = 1; System.out.println(++i); Result) 2 i = 1; System.out.println(i++); System.out.println(i); Result) 1 2

8 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems ** random() Random 한 수 출력.. Random rand = new Random(47); seed 를 47 로 시작.. Random rand = new Random(); seed 를 현재 밀리초 시간으로 정함 ( System.currentTimeMillis(); ) Java.util.Random 내부 method 들 중 int nextInt( int n ) : n 을 bound 로 하는 값을 return; 참고 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html import java.util.*; public class MathOps { public static void main(String[] args){ Random rand = new Random(47); System.out.println(rand.nextInt(100)); }

9 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Relational operators ==, != : 객체의 reference 비교 equals() public static void main(){ Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1==n2); System.out.println(n1!=n2); System.out.println(n1.equals(n2)); } → False True

10 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Relational operators But… 새로운 class 에 equals() 를 override 해야 함. class AA { int i; } … public static void main(String[] args) { AA a1=new AA(); AA a2=new AA(); a1.i = a2.i = 100; System.out.println(a1.equals(a2)); } → false

11 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Logical operators &&, ||, ! –results a boolean value ( true / false ) –must apply boolean values only. –Can’t use a non-boolean (as C or C++) –Short-circuiting  만약 test1(0) 이 false 이면 if 문을 그냥 끝내고 지나가게 됨. //! print( i || j ); print( i < 10 && j < 10 ); if( test1(0) && test2(2) && test3(2) ){ … }

12 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Literals Number Literals Exponential notation int i1 = 0x2f; //Hexadecimal(lowercase) int i2 = 0x2F; //Hexadecimal(uppercase) int i3 = 0177; //Octal(0 으로 시작 ) long n = 200L; float f = 1F; double d = 1D; float expFloat = 1.39e-43F; double expDouble = 47e47;

13 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Bitwise operators &, |, ^, ~ –& : Both input bits are 1 → 1 / otherwise 0 –| : either input bits is 1 → 1 / otherwise 0 –^ : Both input bits are 1 or 0 → 0 / otherwise 1 –~ : complement operator/ 1 → 0 / 0 → 1 &=, |=, ^= &|^~ 0000001 01011 1001110 11110

14 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Shift operators > (sign extension), >>> (0 extension) i 10111010001001000100001010010101 i << 5 01000100100010000101001010100000 i >> 5 11111101110100010010001000010100 i >>>5 00000101110100010010001000010100

15 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Ternary if-else operator Three operands if-else statement Ternary(int i) { return i < 10 ? i*100 : i*10; } standardIfElse(int i) { if(i < 10) return i * 100; else return i * 10; }

16 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems String operator + Concatenate strings –If an expression begins with a String, all following operands must be Strings ( automatically turn ) *int 형인 x, y, z 가 자동으로 string 으로 변환되어 출력된다. int x=0, y=1, z=2; String sString = “x, y, z “; System.out.println(sString + x + y + z); → x, y, z 012

17 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Casting operators cast ? “ casting into a mold ” When appropriate, Java will automatically change.. –If you assign an integer to a float, automatically int → float. Type casting –Make this conversion explicit / when it wouldn’t normally happen → widening conversion : cast 할 필요 없음. → narrowing conversion : 꼭 cast 해야 함. 어느 Primitive type 에서나 가능 !! ( boolean 제외 ) int i = 200; long lng = (long) i; lng = i; //”widening conversion” i = (int)lng;//”narrowing conversion”

18 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Truncation and rounding Narrowing conversion : float/double → int round() methods in java.lang.Math double above = 0.7; float fabove = 0.4; print((int)above); print((int)fabove); → 0 0 double above = 0.7; float fbelow = 0.4; print(Math.round(above)); print(Math.round(fbelow)); → 1 0

19 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Java has no “sizeof” 모든 data type 이 모든 기계에서 동일한 크기로 설계되었다. Primitive typesizeWrapper class boolean-Boolean char16 bitCharacter byte8 bitByte short16 bitShort int32 bitInteger long64 bitLong float32 bitFloat double64 bitDouble Void-

20 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Homework Exercise 3,5,8,9,11,14

21 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Reference Thinking in JAVA 4th Edition


Download ppt "S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study."

Similar presentations


Ads by Google