zoukankan      html  css  js  c++  java
  • 第三章 Java变量

    第三章 变量

    3.1 为什么需要变量

    3.1.1 一个程序就是一个世界

    3.1.2 变量是程序的基本组成单位

    image-20210925095858699

    3.1.3 简单原理图

    image-20210925100113571

    3.2 变(变化)量(值)的介绍

    3.2.1 概念

    • 变量相当于内存中一个数据存储空间的表示,你可以把变量看做是一个房间的门牌号,通过门牌号我们可以找到房间,而通过变量名可以访问到变量(值)。

    3.2.2变量使用的基本步骤

    • 声明变量 int a; 2) 赋值 a = 60; //应该这么说: 把 60 赋给 a 使用 System.out.println(a);
    • 也可以一步到位[int a = 60; 通常我们是一步完成]

    3.3 变量快速入门

    • 变量使用入门案例 看演示并对代码进行说明:演示记录人的信息的代码
    public class Var02 {
        public static void main(String[] args) {
            //记录一个人的信息
            int age = 20;
            double score = 89.5;
            char gender = '男';
            String name = "刘";
            //输出信息
            System.out.println(name);
            System.out.println(gender);
            System.out.println(age);
            System.out.println("成绩:" + score);
        }
    }
    

    3.4 变量使用注意事项

    image-20210925101143226

    public class VarDetail {
        public static void main(String[] args) {
            //变量必须先声明,后使用
            int a = 10;
            System.out.println(a);//10
            //该区域的数据值可以在同一类型范围内不断变化的
            a = 26;
            System.out.println(a);//26
    
            //变量在同一个作用域内不能重名
            //int a = 77;//错误
    
            //java的整型常量(具体值)默认为int型,声明long型常量须后加“L"或“l”
            int n1 = 1;//4个字节
            //int n2 = 1l;//错误
            long n3 = 1l;
        }
    }
    class Dog{
        public static void main(String[] args) {
            int a = 666;//对
        }
    }
    

    3.5 程序中 + 号的使用

    image-20210925101709699

    3.6 数据类型

    image-20210925101931521

    • java 数据类型分为两大类基本数据类型, 引用类型
    • 基本数据类型有 8 种数值型 [byte , short , int , long , float ,double] char , boolean]
    • 引用类型 [类,接口, 数组]

    3.7 整数类型

    3.7.1基本介绍

    Java 的整数类型就是用于存放整数值的,比如 12 , 30, 3456 等等

    3.7.2案例演示

    byte n1 = 10;

    short n2 = 10;

    int n3 = 10;//4 个字节

    long n4 = 10; //8 个字节

    3.7.3整型的类型

    image-20210925102835006

    3.7.4整型的使用细节 IntDetail.java

    image-20210925103152792

    public class IntDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
        //java的整型常量(具体值)默认为int型,声明long型常量须后加“L"或“l”
            int n1 = 1;//4个字节
            //int n2 = 1l;//错误
            long n3 = 1l;
      }  
    }
    

    3.8 浮点类型

    3.8.1 基本介绍

    • Java 的浮点类型可以表示一个小数,比如 123.4 ,7.8 ,0.12 等等

    3.8.2 案例演示

    3.8.3 浮点型的分类

    image-20210925103951610

    3.8.4 说明一下

    • 关于浮点数在机器中存放形式的简单说明, 浮点数=符号位+指数位+尾数位
    • 尾数部分可能丢失,造成精度损失(小数都是近似值)

    3.8.5 浮点型使用细节 FloatDetail.java

    image-20210925104844512

    public class FloatDetail {
        public static void main(String[] args) {
            //JAVA的浮点型常量(具体值)默认为double型,声明float常量,须后加“f”或“F”
            //float num1 = 1.1;//错误
            float num2 = 1.1F;//对
            double num3 = 1.1;//对
            double num4 = 1.1f;//对
            //通常情况下,应该使用double型,因为它比float型更精确
            double num5 = 3.1415926535;
            float num6 = 3.1415926535f;
            System.out.println(num5);
            System.out.println(num6);
    
            //浮点数使用陷阱:2.7 和 8.1/3
            double num7 = 2.7;
            double num8 = 8.1/3;
            System.out.println(num7);
            System.out.println(num8);//无限接近2.7,而不是2.7
    
            //得到一个重要的使用点:当我们对运算结果是小数的进行相等判断是,要小心
            //应该是以两个数的差值的绝对值,在某个精度范围类判断
            if( num7 == num8){
                System.out.println("相等");
            }
        }
    }
    

    3.9 字符类型(char)

    3.9.1 基本介绍

    字符类型可以表示单个字符,字符类型是 char,char 是两个字节(可以存放汉字),多个字符我们用字符串 String(我们后面详细讲解 String)

    3.9.2 案例演示 Char01.java

    char c1 = 'a'; 
    
    char c2 = '	';
    
    char c3 = '韩'; 
    
    char c4 = 97;
    

    3.9.3 字符类型使用细节

    image-20210925110613887

    public class CharDetail {
        public static void main(String[] args) {
    
            //在java中,char的本质是一个整数,在输出时,是unicode码对应的字符
            //要输出对应的数字,可以(int)字符
            char c1 = 97;
            System.out.println(c1);//a
    
            char c2 = 'a';//输出a对那个的数字
            System.out.println((int)c2);
            char c3 = '刘';
            System.out.println((int)c3);
    
            //char类型是可以进行运算的,相当于一个整数,因为它都对应有Unicode码
            System.out.println('a' + 10);//107
    
            //课堂测试
            char c5 = 'b'+ 1;
            System.out.println((int)c5);//99
            System.out.println(c5);//c,99对应的字符,编码表是c
        }
    }
    
    

    image-20210925113625257

    3.10 布尔类型:Boolean

    image-20210925113746653

    public class Boolean01 {
        public static void main(String[] args) {
            //演示判断成绩是否通过的案例
            //定义一个布尔变量
            boolean isPass = true;
            if(isPass == true) {
                System.out.println("考试通过,恭喜");
            }else{
                System.out.println("考试没通过,下次努力");
            }
        }
    }
    
    

    3.11基本数据类型

    3.11.1 自动类型转换

    image-20210925142450480

    3.11.2 自动类型转换注意和细节

    image-20210925142525380

    //自动类型转换细节
    public class AutoConvertDetail {
        public static void main(String[] args) {
    //        细节一:有多种类型的数据混合运算时,系统首先自动将所有数据转换成容量最大的那种数据类型
    //        然后在进行计算
            int n1 = 10;
    //        float d1 = n1 + 1.1;//×,n1 + 1.1 => 结果类型是double
    //        float d1 = n1 + 1.1f;//√,n1 + 1.1 =>结果类型是float
              double d1= n1 + 1.1;//√,n1 + 1.1 => 结果类型是double
    
    //        细节二:当我们把精度(容量)大的数据据类型赋值给精度(容量)小的数据类型时,就会报错,繁殖会进行自动类型转换
    //        int n2 = 1.1//×,double => int
    
    //        细节三:(byte , short)和char之间不会相互自动转换
    //        当把具体数赋值给byte时,(1)先判断这个数是否在byte这个范围内,如果是,就可以用
            byte b1 = 10;//√ -128~127
            int n2 = 1 ; //n2 此时时int类型
    //        byte b2 = b2;//×,原因:如果是变量赋值,判断赋值
    
    //        char c1 = b1;//×,原因:byte不能自动转换成char
    
    //        细节四:byte short char 他们三者可以计算,在计算时首先转换为int类型
            byte b2 = 1;
            byte b3 = 2;
            short s1 = 1;
    //        short s2 = b2 + s1 ;//× , b2 + s1 =>int
    //        int s2 = b2 + s1 ;//√ , b2 + s1 =>int
    //        byte b4 = b2 + b3;//× , b2 + b3 =>int
    
    //        细节五:boolean 不参与转换
            boolean pass = true;
    //        int num10 = pass;//× , boolean 不参与类型的自动转换
    
    //        细节六:自动提升原则:表达式结果的类型自动提升为,操作数中最大的类型
            byte b4 = 1 ;
            short s3 = 100;
            int num11 = 1;
            double num14 = 1.1;
            double num15 = b4 + s3 + num11 +num14;
        }
    }
    

    3.11.3 强制类型转换

    • 介绍:自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符 ( )但可能造成精度降低或溢出,格外要注意
    public class ForceConvert {
        public static void main(String[] args) {
    
            //演示强制类型转换
            int  n1 = (int)1.9;
            System.out.println("n1=" + n1);//1,造成精度损失
    
            int n2 = 2000;
            byte b1 = (byte)n2;
            System.out.println("b1=" + b1);//造成数据溢出
        }
    }
    

    3.11.4 强制类型转换细节说明

    image-20210925144002991

    public class ForceConvertDetail {
        public static void main(String[] args) {
    
    //        演示强制类型转换
    //        强制符号只针对与最近的操作数有效,往往会使用小括号提升优先级
    //        int x = (int)10*3.5+6*1.5;//编译错误,结果类型是double,编译不过去
            int y = (int)(10*3.5+6*1.5);//对
            System.out.println(y);//44
            
            char c1 = 100;//ok
            int m = 100;//ok
    //        char c2 = m;//❌
            char c3 = (char)m;//ok
            System.out.println(c3);//100对应的字符,d字符
        }
    }
    

    3.11.5 基本数据类型转换-练习题

    image-20210925145728328

    3.12 基本数据类型和 String 类型的转换

    3.12.1 介绍和使用

    image-20210926192038565

    public class StringToBasic {
        public static void main(String[] args) {
    //        基本数据类型-->String
            int n1 = 2;
            float f1 = 1.1F;
            double d1 = 4.5;
            boolean b1 = true;
            String s1 = n1 + "";
            String s2 = f1 + "";
            String s3 = d1 + "";
            String s4 = b1 + "";
            System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
    
    //        String-->对应的基本数据类型
            String s5 = "123";
    //        会在讲对象和方法的时候详细讲解
    //        解读:使用 基本数据类型对应的包装类的相应方法,得到基本数据类型
            int num1 = Integer.parseInt(s5);
            double num2 = Double.parseDouble(s5);
            float num3 = Float.parseFloat(s5);
            long num4 = Long.parseLong(s5);
            byte num5 = Byte.parseByte(s5);
            boolean b = Boolean.parseBoolean("true");
            short num6 = Short.parseShort(s5);
    
            System.out.println(num1);//123
            System.out.println(num2);//123.0
            System.out.println(num3);//123.0
            System.out.println(num4);//123
            System.out.println(num5);//123
            System.out.println(num6);//123
            System.out.println(b);//true
            
        }
    }
    

    3.12.2 注意事项

    • 在将 String 类型转成 基本数据类型时,比如我们可以把 "123" , 转成一 个整数,但是不能把 "hello"
    • 如果格式不正确,就会抛出异常,程序就会终止,这个问题在异常处理章节中,会处理
    public class StringToBasicDetail {
        public static void main(String[] args) {
    
            String str = "hello";
    //        转成int
            int n1 = Integer.parseInt(str);
            System.out.println(n1);//异常
        }
    }
    ===============================================
        Exception in thread "main" java.lang.NumberFormatException: For input string: "hello"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:580)
    	at java.lang.Integer.parseInt(Integer.java:615)
    	at StringToBasicDetail.main(StringToBasicDetail.java:6)
    

    3.13 本章作业

    image-20210925163213532

    public class Homework01 {
        public static void main(String[] args) {
            int n1 = 13;
            int n2 = 17;
            int n3 = n1 + n2;
            System.out.println("n3=" + n3);//30
            int n4 = 38;
            int n5 = n4 - n3;
            System.out.println("n5=" + n5);//8
        }
    }
    
    =============================================
        
    public class Homework02 {
        public static void main(String[] args) {
            char c1 = '
    ';
            char c2 = '	';
            char c3 = '
    ';
            char c4 = '\';
            char c5 = '1';
            char c6 = '2';
            char c7 = '3';
            System.out.println("c1=" + c1);//换行
            System.out.println("c2=" + c2);//制表位
            System.out.println("c3=" + c3);//回车
            System.out.println("c4=" + c4);//输出
            System.out.println("c5=" + c5);//1
            System.out.println("c6=" + c6);//2
            System.out.println("c7=" + c7);//3
        }
    }
    
    =============================================
        
    public class Homework03 {
        public static void main(String[] args) {
            String str1 = "完美世界";
            String str2 = "死亡笔记";
            System.out.println(str1 + str2);//完美世界死亡笔记
            char c1 = '男';
            char c2 = '女';
            System.out.println(c1 + c2 );//52906
            int n1 = 50;
            int n2 = 45;
            System.out.println(n1 + n2);//95
        }
    }
    
    ===============================================
       
    public class Homework04 {
        public static void main(String[] args) {
    
            String name = "张三";
            int age = 20;
            int grade = 85;
            char sexual = '男';
            String hobby = "打篮球";
    
            System.out.println("姓名	年龄	成绩	性别	爱好
    " +
                    name + "	" + age +  "	     " + grade + "   	" + sexual + "  	" + hobby);
    //        输出结果:姓名	年龄	成绩	性别	爱好
    //                  张三	 20	   85    男   打篮球
    
        }
    }
    
    
  • 相关阅读:
    Spring Boot 缓存技术:Spring Boot
    Java基础之Iterable接口
    使用sqlyog连接 Mysql 出现1251错误
    IDEA更改主题插件——Material Theme UI详解
    免安装版的Mysql
    使用Nexus搭建Maven私服
    Spring之注解注入bean
    Idea springboot 配置热部署
    Spring Boot 异常处理与单元测试
    Ubuntu20.04在线安装VMware-Tools
  • 原文地址:https://www.cnblogs.com/joker-bea/p/15339669.html
Copyright © 2011-2022 走看看