javase-运算符

来源:互联网 发布:网络证据 编辑:程序博客网 时间:2024/06/16 04:47

一.算术运算符

+、-、*、/、%、++、--

1.需注意的一些小细节  

//算数运算符public class Operator_1Arithmetic{    public static void main(String[] args) {        double a = 5;int b = 0;System.out.println(a/b);//输出为Infinity(无限大),只有当其中一个数为double或float类型时,才会输出此结果double aa = 35.66;double bb = 12.56;System.out.println(aa%bb);//因为计算机存储为二进制数,而小数不能完全转换为二进制,所以本来能够除尽的数,这里取余却为除不尽的数。byte c = 10;byte d = 120;//byte n = c + d;//short n = c + d;  都错误,因为编译器并不知道c+d的值是多少,会将其自动提升为int类型。int n = c + d;byte m = (byte)(c+d);//强制类型转换可以,但这里10+120大于127,会输出一个溢出值。System.out.println(n);System.out.println(m);    }}/***************JDK1.8结果***************Infinity10.539999999999996130-126*/


2.运算精度

如果表达式中有双精度浮点数(double型数据),则按双精度进行运算。

如果表达式中最高精度是单精度浮点数(float型数据),则按单精度进行运算。

如果表达式中最高精度是long型整数,则按long精度进行运算。

如果表达式中最高精度低于int型整数,则按int精度进行运算。

二.关系运算符   

==、!=、>、<、>=、<=

三.位运算符

&、|、^、~、<<、>>、>>>

应用于int、long、short、char、byte等类型。


//按位运算符public class Operator_2Bitwise {    public static void main(String[] args) {System.out.println(1&2);//按位与        对应位都为1,则为1,否则为0System.out.println(1|2);//按位或        对应位都为0,则为0,否则为1System.out.println(1^1);//按位异或     对应位都相同,则为0,否则为1System.out.println(~9);//按位取反     0变1,1变0   //二进制在内存中已补码的形式存储   //注:原码->补码:正数的补码与原码相同,负数的补码是对其原码逐位取反,但符号位除外,然后整个数加1。   //   补码->原码:正数的原码是补码本身,负数由右向左查找到第一个1与符号位之间的所有数字按位取反。   //~9的计算过程:   //转二进制:0 1001   //计算补码:0 1001   //按位取反:1 0110   //转为原码:1 1010   //符号位为1是负数,即-10System.out.println(1<<3);//按位左移  相当于1*2^3  计算2^3效率最高的算法。System.out.println(8>>2);//按位右移  相当于8/2^2}}/****************JDK1.8运行结果****************030-1082*/


四.逻辑运算符

&&、||、!

1.&&、||与 &、| 的区别

(1).&、|会将操作数都转换为二进制

(2).&、|的适用类型:int、long、short、char、byte

         &&、||的适用类型:boolean

(3).短路逻辑运算符:

当使用&&时,当得到第一个操作为false时,其结果就必定是false,这时候就不会再判断第二个操作了;使用||时,得到第一个操作数为true时,其结果必定为true,此时也不会执行第二个操作数。

五.赋值运算符

=、+=、-=、*=、/=、%=、<<=、>>=、&=、^=、|=

例:short  s1 = 1;

    s1 = s1 + 1;会发生错误,因为1是int型的,执行s1+1时,s1会自动提升为int型,将int型赋给short型的s1会丢失精度。

    而写成  s1+=1;相当于s1 = (short)(s1 + 1),不会出错。

六.条件运算符(唯一的三目运算符)

?:

七.instanceof运算符(判断对象是否是一个特定类型)

应用: 

import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;/** * date:2017年5月23日16:31:24 * target:instanceof的使用。 * @author ypy */class Pet {}class Dog extends Pet {}class Pug extends Dog {}class Cat extends Pet {}class Rodent extends Pet {}class Gerbil extends Rodent {}class Hamster extends Rodent {}//计数类class Counter { int i; }public class InstanceOf {public static void main(String[] args) throws InstantiationException, IllegalAccessException {Vector pets = new Vector();Class[] petTypes = {Pet.class,Dog.class,Pug.class,Cat.class,Rodent.class,Gerbil.class,Hamster.class,};for (int i=0; i<15; i++) {int rnd = (int) (1 + Math.random()*(petTypes.length-1));//random随机产生一个小于1的数,乘以(petTypes.length-1)后,产生一个小于(petTypes.length-1)的数,再加1,产生一个小于petTypes.length的数pets.addElement(petTypes[rnd].newInstance());//在petTypes数组中随机new出一个类的对象}Hashtable h = new Hashtable<>();for (int i=0; i<petTypes.length; i++) {h.put(petTypes[i].toString(), new Counter());//将petTypes数组中的每个元素的toString方法返回的字符串放入Hashtable,并给其一个Counter对象,用于计数}for (int i=0; i<pets.size(); i++) {Object o = pets.elementAt(i);//通过取出Hashtable中放置的toString方法返回的字符串判断该对象属于哪个类,并为其计数/*if (o instanceof Pet)((Counter)h.get("class dao.Pet")).i++;if(o instanceof Dog)((Counter)h.get("class dao.Dog")).i++;if(o instanceof Pug)((Counter)h.get("class dao.Pug")).i++;if(o instanceof Cat)((Counter)h.get("class dao.Cat")).i++;if(o instanceof Rodent)((Counter)h.get("class dao.Rodent")).i++;if(o instanceof Gerbil)((Counter)h.get("class dao.Gerbil")).i++;if(o instanceof Hamster)((Counter)h.get("class dao.Hamster")).i++;*///动态的 instanceoffor (int j = 0; j < petTypes.length; j++) {if (petTypes[j].isInstance(o)) {String key = petTypes[j].toString();((Counter)h.get(key)).i++;}}}for (int i=0; i<pets.size(); i++) {System.out.println(pets.elementAt(i).getClass().toString());}Enumeration keys = h.keys();while (keys.hasMoreElements()) {String nm = (String) keys.nextElement();Counter cnt = (Counter) h.get(nm);System.out.println(nm.substring(nm.lastIndexOf('.')+1) + "数量:" + cnt.i);//substring返回从第参数个字符开始到此字符串末尾的子字符串(从0开始) //lastIndexOf返回指定字符在此字符串中最后一次出现处的索引}}}/* *************** jdk1.8运行结果*************** class dao.Pugclass dao.Dogclass dao.Hamsterclass dao.Hamsterclass dao.Gerbilclass dao.Rodentclass dao.Pugclass dao.Pugclass dao.Dogclass dao.Pugclass dao.Pugclass dao.Pugclass dao.Rodentclass dao.Hamsterclass dao.HamsterGerbil数量:1Hamster数量:4Rodent数量:7Cat数量:0Pet数量:15Pug数量:6Dog数量:8 */


八.运算符优先级


 

原创粉丝点击