I'm a fledgling programmer for the first year. This is a collection of personal notes that have been organized and published for review purposes. Please note that there is a high possibility that you are wrong.
――If you do it with the image of mathematics that you learned at school, you will be surprised at the difference
x = x + 1;
And
count += 2;
Or, as a programmer, it feels like "Oh, yes", but for those who are just starting programming, it can be "! ??".
――Since it may differ from the meaning of general mathematical operators in this way, it is basically necessary to think of it as a different meaning.
On top of that, you should find a common place.
--The value described in the source code, which is an element in itself --The difference from operands is that literals have the described value itself as an element, but operands do not necessarily have the described value as the element itself.
int a , b;
a = 10; //10 is an integer literal
b = a + 27; //a,b,27 is an operand, but literals are only 27
boolean flag;
flag = true; //true is a boolean literal
--The operator performs calculations using the surrounding operands and changes to values with the operands.
Example
int numA = 1;
int numB = 2;
int ans = numA + numB;
//operator+Changes to a value of 3 with numA and numB.
if(numA < numB){
System.out.println("numA is less than numB");
}
//operator>Changes to true with numA and numB
System.out.println(numA == numB);
//When you actually output characters, the result will be false
--The site on the right is easy to understand (round throw). Reference) Java Road: Operator
――Roughly summarize in a table
type | operator | Meaning (roughly) | Remarks |
---|---|---|---|
Arithmetic operator | + | Add | |
- | Pull | ||
* | Hang | ||
/ | Divide | ||
% | Ask for the remainder | ||
Assignment operator | = | Substitute the right side for the left side | |
+= | Substitute the value obtained by adding the right side to the left side | ||
-= | Substitute the value obtained by dividing the right side from the left side | ||
*= | Substitute the value obtained by multiplying the left side by the right side | ||
/= | Substitute the value obtained by dividing the right side from the left side | ||
%= | Divide the left side Substitute the remainder of the right side | ||
Increment operator | ++ | Increase the value by 1 | a++When++The meaning is different with a † Note 1 |
Decrement operator | -- | Decrease the value by 1 | a--When--The meaning is different with a † Note 1 |
Comparison operator | == | equal | † Note 2 |
!= | Not equal | ||
> | large | ||
>= | that's all | ||
< | small | ||
<= | Less than | ||
Logical operator | & | And | |
&& | And | Perform short-circuit evaluation † Note 3 |
|
| | |||
|| | Or | Perform short-circuit evaluation † Note 3 |
|
! | denial |
† Note 1: For a ++, assign a value and then add +1. ++ a is +1 and then assigned.
Example
int a = 0;
int b = 0;
System.out.println(a++); //The result is 0
System.out.println(++b); //The result is 1
System.out.println(a); //The result is 1
System.out.println(b); //The result is 1
But in fact, most of the time I see ```i ++; `` `alone, and I haven't seen or written the code (for now) if I can't distinguish it. I think you should put it in the corner of your head.
† Note 2: Be careful when comparing objects. Reference) Comparison of objects † Note 3: What is short-circuit evaluation?
int num = 5;
if(num > 2 || num > 7){
//Process A
} else {
//Process B
}
If there is a process like this, num> 7 is not evaluated because it is confirmed that it will be true when the evaluation of num> 2 is completed. When the evaluation is confirmed in this way, not performing unnecessary evaluation after that is called ___ short-circuit evaluation ___.
--Use as below
How to write
[conditions] ? [Return value if TRUE] : [Return value for FALSE] ;
――Be careful because it is said that readability is poor if you use it easily. Until you get used to it, you should remember it exclusively when reading
int n = a > 0 ? 1 : 0 ;
//The meaning is the same as below
int n;
if(a > 0){
n = 1;
} else {
n = 0;
}
reference)
-Is the ternary operator evil?
Recommended Posts