본문 바로가기

JAVA

C++ 프로그래머 Java 맛보기 #10

다음은 연산자 이다. 그닥 볼것도 없으므로 소스로만 보자면

+ additive operator (also used for String concatenation)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator

class ArithmeticDemo {
     public static void main (String[] args){
        
         int result = 1 + 2; // result is now 3
         System.out.println(result);
         result = result - 1; // result is now 2
         System.out.println(result);
         result = result * 2; // result is now 4
         System.out.println(result);
         result = result / 2; // result is now 2
         System.out.println(result);
         result = result + 8; // result is now 10
         result = result % 7; // result is now 3
         System.out.println(result);
     }
}

class ConcatDemo {
     public static void main(String[] args){
         String firstString = "This is";
         String secondString = " a concatenated string.";
         String thirdString = firstString+secondString;
         System.out.println(thirdString);
     }
}

+ Unary plus operator; indicates positive value (numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
--     Decrement operator; decrements a value by 1
!     Logical complement operator; inverts the value of a boolean


class UnaryDemo {
     public static void main(String[] args){
         int result = +1; // result is now 1
         System.out.println(result);
         result--; // result is now 0
         System.out.println(result);
         result++; // result is now 1
         System.out.println(result);
         result = -result; // result is now -1
         System.out.println(result);
         boolean success = false;
         System.out.println(success); // false
         System.out.println(!success); // true
     }
}

class PrePostDemo {
     public static void main(String[] args){
         int i = 3;
i++;
System.out.println(i); // "4"
++i;    
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
     }
}

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

class ComparisonDemo {
     public static void main(String[] args){
         int value1 = 1;
         int value2 = 2;
         if(value1 == value2) System.out.println("value1 == value2");
         if(value1 != value2) System.out.println("value1 != value2");
         if(value1 > value2) System.out.println("value1 > value2");
         if(value1 < value2) System.out.println("value1 < value2");
         if(value1 <= value2) System.out.println("value1 <= value2");
     }
}