s ystem p rogrammers' a ssociation for r esearching c omputer s ystems 3. operators...
TRANSCRIPT
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
3. Operators
SPARCS JAVA Study
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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 : “);
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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); }
}
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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 는 같게 된다 .
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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 => 27n2
n1
i=9
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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’
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Mathematical operators
• +, -, *, /, %(modulus)
• shorthand notationx += 4; x = x + 4;
• Auto increment and decrementx++; ++x; x--; --x;
i = 1;System.out.println(++i);
• Result) 2
i = 1;System.out.println(i++);System.out.println(i);
• Result) 1 2
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
** 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
import java.util.*;public class MathOps { public static void main(String[] args){ Random rand = new Random(47); System.out.println(rand.nextInt(100)); }}
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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 True
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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) ){…
}
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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;
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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
• &=, |=, ^=
& | ^ ~
0 0 0 0 0 0 1
0 1 0 1 1
1 0 0 1 1 1 0
1 1 1 1 0
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Shift operators
• <<, >> (sign extension), >>> (0 extension)
i 10111010001001000100001010010101
i << 5 01000100100010000101001010100000
i >> 5 11111101110100010010001000010100
i >>>5 00000101110100010010001000010100
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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;}
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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”
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
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
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Java has no “sizeof”
• 모든 data type 이 모든 기계에서 동일한 크기로 설계되었다 .
Primitive type size Wrapper class
boolean - Boolean
char 16 bit Character
byte 8 bit Byte
short 16 bit Short
int 32 bit Integer
long 64 bit Long
float 32 bit Float
double 64 bit Double
Void - Void
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Homework
• Exercise 3,5,8,9,11,14
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Reference
• Thinking in JAVA 4th Edition