欢迎使用CSDN-markdown编辑器

来源:互联网 发布:儿童认字软件 编辑:程序博客网 时间:2024/06/03 21:24

java学习总结(一)

最近学习了运算符,可分为如下:
1.算数运算符
2.关系运算符
3.逻辑运算符
4.位运算符
5.赋值运算符
6.其他运算符
算术运算符:
算术运算符用于在数学表达式中,他们是在代数中使用的方法相同。下表列出了算术运算符:

假设整型变量A=10和变量B=20,则:

算术运算实例

运算符 描述 实例 + Addition - Adds values on either side of the operator a+b=6 - Subtraction - Subtracts right hand operand from left hand operand a-b=2 * Multiplication - Multiplies values on either side of the operator a*b=8 / Division - Divides left hand operand by right hand operand a/b=2 ++ Increment - Increases the value of operand by 1 ++a==5 – Decrement - Decreases the value of operand by 1 –a==3;

Java定义了几个位运算符,它可以应用到整数类型,长型,整型,短整型,字符和字节。

位运算符作用于位,并执行逐位操作。假设当a =60和b= 13; 现在以二进制格式,他们将会如下:

a = 0011 1100

b = 0000 1101


a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

位运算符
假设当a =4和b= 2;

运算符 描述 实例 & Binary AND Operator copies a bit to the result if it exists in both operands 二进制a:0100 二进制b:0010 a&b==0(对位相与) l Binary OR Operator copies a bit if it exists in either operand. 二进制a:0100 二进制b:0010 allb==6(对位相或) ~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. 取反 二进制a:0100~a:1011 == -5 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. 二进制a^b:0110 << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a<<2 a==16 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a>>2 a==1 >>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. 设c=-3 c>>>2 c==1073741823

*个人理解 针对负数>>> 就是去补码位移最后结构不按带符号算

public class Demo1 {    public static void main(String args[]){        System.out.println("-3>>>2="+-3>>>2 );    }}
-3>>>2=1073741823

下表列出了逻辑运算符:

假设布尔变量A=ture,变量B=false,那么:

运算符 描述 实例 && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. ll Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. (A ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

有下列由Java语言支持赋值操作符:
a=3,b=5

运算符 描述 实例 = Simple assignment operator, Assigns values from right side operands to left side operand a+b如:3+5 +=,-= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand,Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand a+=1 如:a=a+1 a-=1 如:a=a-1 *=,/=,%= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand,Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand,Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand a/=3 如a=a/3;a*=2 如:a=a*2 a%=2 如 a=a%2 <<=,>>= Left shift AND assignment operator,Right shift AND assignment operator a<<=2 如 a=a<<2;a>>=2 如 a=a>>2 &= Bitwise AND assignment operator a&=3 如 a=a&3 ^= bitwise exclusive OR and assignment operator a^=2 如 a=a^2 l= bitwise inclusive OR and assignment operator al=2 如 a=al2

条件运算符也被称为三元运算符。该运算符包括三个操作数,用于评估计算布尔表达式。此运算符的目标是确定哪些值应分配给该变量。可写为:

public class Demo2 {    public static void main(String args[]){        int a = 2 ,b = 3  ,c;        c = (a==3) ? a :b ;        System.out.println("Value of c :"+c);    }}
Value of c :3

instanceof运算符:
这个操作符只用于对象引用变量。操作检查对象是否为特定类型(类类型或接口类型)。instanceof 运算符被写为:

public class Demo1 {    public static void main(String args[]){        String name = "tom";        boolean result = name  instanceof String;        System.out.println("result="+result);    }}
result=true
0 0
原创粉丝点击