Java의 바로 가기 "또는 할당"(| =) 연산자
Java에서 수행 할 긴 비교 세트가 있으며 그중 하나 이상이 사실인지 알고 싶습니다. 비교 문자열이 길고 읽기 어려웠 기 때문에 가독성을 위해 분리했고 자동으로 . |=
대신 단축키 연산자를 사용했습니다 negativeValue = negativeValue || boolean
.
boolean negativeValue = false;
negativeValue |= (defaultStock < 0);
negativeValue |= (defaultWholesale < 0);
negativeValue |= (defaultRetail < 0);
negativeValue |= (defaultDelivery < 0);
negativeValue
default <something> 값이 음수이면 true가 될 것으로 예상 합니다. 이것이 유효합니까? 내가 기대하는대로 작동할까요? Sun의 사이트 또는 stackoverflow에서 언급 된 것을 볼 수 없었지만 Eclipse에는 문제가없는 것 같고 코드가 컴파일되고 실행됩니다.
마찬가지로 여러 논리적 교차를 수행하려면 &=
대신 &&
?를 사용할 수 있습니다 .
는 |=
복합 할당 연산자이다 ( JLS 15.26.2 부울 논리 연산자) |
( JLS 15.22.2 ); conditional-or ||
( JLS 15.24 ) 와 혼동하지 마십시오 . 또한 거기 &=
와 ^=
논리적 부울의 화합물 할당 버전에 대응 &
하고 ^
, 각각.
즉,의 boolean b1, b2
경우이 두 가지는 동일합니다.
b1 |= b2;
b1 = b1 | b2;
논리 연산자의 차이 ( &
및 |
그 조건 대응 (비교) &&
과는 ||
) 전자는 "단락"을하지 않는 것입니다; 후자는합니다. 그건:
&
그리고|
항상 두 피연산자를 평가&&
그리고||
우측 피연산자를 평가 조건부 ; 오른쪽 피연산자는 해당 값이 이진 연산의 결과에 영향을 미칠 수있는 경우에만 평가됩니다. 즉, 다음과 같은 경우 오른쪽 피연산자가 평가되지 않습니다.- 의 왼쪽 피연산자는 다음으로
&&
평가됩니다.false
- (오른쪽 피연산자가 무엇으로 평가 되든 전체 표현식은이므로
false
)
- (오른쪽 피연산자가 무엇으로 평가 되든 전체 표현식은이므로
- 의 왼쪽 피연산자는 다음으로
||
평가됩니다.true
- (오른쪽 피연산자가 무엇으로 평가 되든 전체 표현식은이므로
true
)
- (오른쪽 피연산자가 무엇으로 평가 되든 전체 표현식은이므로
- 의 왼쪽 피연산자는 다음으로
그래서 원래의 질문으로 돌아가서, 예, 그 구조는 유효하며 |=
는 =
및 ||
에 대한 바로 가기는 정확하지 않지만 원하는 것을 계산합니다. |=
사용 하는 연산자 의 오른쪽 은 간단한 정수 비교 연산이므로 |
단락되지 않는 사실 은 중요하지 않습니다.
단락이 필요하거나 필요한 경우가 있지만 시나리오는 그중 하나가 아닙니다.
다른 언어와 달리 Java에는 &&=
및 ||=
. 이것은 왜 Java에 conditional-and and conditional-or 연산자의 복합 할당 버전이 없습니까? 라는 질문에서 논의되었습니다 . (&& =, || =) .
그런 식으로 "바로 가기"(또는 단락) 연산자가 아닙니다 || &&는 (LHS를 기반으로 한 결과를 이미 알고있는 경우 RHS를 평가하지 않는다는 점에서) 작업 측면에서 원하는 작업을 수행 합니다.
차이점의 예로서이 코드 text
는 null이면 괜찮습니다 .
boolean nullOrEmpty = text == null || text.equals("")
그러나 이것은 다음과 같지 않습니다.
boolean nullOrEmpty = false;
nullOrEmpty |= text == null;
nullOrEmpty |= text.equals(""); // Throws exception if text is null
(분명히 당신은 "".equals(text)
그 특정한 경우에 대해 할 수 있습니다 -나는 단지 원리를 보여주기위한 것입니다.)
당신은 단지 하나의 진술을 가질 수 있습니다. 여러 줄로 표현하면 샘플 코드와 거의 똑같이 읽습니다.
boolean negativeValue
= defaultStock < 0
| defaultWholesale < 0
| defaultRetail < 0
| defaultDelivery < 0;
가장 단순한 표현식의 경우 using 이 비교를 피하더라도 암시 적으로 분기를 사용하는 것을 의미하고 훨씬 더 많은 비용이들 수 있기 때문에 사용하는 |
것이 더 빠를 ||
수 있습니다.
그것은 당신의 문제에 과잉 일 수 있지만, Guava 라이브러리는 Predicate
s와 함께 멋진 구문을 가지고 있으며 or / and Predicate
s에 대한 단락 평가를 수행 합니다.
기본적으로 비교는 객체로 변환되고 컬렉션으로 패키징 된 다음 반복됩니다. 또는 술어의 경우 첫 번째 실제 적중이 반복에서 리턴되고 and의 경우 그 반대의 경우도 마찬가지입니다.
If it is about readability I've got the concept of separation tested data from the testing logic. Code sample:
// declare data
DataType [] dataToTest = new DataType[] {
defaultStock,
defaultWholesale,
defaultRetail,
defaultDelivery
}
// define logic
boolean checkIfAnyNegative(DataType [] data) {
boolean negativeValue = false;
int i = 0;
while (!negativeValue && i < data.length) {
negativeValue = data[i++] < 0;
}
return negativeValue;
}
The code looks more verbose and self-explanatory. You may even create an array in method call, like this:
checkIfAnyNegative(new DataType[] {
defaultStock,
defaultWholesale,
defaultRetail,
defaultDelivery
});
It's more readable than 'comparison string', and also has performance advantage of short-circuiting (at the cost of array allocation and method call).
Edit: Even more readability can be simply achieved by using varargs parameters:
Method signature would be:
boolean checkIfAnyNegative(DataType ... data)
And the call could look like this:
checkIfAnyNegative( defaultStock, defaultWholesale, defaultRetail, defaultDelivery );
It's an old post but in order to provide a different perspective for beginners, I would like give an example.
I think the most common use case for a similar compound operator would be +=
. I'm sure we all wrote something like this:
int a = 10; // a = 10
a += 5; // a = 15
What was the point of this? The point was to avoid boilerplate and eliminate the repetitive code.
So, next line does exactly the same, avoiding to type the variable b1
twice in the same line.
b1 |= b2;
List<Integer> params = Arrays.asList (defaultStock, defaultWholesale,
defaultRetail, defaultDelivery);
int minParam = Collections.min (params);
negativeValue = minParam < 0;
|| logical boolean OR
| bitwise OR
|= bitwise inclusive OR and assignment operator
The reason why |= doesn't shortcircit is because it does a bitwise OR not a logical OR. That is to say:
C |= 2 is same as C = C | 2
참고URL : https://stackoverflow.com/questions/2486472/shortcut-or-assignment-operator-in-java
'programing tip' 카테고리의 다른 글
Oracle SQL Developer SQL 워크 시트 창에서 텍스트 인쇄 (0) | 2020.09.18 |
---|---|
Java 7의 새로운 기능 (0) | 2020.09.18 |
모델 김태연 (0) | 2020.09.17 |
C #의 스택 크기가 정확히 1MB 인 이유는 무엇입니까? (0) | 2020.09.16 |
Win32 앱에서 디버그 출력 창에 인쇄하려면 어떻게해야합니까? (0) | 2020.09.16 |