본문 바로가기
Java 배우기

Assignment, Arithmetic, and Unary Operators

by 노화방지 Anti-aging Hairstyle 2016. 1. 5.
반응형

The Simple Assignment Operator


마주치게 되는 가장 보편적인 연산자 중 하나는 간단한 assignment 연산자인 "=" 입니다.
이미 클래스 Bicycle에서 이 연산자를 본 적이 있습니다; 이 연산자의 오른 쪽의 값을 왼 쪽의 피연산자(operand) 에게 할당합니다:
One of the most common operators that you'll encounter is the simple assignment operator "=". You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:

 int cadence = 0;
 int speed = 0;
 int gear = 1;

이 연산자는 또한 Creating Objects에서 논의된 객체에서 객체 참조(object references) 를 할당하는 데에도 사용될 수 있습니다.
This operator can also be used on objects to assign object references, as discussed in Creating Objects.


산술 연산자 The Arithmetic Operators


자바 프로그래밍 언어는 덧셈, 뺄셈, 곱셈, 나눗셈을 수행하는 연산자를 제공합니다.
이들 연산자들을 기초 수학의 연산자들로 인식하는 좋은 기회가 있습니다. 
새로운 기호는 한 피연산자를 다른 피연산자로 나눈 나머지를 결과로 반환하는 "%" 입니다.
The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.

연산자 Operator설명 Description
+

덧셈연산자 Additive operator (also used for String concatenation)

-

뺄셈연산자 Subtraction operator

*

곱셈연산자 Multiplication operator

/나눗셈연산자 Division operator
%나머지연산자 Remainder operator


다음 프로그램, ArithmeticDemo는  산술 연산자를 테스트 합니다.
The following program, ArithmeticDemo, tests the arithmetic operators.

class ArithmeticDemo {

    public static void main (String[] args) {

        int result = 1 + 2;
        // result is now 3
        System.out.println("1 + 2 = " + result);
        int original_result = result;

        result = result - 1;
        // result is now 2
        System.out.println(original_result + " - 1 = " + result);
        original_result = result;

        result = result * 2;
        // result is now 4
        System.out.println(original_result + " * 2 = " + result);
        original_result = result;

        result = result / 2;
        // result is now 2
        System.out.println(original_result + " / 2 = " + result);
        original_result = result;

        result = result + 8;
        // result is now 10
        System.out.println(original_result + " + 8 = " + result);
        original_result = result;

        result = result % 7;
        // result is now 3
        System.out.println(original_result + " % 7 = " + result);
    }
}

This program prints the following:

1 + 2 = 3
3 - 1 = 2
2 * 2 = 4
4 / 2 = 2
2 + 8 = 10
10 % 7 = 3

You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.

The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:

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);
    }
}

By the end of this program, the variable thirdString contains "This is a concatenated string.", which gets printed to standard output.


The Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

OperatorDescription
+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

The following program, UnaryDemo, tests the unary operators:

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;
        // false
        System.out.println(success);
        // true
        System.out.println(!success);
    }
}

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:

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


반응형

'Java 배우기' 카테고리의 다른 글

Bitwise and Bit Shift Operators  (0) 2016.01.05
Equality, Relational, and Conditional Operators  (0) 2016.01.05
Operators 연산자  (0) 2016.01.05
Summary of Variables 변수 요약  (0) 2016.01.05
Arrays 배열  (0) 2016.01.05

댓글