WGL的Day03

数据类型

强类型语言

  • 要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用

弱类型语言

  • 与强类型语言相反

Java的数据类型分为两大类

  • 基本类型(primitive type)
  • 引用类型(reference type) : 类、接口、数组

在这里插入图片描述

字节

  • 位 (bit) : 是计算机内部数据储存的最小单位 , 11001100是一个八位二进制数

  • 字节 (byte) : 是计算机中数据处理的基本单位 , 习惯上用大写B来表示

  • 1B (byte,字节) = 8bit(位)

  • 字符 : 是指计算机中使用的字母、数字、字和符号

  • 1bit表示1位

  • 1Byte表示一个字节 1B = 8b

  • 1024B = 1KB

  • 1024KB = 1MB

  • 1024MB = 1GB

  • 1024GB = 1TB

计算机的32位和64位的区别

  • 寻址能力

32位只支持4GB内存

64位理论为128GB或以上

数据类型举例

public class day03_2 {
    public static void main(String[] args) {
        //整数拓展: 进制  二进制0b  十进制0  十六进制0x
        int i1 = 10;
        int i2 = 010;  //八进制
        int i3 = 0x10; //十六进制
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        //------------------------------
        //浮点数
        //------------------------------
        //银行业务用BigDecimal 数学工具类
        //float   有限 离散 误差 大约
        //double
        //最好使用完全浮点数

        float f = 0.1f;  //0.1
        double d = 1.0 / 10; //0.1
        System.out.println(f == d);//false, 为什么是错的?

        float d1 = 23232323232323f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);//true

        //------------------------------
        //字符拓展
        //------------------------------
        char c1 = 'a';//'a'为97
        char c2 = '中';//'中'为20013

        System.out.println(c1);
        System.out.println((int)c1);//---
        System.out.println(c2);
        System.out.println((int)c2);//强制类型转换
        //所有的字符本质还是数字
        //编码 Unicode表 2字节  65536  Excel最大为2ˇ16=65536

        //范围 : U0000-UFFFF
        char c3 = '\u0061';
        System.out.println(c3);

        //转义字符
        // \r 回车 \n 换行 \f 走纸换页 \t 横向跳格 \b 退格

        System.out.println("Hello\tWorld!");
        System.out.println("Hello\nWorld!");

        System.out.println("=============================");


        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);//false
        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);//true
        //得从内存内分析
        
        //布尔值
        boolean flag = true;
        if (flag==true){}//新手
        if (flag){}      //老手
            
        

    }
}

类型转换

  • 由于Java是强类型语言,进行运算时,时常需要类型转换

在这里插入图片描述

public class day03_3 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte)i;//(byte)i 强制类型转换 高--低
        //b=-128 内存溢出
        double c = i;   // i = 128 低--高 自动类型转换
        /*1.不能对布尔值进行转换
          2.不能把对象类型转换为不相干的类型
          3.在吧高容量转换到低容量的时候,强制转换
          4.转换时候容量问题可能出现内存溢出,精度出现问题!
        */
        System.out.println("------------------------------");
        System.out.println((int)23.7);  //23
        System.out.println((int)-45.89f); //-45
        System.out.println("------------------------------");
        char a1 = 'a';
        int a2 = a1+1;
        System.out.println(a2);
        System.out.println((char)a2);
        System.out.println("------------------------------");
        //JDK7特性,操作比较大的数时可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);//1000000000
        int years = 20;
        int total = money * years;
        long total2 = ((long)money * years);//转换为long
        System.out.println(total);
        System.out.println(total2);//超过 int 的最大范围,内存溢出!
       

    }
}

变量,常量

变量

  • 可以变化的量
  • Java是强类型语言,每个变量都必须声明其类型
  • Java变量是程序中最基本的存储单元,包含要素有变量名,变量类型和作用域

作用域

  • 类变量
  • 实例变量
  • 局部变量
public class day03_4 {
    
    static int allClicks = 0;  //类变量
    
    String str = "Hello world"; //实例变量
    
    public void metho(){
        int i = 0;         //局部变量
    }

}

常量

  • 常量(Constant) : 初始化后不能再改变的值!不会变动的值

  • 所谓常量可以理解成一种特殊的变量,他的值被设定后,在程序运行中不允许被改变

    final 常量名 = 值

变量的命名规范

  • 所有变量、方法、类名 : 见名见意
  • 类成员变量 : 首字母小写和驼峰原则 : monthSalary ,第一个字母小写,后边的单词首字母大写
  • 局部变量: 首字母小写和驼峰原则
  • 常量 : 大写字母和下划线 : MAX_VALUE
  • 类名 : 首字母大写和驼峰原则 : Man,goodMan
  • 方法名 : 首字母小写和驼峰原则 : run(),runRun()

运算符

  • 算术运算符 : + , - , * , / , % , ++ , –

  • 赋值运算符 : =

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

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

  • 位运算符 : & , | , ^ , ~ , >> , << , >>>

  • 条件运算符 : ? , :

  • 扩展运算符 : += , -= , *= , /=

    关系运算符
    public class day03_6 {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
            int c = 25;
            int d = 30;
            System.out.println(a+b);
            System.out.println(a-b);
            System.out.println(a*b);
            System.out.println((double)a/b); //注意数值类型
    
            long e = 12121212121212L;
            int f = 123;
            short g = 10;
            byte h = 8;
            System.out.println(e+f+g+h); //long 有一个long,结果就为long double同理
            System.out.println(f+g+h);   //int
            System.out.println(g+h);     //int
    
    
            System.out.println(a>b);//false
            System.out.println(a<b);//true
            System.out.println(a==b);//false
            System.out.println(a!=b);//true
            System.out.println(c%b);//25/20 取余
    
            System.out.println(a++);//10 a++在此行的a仍为10,下一行自加1
            System.out.println(a);  //11
            System.out.println(++a);//12 ++a在本行直接自加1
        }
    }
    
    逻辑运算符
    public class day03_7 {
        public static void main(String[] args) {
            // 与 (and)(or)(取反)
            boolean a = true;
            boolean b = false;
    
            System.out.println("a && b"+(a&&b));//逻辑与运算 : 两个为真,则为真
            System.out.println("a || b"+(a||b)); //两个结果一个为真,则为true
            System.out.println("!(a && b)"+!(a&&b));//如果是真则是假,如果是假则为真
    
            //短路运算
            int c = 5;
            boolean d = (c<4)&&(c++<4); //前为错,后不看
            System.out.println(d);      //d = false
            System.out.println(c);      //c = 5
        }
    
    }
    
    
    位运算符
    public class day03_8 {
        public static void main(String[] args) {
    
    
        /*
        A = 0011 1100
        B = 0000 1101
    
        A&B 0000 1100 全是1则为1
        A|B 0011 1101 全是0则为0
        A^B 0011 0010 相同为1,不同为0
        ~B  1111 0010 B取反
        ~A  1100 0011 A取反
    
        2*8 = 16  2*2*2*2
    
        <<(左移)  >>(右移)
        */
            System.out.println(2<<3);
            /*
            0000 0000    0
            0000 0001    1
            0000 0010    2
            0000 0011    3(左移三位) = 16  右移减小
            0000 0100    4
            0000 1000    8
            0001 0000    16
            与计算机内存交流,效率高!
             */
    
        }
    }
    
    public class day03_9 {
        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+b);   //30
            System.out.println(""+a+b);//1020,空字符串+int类型,结果为字符串
            System.out.println(a+b+"");//30,int型在前边,结果为30
    
    三元运算符
    
            // x ? y : z
            //如果x==true , 结果为y ,否则为z
    条件运算符
            int score = 50;
            String type = score < 60 ?"不及格":"及格";//判断
            // if
            System.out.println(type);
        }
    }
    
    
    

020,空字符串+int类型,结果为字符串
System.out.println(a+b+"");//30,int型在前边,结果为30

三元运算符

      // x ? y : z
      //如果x==true , 结果为y ,否则为z

条件运算符
int score = 50;
String type = score < 60 ?“不及格”:“及格”;//判断
// if
System.out.println(type);
}
}




 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值