In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to sort out the operators in the basic grammar of Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Generally speaking, there are two basic elements of a Java program, one is an expression and the other is an operator. Operator, as an important knowledge point of Java basic grammar, is the foundation of learning.
1. Arithmetic operator
(1) operators and expressions
Operator: a symbol that operates on a constant or variable. Expressions: expressions that join constants or variables with operators that conform to the java syntax can be called expressions. Expressions connected by different operators represent different types of expressions.
Examples are as follows:
```java
Int a = 10
Int b = 20
Int c = a + b
`
+: is an operator and is an arithmetic operator.
A + b: is an expression, which is called an arithmetic expression because + is an arithmetic operator.
(2) arithmetic operator
+, -, *, / correspond to addition, subtraction, multiplication and division, respectively. What you need to remember is that the function of% is to take the remainder, that is, to obtain the remainder of the division of the two data.
Note: the difference between / and%: two data are divided, / the quotient of the result is taken, and the remainder of the result is taken. Integer operation can only get integers, to get decimals, there must be floating-point numbers to participate in the operation.
~ java
Int a = 10
Int b = 3
System.out.println (a / b); / / output result 3
System.out.println (a% b); / / output result 1
~ ~
(3) "+" operation of characters
The char type participates in arithmetic operations and uses the decimal values corresponding to the underlying computer. We need to remember the values corresponding to the three characters:
'a'- 97 a'z is continuous, so the corresponding value of'b'is 98, and the corresponding value is 99, which is added in turn.
'A'-65 Amurz is continuous, so the corresponding value of'B' is 66 and the corresponding value is 67, which is added step by step.
'0'-48 0-9 is continuous, so the corresponding value of'1' is 49.'2' is 50, which is added step by step.
~ java
/ / you can get the corresponding value of a character by using characters and integers to do arithmetic operations.
Char ch2 ='a'
System.out.println (ch2 + 1); / / output 98 Ji 97 + 1 = 98
Char ch3 ='A'
System.out.println (ch3 + 1); / / output 66j65 + 1 = 66
Char ch4 ='0'
System.out.println (ch4 + 1); / / output 49pm 48 + 1 = 49
~ ~
When an arithmetic expression contains values of different basic data types, the type of the entire arithmetic expression is automatically promoted.
Promotion rules:
Byte types, short types, and char types will be promoted to int types, regardless of whether or not other types are involved in the operation. The type of the entire expression is automatically promoted to the same type as the highest-level Operand in the expression. Rank order: byte,short,char-- > int-- > long-- > float-- > double
For example:
~ java
Byte b1 = 10
Byte b2 = 20
/ / byte b3 = b1 + b2; / / this line reports an error because the byte type participating in the arithmetic operation automatically prompts that assigning a value to int,int to byte may lose precision
Int i3 = b1 + b2; / / should be received using int
Byte b3 = (byte) (b1 + b2); / / or cast the result to byte type
-
Int num1 = 10
Double num2 = 20.0
Double num3 = num1 + num2; / / receive using double because num1 is automatically promoted to double type
~ ~
Note: for these reasons, we rarely use byte or short types to define integers in program development. It is also rare to use the char type to define characters, but to use the string type, let alone the char type to do arithmetic operations.
(4) "+" operation of string
When a string appears in the "+" operation, the "+" is a string connector, not an arithmetic operation.
~ java
System.out.println ("itheima" + 666); / / output: itheima666
~ ~
In the "+" operation, if a string appears, it is a concatenation operator, otherwise it is an arithmetic operation. When the "+" operation is performed continuously, it is performed one by one from left to right.
~ java
System.out.println (1 + 99 + "year dark horse"); / / output: 100 years dark horse
System.out.println (1 + 2 + "itheima" + 3 + 4); / / output: 3itheima34
/ / you can use parentheses to change the priority of the operation
System.out.println (1 + 2 + "itheima" + (3 + 4)); / / output: 3itheima7
~ ~
2. Assignment operator
The assignment operator assigns the value of an expression to the left, which must be modifiable, not constant.
Note: the extended assignment operator implies a cast.
~ java
Short s = 10
S = s + 10; / / this line of code reports, because s is promoted to int type in the operation, the precision may be lost when the result int is assigned to short.
S + = 10; / / there is no problem with this line of code, implying a cast, which is equivalent to s = (short) (s + 10)
~ ~
3. Self-increasing and self-subtracting operators
Note: + + and-- can be placed either behind the variable or in front of the variable; when used alone, the result is the same whether in front of the variable or after the variable; when participating in the operation, if you put it after the variable, first take the variable to participate in the operation, and then take the variable to do + + or-- When participating in the operation, if you put it in front of the variable, take the variable as + + or-- first, and then take the variable to participate in the operation. The most common usage: used alone.
After reading the above, do you have any further understanding of how to sort out the operators in the basic grammar of Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.