post increment 연산자가 int를 반환하는 메서드에서 작동하지 않는 이유는 무엇입니까?
public void increment(){
int zero = 0;
int oneA = zero++; // Compiles
int oneB = 0++; // Doesn't compile
int oneC = getInt()++; // Doesn't compile
}
private int getInt(){
return 0;
}
그것들은 모두 int인데 왜 B & C가 컴파일되지 않습니까? ++
연산자가 다른 방식 과 관련이 = 0 + 1;
있습니까?
++ /-작업에 대한 잘못된 인수
i++
변수에 대한 할당 i
입니다.
귀하 zero++
의 경우은 zero = zero + 1
. 그래서 0++
의미 0 = 0 + 1
가 없습니다 getInt() = getInt() + 1
. 뿐만 아니라 .
좀 더 정확하게 :
int oneA = zero++;
방법
int oneA = zero;
zero = zero + 1; // OK, oneA == 0, zero == 1
int oneB = 0++;
방법
int oneB = 0;
0 = 0 + 1; // wrong, can't assign value to a value.
int oneC = getInt()++;
방법
int oneC = getInt();
getInt() = getInt() + 1; // wrong, can't assign value to a method return value.
보다 일반적인 관점에서 변수는 L- 값입니다 . 즉, 메모리 위치를 나타내므로 할당 할 수 있습니다. L 에서 L의 -value는 약자 왼쪽 할당 연산자 (즉, 측면 =
L - 값은 좌측 또는 (할당 연산자의 오른쪽에 하나 찾을 수 있더라도) x = y
예를 들면).
반대는 R- 값입니다 ( R 은 할당 연산자의 오른쪽을 나타냄 ). R- 값은 L- 값에 무언가를 할당하기 위해 할당 문의 오른쪽에서만 사용할 수 있습니다. 일반적으로 R- 값은 리터럴 (숫자, 문자열 ...) 및 메서드입니다.
JLS에 명시된 바와 같이 :
접미사 식의 결과는 숫자 형식으로 변환 할 수있는 (§5.1.8) 형식의 변수 여야합니다. 그렇지 않으면 컴파일 타임 오류가 발생합니다.
getInt()
아니다 int
getInt()
보고 int
++
연산자는 두 가지 일을합니다 increment
+assignment
그래서위한 ++
작업에 운영자는 증가 작동하는을의 결과를 저장하는 변수를 필요로 0
하고 getInt()
둘 수 없습니다.
사전 및 사후 연산자는 호출 될 때 변수 또는 lvalue에 대해서만 작동합니다. lvalue는 왼쪽 값의 약자입니다. 즉, 할당에서 왼쪽에 설 수있는 것입니다. 귀하의 예에서 :
zero = 1; // OK
0 = 1; // Meaningless
getInt() = 1; // Also meaningless
// jk
B와 C 모두 컴파일러는 다음과 같이 말합니다.
예상치 못한 유형, 필수 : 변수, 발견 : 값
따라서 값을 증가시킬 수없고 변수 만 증가시킬 수 있습니다.
post increment 연산자가 int를 반환하는 메서드에서 작동하지 않는 이유는 무엇입니까?
Because it is a getter method, and it doesn't make sense to change a value via getter.
int z = x + y++;
is equivalent to:
int z = x + y;
y = y + 1;
so it is not valid to have something like:
int z = x + getY()++;
which is equivalent to:
int z = x + getY();
getY() = getY() + 1; // invalid!
0++
It is equivalent to 0 = 0 + 1;
and certainly it is not possible.
i.e. it has to be l-value
to assign to it.
getInt()++;
Similar reason here.
Because 0
is a rValue
(i.e. You can use it only from right of the assignment operator) not a lValue
.
++
operator increments the value and sets it to itself therefore 0++
will give You an error.
My answer its kind of "out of the box".
When I have doubt about an operator usage, I think "which its the overloaded function equivalent" of this operator ?
I, know, that Java operators doesn't have operator overloading, its just an alternative way to make a solution.
In this case:
...
x++;
...
should be read as:
...
int /* function */ postincrement (/* ref */ int avalue)
{
int Result = avalue;
// reference value,
avalue = avalue + 1;
return Result;
}
...
postincrement(/* ref */ x);
...
And:
...
++x;
...
...
int /* function */ preincrement (/* ref */ int avalue)
{
// reference value,
avalue = avalue + 1;
int Result = avalue;
return Result;
}
...
preincrement(/* ref */ x);
...
So, both, versions of "++", work as a function that receives a variable parameter by reference.
So, a literal value like "0++" or a function result like "getInt()++", are not a variable references.
Cheers.
postincrement and preincrement can apply only with the help of variable.So the first case compile.
Since function return is RHS expression and pre/post increment/decrement operations can be applied to LHS expressions only.
'Program Tip' 카테고리의 다른 글
SPAN_EXCLUSIVE_EXCLUSIVE와 같은 Span 플래그의 의미를 설명하십시오. (0) | 2020.12.02 |
---|---|
UITableView 배경 이미지 (0) | 2020.12.02 |
if 문에서 여러 옵션 풀기 (0) | 2020.12.02 |
Qt 링커 오류 : "vtable에 대한 정의되지 않은 참조" (0) | 2020.12.02 |
CSS에서 최소 글꼴 크기를 설정하는 방법 (0) | 2020.12.02 |