03: Java basic grammar (b)

Java basic grammar

Java Operators

Arithmetic Operators

Operator is a special symbol to indicate operation, assignment and comparison data, and the like.

1, the number of operation: The operation involved in computing the number called.
2, the expression: operators and operands referred to overall expression. A single operand can also be seen as one of the most simple expression. Age +. 1. 1 = int;
. 3, Return Value: the operand or the result of calculation involved in the expression.
4, unary operators: only one operand operator. Also called unary operator.
5, binary operators: requires two operands operator. Also known as binary operators.
6, the ternary operator: require three operands operator. Also called ternary operator.
Note: all the data involved in the operation, are operands or expression of the return value of participation.


/% And applications [seeking random number one thousand, one hundred, ten, bit]

public  class the Test {
     public  static  void main (String [] args) {
         // arbitrary digit 
        Scanner Scanner = new new Scanner (the System.in); 
        of System.out.print ( "Please enter any digit:" );
         int = NUM scanner.nextInt ();
         int Unit NUM = 10%; // bit 
        int Decade NUM = / 10% 10; // ten 
        int hundred NUM = / 100% 10; // one hundred 
        int the Thousands NUM = / 1000% 10; // one thousand
        System.out.println ( "one thousand:" + Thousands + "\ n one hundred:" + hundred + "\ n ten:" + decade + "\ n bits:" + Unit); 
    } 
}
Code demonstrates

 
and the similarities and differences i ++ ++ i in
common:
. 1, and i ++ ++ i variables are incremented +1, are equivalent. 1 + I = I
2, if i ++, ++ i is a single statement, both did not make any difference.
3, i ++ and ++ i use only for variables. 5 5 ++ and ++ will complain, because 5 is not a variable.
Differences:
If i ++, ++ i is not a single statement, they have a big difference.

Assignment Operators


Symbols: = comply assignment operator: [in the Java: + =, - =, * =, / =,% =] => Type A lift
compound assignment operators characteristics:
1, the resulting byte code files to a slightly smaller little (less memory consumption).
2, implied a type of forced conversion. Type casts to the left of the variable
type * values to the left and right value of the expression returns the value of the type to be compatible, otherwise there will be loss of precision.
Example: int A, B, C; A = B = C =. 3;
   int. 3 = A; A + =. 5; => = A +. 5 <==> A = + A. 5

Comparison operators (relational operator)


1,>, <,> =, <=: only applicable to numeric data types including char.
2, ==,! = Apply to all data types java support.

Logical Operators

Operands must be involved in the logical operation boolean data type. Boolean expression is a boolean value. May not be written in Java, 3 <x <6, should be written as x> 3 & x <6





shorted (&&), conditions (&) difference:
&&: If the left side of the expression is true, the expression to the right of participating in operation; if on the left is false, then the right not to participate in operations, direct return results of the entire logical expression is false.
&: Left expression calculation result whether true or false, the right have carried out operations.
* Efficiency is often higher than the short-circuit logic, typically used with short-circuit &&

1 int value = 1;
2 System.out.println((1 < 2) & (value++ > 0)); // true
3 System.out.println((1 > 2) & (value++ > 0)); // false


Short circuit or (||), conditions, or (|) difference:

|: No matter what the result of the operation on the left is the right to participate in operations;
||: When left is true, the right not to participate in operations; direct return value of the entire logical expression true

1 int value = 1;
2 System.out.println((1 < 2) | (value++ > 0)); // true
3 System.out.println((1 > 2) || (value++ > 2)); // false


异或( ^ )与或( | )的不同之处是:只有表达式左右都为true时,结果为false,当两边都为false,结果也为false

1 // 一边为true,一边为false
2 System.out.println((3 > 2) ^ (9 < 8)); // true
3 // 一边为false,一边为true
4 System.out.println((7 > 10) ^ (9 > 3)); // true
5 // 两边都为true
6 System.out.println((3 > 2) ^ (2 > 1)); // false
7 // 两边都为false
8 System.out.println((1 > 2) ^ (2 > 3)); // false

位运算符

解释:位运算符是直接对整数进行二进制运算


按位与( & ):二元运算符。

规则:低位对齐,进行按位与。 对应的位 有0则0  全1则1.
1:可以将某个整数的某些二进制位设置为0。
2:可以得到某一个数的制定区间的二进制表示的数据。

按位或( | ):二元运算符。
规则:低位对齐,进行按位或。对应的位,有1则1,全0则0

*如果两个操作数 对应的位上没有全1 的,那么进行按位或和加的效果一致

按位异或( ^ ):二元运算符。
规则:对应的位上 相异为1,相同为0

特点:一个数先后和同一个数据异或两次,结果是它自身

异或简单加密应用

按位取反( ~ ) :一元运算符。
规则:所有的二进制位0--->1 1--->0
System.out.println(~0);//-1
移位运算符
<< 左移 :高位被移走,包括符号位。低位补0

>> :带符号右移  :低位被移走,高位空出来的补符号位,正数补0 负数补1

三元运算符( 条件运算符 )

Java 中唯一的三目运算符。 需要三个操作数。可以是java 支持的任意的表达式。两个表达式的返回值的类型 需要兼容。
格式:条件表达式 ? 表达式1 : 表达式2
1)条件表达式为true,返回表达式1;
2)条件表达式为false,返回表达式2;

两值互换的几种方法:
1、定义第三方变量进行互换

2、数值直接运算   缺:数值过大超过int取值范围,出现强制转换,数据发生变化
3、异或:一个数异或同一个数两次,结果还是这个数(阅读性差)

运算符的优先级


1:乘除取余 大于 加减。
2:一元运算符 大于 二元运算符 的优先级。
3:二元运算符的优先级高于三元运算符的优先级。
4:赋值运算符优先级最低。
5:配合小括号使用。

random随机数

1、Math.random();返回值是一个随机的double 值
语句:Math.random() * (MAX-MIN) + MIN

取整:int value = (int)(Math.random() * (MAX-MIN) + MIN);

2、导入import java.util.Random产生随机数
位移运算符:一条语句产生随机数:int value = (random.nextInt()>>>1)%(MAX-MIN)+MIN;

随机数+三目运算符应用[ 求三个数的最大值 ]

 1 public class Test {
 2     public static void main(String[] args) {
 3         // 定义随机数区间
 4         final int MIN = 1, MAX = 100;
 5         Random random = new Random();
 6         int num1 = (random.nextInt() >>> 1) % (MAX - MIN) + MIN;
 7         int num2 = (random.nextInt() >>> 1) % (MAX - MIN) + MIN;
 8         int num3 = (random.nextInt() >>> 1) % (MAX - MIN) + MIN;
 9 
10         // int num1 = (int)(Math.random()*(MAX-MIN)+MIN);
11         // int num2 = (int)(Math.random()*(MAX-MIN)+MIN);
12         // int num3 = (int)(Math.random()*(MAX-MIN)+MIN);
13 
14         int max = num1 > num2 ? num1 : num2;
15         max = max > num3 ? max : num3;
16         System.out.print("num1 = " + num1 + "\tnum2 = " + num2 + "\tnum3 = " + num3 + "\tmax=>" + max);
17 
18     }
19 }

Guess you like

Origin www.cnblogs.com/cao-yin/p/11458101.html