为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA技术之运算符

标签:
Java

JAVA技术之运算符

数学函数与常量

public class Note {
    public static void main(String[] args) {
//平方根计算
        System.out.println(Math.sqrt(4));
        System.out.println(Math.sqrt(12));
        System.out.println(Math.sqrt(0));
        System.out.println(Math.sqrt(-12));
//输出结果:2.0
//3.4641016151377544
//0.0
//NaN
//pow为表示a的b次幂,幂函数算法涉及到高中部分的知识,幂可为正数和负数,0,请不要考虑虚数...
//pow返回的是double类型
        System.out.println(Math.pow(4,2));//4的2次方=16
        System.out.println(Math.pow(0,0));//任何数的0次幂都是1
        System.out.println(Math.pow(4,-2));//由于a=4,即a!=0,则此相当于1.0/16
//输出结果:16.0
//1
//0.0625
//三角函数计算
        System.out.println(Math.sin(90));
        System.out.println(Math.sin(Math.PI/2));
        System.out.println(Math.cos(30));
        System.out.println(Math.cos(0));
        System.out.println(Math.tan(45));
        System.out.println(Math.tan(Math.PI/4));
//输出结果
//0.8939966636005579
//1.0
//0.15425144988758405
//1.0
//1.6197751905438615
//0.9999999999999999
    }
}

强制类型转换

6个实箭头,3个虚箭头,实箭头表示没有精度损失,虚箭头表示有精度损失当两个或多个不同的操作数进行运算,会先转换为同一个类型,然后计算

  • 如果两个操作数中有一个是double类型,另一个操作数就会转换为double类型。

  • 否则,如果其中-一个操作数是float类型,另一个操作数将会转换为float类型。

  • 否则,如果其中-一个操作数是long类型,另一个操作数将会转换为long类型。

否则,两个操作数都将被转换为int类型。 强制类型转换(cast) 圆括号里面表示需要转换后的类型,也可以叫窄化转换

  • 只要类型比int小(即byte或者short),那么运算之前这些会自动转换成int,最终结果就是int类型,如果赋值给较小类型就需要窄化转换

  • 表达式中出现的最大的数据类型决定了表达式最终的结果的数据类型

  • 如果将一个float值与double值相乘,结果就是double如果将一个int值与long值相加,结果就是long

public class Note {
    public static void main(String[] args) {
//强制转换,小数部分会被截掉变为整数
        double x = 9.997;
        int nx = (int) x;//double转换为int,需要强制转换
        System.out.println(nx);
        nx = (int) Math.round(x);//round返回为long类型,long转换为int,需要强制转换
        System.out.println(nx);
//输出结果:
//9
//10
//如果转换的值超过所需要转换后的类型最大值,会得到完全不同的一个值
        byte b = (byte) 300;
        System.out.println(Byte.MAX_VALUE);//byte类型最大值127
        System.out.println(b);
//输出结果
//127
//44
//极少数情况下需要boolean参与转换
        boolean is = Math.random() + 2 <= 2.5;
        double i = is ? 0 : 1;
        System.out.println(i);
//输出结果
//随机,可能为1也可能为1
    }
}

结合赋值和运算符

+(加) -(减) *(乘) /(除) %(取模)%:它从整除法中产生余数,如果得到小数,则去除小数部分

public class Note {
    public static void main(String[] args) {
//使用二元运算符,很简洁的方式
        int x = 4;
        x += 4;
        System.out.println(x);
        x = 4;
        x = x + 4;
        System.out.println(x);
//输出结果
//8
//8
//同理-=,*=,/=%=等
    }
}

自增与自减运算符

  • 自动递增:操作符"++",增加一个单位

  • 自动递减:操作符"–",减少一个单位

  • 前缀式:先执行运算再生成值

  • 后缀式:先生成值再执行运算

public class Note {
    public static void main(String[] args) {
        int i = 1;
        System.out.println("i:" + i);
//先加1后赋值 2
        System.out.println("++i:" + ++i);
//先赋值再加1 2 下一次使用变量时值为3
        System.out.println("i++:" + i++);
//输出3
        System.out.println("i:" + i);
//先减1再赋值 2
        System.out.println("--i:" + --i);
//先赋值再减1 2 下一次使用变量值为1
        System.out.println("i--:" + i--);
//输出1
        System.out.println("i:" + i);
    }
}

关系和boolean运算符

小于(<),大于或等于(>=),小于或等于(<=),等于(==),不等于(!=)

//<,>,<=,>=,!=,== 这些常用操作符和数学表达等式或不等式含义一致

//&& || 位运算符也是很常用的关系运算符

//关系操作符生成的是一个boolean(布尔)结果

//可以用于操作数的值之间的关系,如果是真的就返回true(真),否则返回false(假)

三目(元)运算(操作)符

condition?expression1?expression2 
//三元操作符 condition ? expression1 : expression2  如 x < y ? x : y  如果x int x=1,y=9; boolean is=x < y ? x : y; boolean-exp ? value0:value1 
//如果boolean-exp的结果为true,则计算value0 //如果boolean-exp的结果为false,则计算value1
public class Note {
    static int ternary(int i) {
        return i < 10 ? i * 100 : i * 10;
    }
    static int standardIfElse(int i) {
        if (i < 10)
            return i * 100;
        else
            return i * 10;
    }
    public static void main(String[] args) {
        System.out.println(ternary(9));
        System.out.println(ternary(10));
        System.out.println(standardIfElse(9));
        System.out.println(standardIfElse(10));
//输出结果
//900
//100
//900
//100
    }
}

测试对象的等价性

关系操作符==!=也适用于对象
public class Note {
    public static void main(String[] args) {
//内容为47
        Integer n1 = new Integer(47);
//内容为47
        Integer n2 = new Integer(47);
//==,!= 即会判断内容,还会判断对象的引用,如果对象的引用不一致则返回false
        System.out.println(n1 == n2);
        System.out.println(n1 != n2);
//n1,n2对象对应的Integer类重写了equals()方法,所以只会比较内容是否一致,不会比较对象的引用是否一致
//重写Object的equals 方法 内容为: value == ((Integer)obj).intValue();  //比较内容
        System.out.println(n1.equals(n2));
        Value v1 = new Value();
        Value v2 = new Value();
        v1.i = v2.i = 100;
//v1,v2对象没有重写equals()方法,所以会比较对象的引用
//继承Object的equals 方法 内容为: this == obj  //比较引用
        System.out.println(v1.equals(v2));
//equals(参数) 方法默认对象引用的比较
//即是否为同一个对象
    }
    static class Value {
        int i;
    }
}

位运算符

,>>> 位模式左移和右移,需要建立位模式进行掩码,>>>运算符会用0填充高位,>>运算符会用符号位填充高位,不存在<<<运算符

import java.util.Random;
public class Note {
    public static void main(String[] args) {
        /*需要导入包:import java.util.Random;*/
//与 (&&) 或(||) 非(!)
        Random random = new Random(47);
        int i = random.nextInt(100);
        int j = random.nextInt(100);
        System.out.println("i:" + i);
        System.out.println("j:" + j);
        System.out.println("i>j is " + (i > j));
        System.out.println("i<j is " + (i < j));
        System.out.println("i>=j is " + (i >= j));
        System.out.println("i<=j is " + (i <= j));
        System.out.println("i==j is " + (i == j));
        System.out.println("i!=j is " + (i != j));
//以下是错误的操作
//System.out.println("i$$j is " + (i && j));
//System.out.println("i||j is " + (i || j));
//System.out.println("!i is " + (!i);
        System.out.println("(i<10)&&(j<10) is " + ((i < 10) && (j < 10)));
        System.out.println("(i<10)||(j<10) is " + ((i < 10) || (j < 10)));
    }
}

括号与运算符级别

一般更多表达式应当多使用()小括号的方式,以此避免考虑优先级导致结果判断或分析失误的情况当一个表达式中存在多个操作符,操作符的 优先级决定各部分的计算顺序使用括号可以明确规定计算顺序

public class Note {
    public static void main(String[] args) {
        int x = 1, y = 2, z = 3;
//操作符接受 +(加) -(减) *(乘) /(除) =(赋值)  用于操作数值,生成新的数值,
// 也可以改变自身的值,称为"副作用"
//几乎所有的操作符只能操作"基本类型",除了操作符"=","=="和"!="还可以操作对象
//String 支持"+"和"+="
//即 1+2-1+3=5
        int a = x + y - 2 / 2 + z;
//即 1+(2-2)/(2+3)=1
        int b = x + (y - 2) / (2 + z);
//"+"为"字符串连接"操作符
//a,b转换为字符串,即非字符串通过连接符"+"转换成字符串String类型
        System.out.println("a = " + a + " b = " + b);
//输出结果
//a = 5 b = 1
    }
}
运算符 结合性
[].()(方法调用) 从左向右
! ~ + ± -+(一元运算)-(一元运算)()(强制转换类型)new 从右向左
* / % 从左向右
+ - 从左向右
<< >> >>> 从左向右
< <= > >= instanceof 从左向右
== != 从左向右
& 从左向右
^ 从左向右
| 从左向右
&& 从左向右
|| 从左向右
?: 从右向左
= += -= *= /= %= &= |= ^= <<= >>= >>>= 从右向左

枚举类型

枚举类型可以避免变量中取值隐患,例如类型的不一致,取值不正确枚举类型包括有限个命名的值。

e_ Size { SMALL, MEDIUM, LARGE, EXTRA.LARCE };
现在,可以声明这种类型的变量
Size s = Size.MEDIUM;
Size 类型的变量只能存储这个类型声明中给定的某个枚举值,或者 null 值,null 表示这个变量没有设置任何值
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
1

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消