字符转换与运算符

字符转换

public class Demo02 {
    public static void main(String[] args) {
        /*
        注意点:
        1.不能对Boolean值进行转换
        2.不能把对象类型转换为不相干的类型
        3.把高容量转换为低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题
        */
        int  i=128;
        byte b=(byte)i;//内存溢出
        double d=i;
        //强制转换 (类型)变量名 高-->低
        //自动转换 低-->高
        System.out.println(i);//128
        System.out.println(b);//-128
        System.out.println(d);//128.0

        System.out.println((int)23.7);//23
        System.out.println((int)-45.89f);//-45

        char c='a';
        int e=c+1;
        System.out.println(e);//98
        System.out.println((char)e);//b
    }
}

public class Demo03 {
    public static void main(String[] args) {
        //JDK7新特性,数字之间可用下划线分隔
        //大型数字注意溢出
        int money=10_0000_0000;
        int years=20;
        int total=money*years;
        System.out.println(money);//1000000000
        System.out.println(total);//-1474836480

        long total2=money*years;//默认是int,转换之前已经出错
        long total3=money*((long)years);
        System.out.println(total2);//-1474836480
        System.out.println(total3);//20000000000
    }
}

在这里插入图片描述

运算符

package operator;

public class Demo02 {
    public static void main(String[] args) {
        /*
        * A=0011 1100
        * B=0000 1101
        * -----------
        * A&B=0000 1100
        * A|B=0011 1101
        * A^B=0011 0001(异或 相同为0 不同为1)
        * ~B=1111 0010
        *
        * 2*8=16
        * <<  *2
        * >>  /2
        * */
        System.out.println(2<<3);//2*(2^3)=16

        //字符串连接符 +,String
        int a=10;
        int b=20;
        System.out.println(a+b);//30
        System.out.println(""+a+b);//1020
        System.out.println(a+b+"");//30

        //三元运算符
        //x?a:b
        //如果x==true,则结果为a,否则为b
        int score=50;
        String type= score<60?"不及格":"及格";
        System.out.println(type);

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值