Java基础,类型转换 变量和运算符

本文介绍了Java中的类型转换,包括强制转换可能引发的内存溢出问题和自动转换的规则。同时,讲解了常量的定义及其不变性,以及变量的分类如类变量、实例变量和局部变量。此外,还详细讨论了变量的命名规范和各种运算符的使用,如算数、关系、逻辑和位运算符。
摘要由CSDN通过智能技术生成

类型转换

强制转换和自动转换

public class Demo05 {
    public static void main(String[] args) {
        强制转换 (数据类型)变量名 从高-低
        int i = 128;
        byte b = (byte) i;  //内存溢出
        //自动转换 从低-高(这里的 高低 指的就是字节)
        double d = i;
        System.out.println(d);//128.0 double是浮点型所以带小数
        System.out.println(i);//128
        System.out.println(b);//-128
        /*
        注意点:
        1、不能对布尔值进行转换
        2、不能把对象类型转换为不相干的类型
        3、在把高容量转换为低容量的时候要进行 强制转换
        4、转换的时候可能出现内存溢出或者精度问题!
         */
        System.out.println("-----------------------------------");
        //精度问题
        System.out.println((int)23.89);//23
        System.out.println((int)-33.37);//33
    }
}

数据溢出

 public class Demo06 {
     public static void main(String[] args) {
         //操作比较大的数的时候,注意溢出问题
         //jdk7 特性,数字之间可以用下划线分割,且不影响输出
         int money = 10_0000_0000;
         int years = 20;
         int total = money*years;//-1474836480,数据溢出
         System.out.println(total);
         //解决方法转换数据类型
         long total2 = money*years;//-1474836480,默认是int,转换之前就已经存在问题
         System.out.println(total2);
         //将其中一个转换为long型
         long total3 = money*((long)years);//20000000000
         System.out.println(total3);
     }
 }
 ​

常量

初始化之后不能在改变值,不会变动的值。

常量可以理解为一种特殊的变量,他的值设定后在程序运行阶段不允许被改变

常量名一边用大写字符

final 常量名 = 值;

final double PI = 3.14;

变量作用域

类变量、实例变量、局部变量

public class Demo07 {
    //类变量 static
    static double salary = 3000;
    //属性:变量

    //实例变量:从属于对象,如果不自动初始化值,这个类型默认输出的值就是 0、0.0
    //布尔类型默认false
    //除了基本类型,其余都是null
    String name;
    int age;


    //main 方法
    public static void main(String[] args) {
        //局部变量,必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型  变量名 = new 变量名()
        Demo07 demo07 = new Demo07();
        System.out.println(demo07.name); //null
        System.out.println(demo07.age); //0
        //类变量 static
        System.out.println(salary);
    }
    //其他方法
    public void add(){

    }
}

变量的命名规范:

  • 所有的变量、方法、类名必须都见名知意

  • 类成员变量首字母小写,驼峰式命名原则:monthSalary

  • 局部变量首字母小写和驼峰式命名原则

  • 常量大写字母和下划线:MAX_VALUE

  • 类名首字母大写和驼峰原则:Man、GoodMan

  • 方法明首字母小写和驼峰原则:run()、runRun()

运算符

  • 算数运算符:+、-、*、\、%、++、--

  • 关系运算符:>、<、<=、>=、==、!= instanceof

  • 赋值运算符:=

  • 逻辑运算符:&&、||、!

  • 位运算符:&、|、^、~、>>、<<、>>> (了解)

  • 条件运算符?:

  • 扩展赋值运算符:+=、-=、*=、/=

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符 快捷键:Ctrl+D 复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//类型转换,不然数值不对
    }
}

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123L;
        int b = 123;
        short c = 12;
        byte d = 8;
        System.out.println(a+b+c+d);//Long 类型,只要操作过程中有Long类型数据 输出结果就是Long类型
        System.out.println(b+c+d);//Int
        System.out.println(c+d);//Int(没有Long的时候结果都为Int)
    }
}
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符:运算结果:正确、错误、通过布尔值表示
        int a = 10;
        int b = 20;
        int c = 22;
        System.out.println(c % a);//c对a取余,结果是2,模运算
        System.out.println(a > b);//false
        System.out.println(a < b);//true
        System.out.println(a == b);//false
        System.out.println(a != b);//true
    }
}
public class Demo04 {
    public static void main(String[] args) {
        //++、-- 自增  自减 一元运算符
        int a = 3;
        int b = a++;//执行这行代码前,先给b赋值,再自增
        System.out.println(b);
        System.out.println(a);
        int c = ++a;//执行这行代码前,先自增,再给b赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //幂运算,2*2*2 = 8,很多操作需要用到一些工具类
        double pow = Math.pow(2,3);
        System.out.println(pow);//8.0
    }
}
public class Demo05 {
    public static void main(String[] args) {
        //逻辑运算:与(and)、或(or)、非(取反,不是你就是我)
        boolean a = true;
        boolean b = false;
        System.out.println("a && b:" + (a && b));//false,逻辑与运算:两个变量都为真结果才是,true
        System.out.println("a || b:" + (a || b));//true,逻辑或运算:两个变量有一个为真结果才是,true
        System.out.println("!(a && b):" + !(a && b));//true,逻辑非运算:如果是真则变为假,如果是假则变为真
        //短路运算,
        int c = 5;
        boolean d = (c < 4) && (c++ > 6);//如果前面的是错的,后面代码就不会执行
        System.out.println(c);//5
        System.out.println(d);//false
    }
}
public class Demo06 {
    public static void main(String[] args) {
        /*
        位运算:常用于2进制,效率极高!!!
        A = 0011 1100
        B = 0000 1101
        _____________________________________
        A&B = 0000 1100(a与b)
        A/B = 0011 1101(a或b)
        A^B = 0011 0001(取反如果位相同则为0,如果不同则为1,^用于两个二进制数)
        ~B = 1111 0010 (取反B,~用于一个二进制数)
        ------------------------------------
        如果是2*8 = 16,如何计算 2*2*2*2
        <<(左移),移是移的位左乘,右除
        >>(右移)
        0000 0000 0
        0000 0001 1
        0000 0010 2
        0000 0011 3
        0000 0100 4
        0000 1000 8
        0001 0000 16
         */
        System.out.println(2 << 3);
    }
}
public class Demo07 {
    public static void main(String[] args) {
        //偷懒用法
        int a = 10;
        int b = 20;
        a += b;// a = a + b
        a -= b;// a = a - b
        System.out.println(a);
        //字符串连接符+ String类型
        System.out.println(""+a+b);//1020
        System.out.println(a+b+"");//30
        //区别在于一个在前一个在后,字符串在前后面就是拼接,字符串在后依旧进行运算
    }
}
public class Demo8 {
    public static void main(String[] args) {
        //三元运算
        // x ? y : z,如果 x == true,结果为y否则为z
        int score = 40;
        String type = score < 60 ? "不及格":"及格";
        System.out.println(type);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值