• JAVA基础


    Java基础

    注释

    public class Hello {
        public static void main(String[] args) {
            //单行注释
            
            
            
            /*
            多行注释
             */
            
            
            
            /**
             * 文档注释
             */
            System.out.println("Hello,world");
        }
    }
    

    标识符和关键字

    image-20201115092911578

    • 所有标识符都应该以字母,美元符、下划线开始

    • 首字母之后可以是字母、美元符、下划线或者数字的任何字符组合

    • 不能使用关键字作为变量名或方法名

    • 标识符是大小写敏感的

    • 不建议用中文和拼音

    数据类型

    基本类型

    public class Demo2{
        public static void main(String[] args) {
            //八大基本数据类型
    
            //整数
            int num1=10;
            byte num2=20;
            short num3=30;
            long num4=39L;
    
            //小数
            float num5=39.1F;
            double num6=3.1415926;
    
            //字符
            char name='中';
            //字符串中String不是关键字,而是类
    
            //布尔值
            boolean flag = true;
            boolean flag1 = false;
        }
    }
    

    引用类型

    类、接口、数组

    数据类型扩展

    import java.sql.SQLOutput;
    
    public class Demo3 {
        public static void main(String[] args) {
            //整数拓展:   进制  二进制0b  十进制  八进制0  十六进制0x
    
            int i = 10;
            int i2 = 010;
            int i3 = 0x10;
    
            System.out.println(i);
            System.out.println(i2);
            System.out.println(i3);
            System.out.println("===========================================================");
            //============================================================
            //浮点数拓展
            //============================================================
            //float    有限   离散  舍入误差  大约  接近但不等于
            //double
            //最好避免使用浮点数进行比较
    
    
            float f = 0.1f;//0.1
            double d = 1.0 / 10;//0.1
            System.out.println(f == d);//false
    
            float d1 = 23232323232323322f;
            float d2 = d1 + 1;
    
            System.out.println(d1 == d2);//true
            //============================================================
            //字符拓展
            //============================================================
            System.out.println("===========================================================");
    
            char c1 = 'a';
            char c2 = '中';
            System.out.println(c1);
            System.out.println((int) c1);
            System.out.println(c2);
            System.out.println((int) c2);
    
            //所有的字符本质还是数字
            //编码 Unicode 表:(97=a 65=A) 2字节 0-65536
            //U0000 UFFFF
    
            char c3 = 'u0061';
            System.out.println(c3);//a
            System.out.println("===========================================================");
    
            //转义字符
            //	 制表符
            //
     换行
    
            System.out.println("Hello
    World");
            System.out.println("===========================================================");
    
            //
            String sa = new String("hello world");
            String sb = new String("hello world");
            System.out.println(sa == sb);
    
            String sc = "hello world";
            String sd = "hello world";
            System.out.println(sc == sd);
            //对象  从内存分析
    
            /**
             * 布尔值扩展
             *         boolean flag = ture;
             *         if (flag == ture) {}//新手
             *         if (flag) {}//老手
             *         Less is More!  代码要精简易读
             */
    
        }
    }
    

    类型转换

    Java是强类型语言,运算时的时候需要类型转换。

    image-20201116015842639

    运算中,不同类型的数据先转换为同一类型,然后进行运算

    public class Demo4 {
        public static void main(String[] args) {
            int i = 128;
            byte b = (byte)i;
    
            System.out.println(i);
            System.out.println(b);
    
            int a = 128;
            float c = i;
            System.out.println(a);
            System.out.println(c);
            //强制类型转换  由高到低
            //自动类型转换  由低到高
            /*
            注意点:
            1.不能对布尔值进行转换
            2.不能把数据类型转换为不相干的类型
            3.在把高容量转换到低容量的时候,进行强制转换
            4.转换的时候可能存在内存溢出,或者精度问题!
             */
    
            System.out.println("===============");
            System.out.println((int)23.7);//23
            System.out.println((int)-42.32f);//-42
    
            System.out.println("===============");
            char d = 'a';
            int e = d+1;
            System.out.println(e);
            System.out.println((char)e);
        }
    }
    
    public class Demo6 {
        public static void main(String[] args) {
            //操作比较大的数的时候,注意溢出问题
            //JDK7新特性,数字之间可以用下划线分割
            int money = 10_0000_0000;
            int years = 20;
            int total = money*years;//-147836480,计算的时候溢出了
            long total2 = money*years;//默认是int,转换之前已经存在问题了
            long total3 = money*((long)years);//先把一个数转换为Long
            System.out.println(total3);
        }
    }
    

    变量、常量、作用域

    变量

    变量:可以变化的量!

    Java是一种强类型语言,每个变量都必须声明其类型。

    Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。

    type varName [=value][{,varName[=value]}];
    //数据类型 变量名 = 值:可以使用逗号隔开来声明多个同类型变量。
    
    public class Demo5 {
        public static void main(String[] args) {
            /*
            为了程序的可读性一般不用这种
            int a,b,c;
            int a = 1;b=2;c=3;
             */
            int a = 1;
            int b = 2;
            int c = 3;
            String name = "xiaowei";
            char x = 'Z';
            double pi = 3.14;
        }
    }
    
    • 注意事项:

    • 每个变量都有类型,类型可以是基本类型,也可以是引用类型。

    • 变量名必须是合法的标识符。

    • 变量声明是一条完整的语句,因此每一个人声明都必须已分号结束。

    变量作用域

    类变量

    实例变量

    局部变量

    public class Demo8 {
    
        //类变量  static
        static double salary = 2500;
    
        //属性:变量
    
        /*
        实例变量:从属于对象:如果不自行初始化,这个类型的默认值为 0   0.0
        布尔值:默认值是false
        除了基本类型:其余的默认值都是null
         */
        String name;
        int age;
    
        //main方法
        public static void main(String[] args) {
    
            //局部变量:必须声明和初始化
            int i = 10;
            System.out.println(i);
    
            //变量类型 变量名字 = new Demo8();
            Demo8 demo8 = new Demo8();
            System.out.println(demo8.age);
            System.out.println(demo8.name);
    
            //类变量  static
            System.out.println(salary);
        }
    
        //其他方法
        public void add() {
    
        }
    }
    

    常量

    常量:初始化后不能再改变值!不会变动的值。

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

    //final 常量名 = 值;
    final double PI = 3.14;
    
    • 常量名一般使用大写字符。
    public class Demo9 {
        static final double PI = 3.14;
    
        public static void main(String[] args) {
            System.out.println(PI);
        }
    }
    

    变量命名规范

    image-20201116151048502

    运算符

    image-20201116151658496

    public class Demo03 {
        public static void main(String[] args) {
            //关系运算符返回的结果:false,ture
            int a =10;
            int b = 20;
            int c = 21;
            //取余,模运算
            System.out.println(c%a);//  21/10=2......1
    
            System.out.println(a>b);
            System.out.println(a<b);
            System.out.println(a==b);
            System.out.println(a!=b);
        }
    }
    

    算术运算符

    +-*/

    package operator;
    
    public class Demo01 {
        public static void main(String[] args) {
            //二元运算符
            int a = 10;
            int b = 20;
            int c = 25;
            int d = 25;
    
            System.out.println(a+b);
            System.out.println(a-b);
            System.out.println(a*b);
            System.out.println(a/(double)b);
            System.out.println("========================================");
    
            a+=b;//a = a+b
            a-=b;//a = a-b
    
            System.out.println(a);
        }
    }
    

    关系运算符

    package operator;
    
    public class Demo03 {
        public static void main(String[] args) {
            //关系运算符返回的结果:false,ture
            int a =20;
            int b = 10;
            int c = 21;
            //取余,模运算
            System.out.println(c%a);//  21/10=2......1
    
            System.out.println(a>b);
            System.out.println(a<b);
            System.out.println(a==b);
            System.out.println(a!=b);
        }
    }
    

    自增 自减 工具类

    package operator;
    
    public class Demo04 {
        public static void main(String[] args) {
            //++ --  自增,自减    一元运算符
            int a = 3;
    
            int b = a++;//执行完这行代码后,先给b赋值,再自增
            //a = a+1;
            System.out.println(a);
    
            //a = a+1
            int c = ++a;//执行完这行代码前,先自增,再给b赋值
    
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
    
            //幂运算 2^3    2*2*2 = 8 很多运算,我们会使用一些工具类来操作
            double pow = Math.pow(3,2);
            System.out.println(pow);
        }
    }
    
    

    逻辑运算符

    package operator;
    
    public class Demo05 {
        public static void main(String[] args) {
            //与(and)  或(or)  非(取反)
            boolean a = true;
            boolean b = false;
    
            System.out.println("a && b:"+(a && b));//逻辑与运算:两个变量都为真,结果才为true
            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);
            System.out.println(c);
        }
    }
    

    位运算符

    package operator;
    
    public class Demo06 {
        public static void main(String[] args) {
            /*
            A = 0011 1100
            B = 0000 1101
    
            A&B = 0000 1100
            A|B = 0011 1101
            A^B = 0011 0001  异或
            ~B = 1111 0010
    
            2*8 = 16  2*2*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);
    
        }
    }
    

    三元运算符

    package operator;
    //三元运算符
    public class Demo07 {
        public static void main(String[] args) {
            //x ? y : z
            //如果x == true,则结果为y,否则结果为z
    
            int score = 50;
            String type = score<60 ? "不及格" : "及格";//必须掌握
            System.out.println(type);
        }
    }
    

    包机制

    • 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。

    • 包语句的语法格式为:

      package pkg1[.pkg2[.pkg3...]];

    • 一般利用公司域名倒置作为包名;

    • 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用“import"语句可完成此功能

    import package1[.package2...].(className|*);
    

    JAVADOC

    Javadoc命令是用来生成自己API文档的

    • 参数信息

    • @author作者名

    • @version版本号

    • @since指明需要最早使用的jdk版本

    • @param参数名

    • @return返回值情况

    • @throws异常抛出情况

    package com.kikikikikiku.base;
    
    /**
     * @author kikikikikiku
     * @version 1.0
     * @since 1.8
     */
    
    public class Demo10 {
        String name;
    
        /**
         * @author kikikikikiku
         * @param name
         * @return
         * @throws Exception
         */
        public String test(String name) throws Exception{
            return name;
        }
    
    }
    

    image-20201116220354645

    //-encoding UTF-8 编码为UTF-8

    //-charset UTF-8 字符也为UTF-8

  • 相关阅读:
    ML、DL及RL介绍和区别
    ros工作空间中文件夹结构
    C算法,已知结构体成员地址,如何得出结构体的基地址?
    循*迹小车轨道识别算法
    树莓派ssh卡顿之双机互联解决
    楼宇对讲自动开门的设计与实现
    JavaJSONArray jsonArray = JSONArray.fromObject()使用
    Java虚拟机详解JVM常见问题总结
    git 修改commit信息 Dawa
    通过commit_id找回文件 Dawa
  • 原文地址:https://www.cnblogs.com/kikikikikiku/p/13983368.html
Copyright © 2020-2023  润新知