• Java入门知识点


    Java入门知识点

     

    Java源代码的流程

      Java程序由.java文件生成,通过JVM进行编译得到字节文件.class

    class HelloWorld {
        public static void main(String[] args){
            System.out.println("Hello World!");
        }
    }

    关于class有如下几点规则:

    1. 文件的名字必须和class的名字一致(public级别的class名字)。
    2. 文件必须只包含一个public访问基本的class(可以包含多个非public级别的class)。

    命名规则:

    1. 包(其实就是文件夹,用于解决相同类名问题):包名要求全部小写,一般是公司的域名倒着写 com.heima.包的作用
    2. 类或者接口:如果是一个单词,要求首字母大写,如果是多个单词要求每个单词首字母大写(驼峰命名)
    3. 方法和变量: 如果是一个单词,每个字母都小写,如果是多个单词,从第二个单词开始首字母大写
    4. 常量: 如果是一个单词,所有字母大写,如果是多个单词也是所有字母大写,但是用_分开 MAX MAX_VALUE

    Java语言优势:

      完全面向对象,安全可靠,跨平台性

    (补充)有关于dos命令行方式:

      dir  md  rd  cd  cd..  cd/  del  exit

    标识符

      由英文字母,数字,_ ,$组成,其中数字不能开头

    数据类型(11)

     8种原子类型(基本数据类型)

    1. 整数类型:byte(1)、short(2)、int(4)和long(8)。
    2. 小数类型:float(4)和double(8)。
    3. 字符类型:char(2)。
    4. 布尔类型:bool。

     3种引用数据类型

      interface、class和array

     小数类型的常量默认是double类型,声明float类型的常量需要使用F作为后缀。

    运算符

    1. 算术运算符:+、-、*、/ 和 %,两个整数相除,结果还是整数。
    2. 赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、~=、^=、<<=、>>= 、 >>>=、++ 和 --。
    3. 比较运算符:==、!=、<、<=、> 和 >=。
    4. 逻辑运算符:&&、|| 和 !。
    5. 位运算符:&、|、~、^、<<、>> 和 >>>。

    注意:

    1. 字符串数据和任何数据使用+都是相连接,最终都会变成字符串。//System.out.println(“5+5=”+5+5);//5+5=55
    2. 出现负数取余只看左边。//System.out.println(-1%5);
    3. s+=5;//只一次赋值运算,进行自动转换
    4. &和&&的区别 [&:无论左边是true是false,右边都运算 &&:当左边为false时,右边不运算。同理,|和||的区别一致]

    键盘录入

    复制代码
    import java.util.Scanner;
    class Demo_Scanner {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个整数:");
            int x=sc.nextInt();
            System.out.println(x);
        }
    }
    复制代码

    控制结构

    1. 条件:if-else if-else、switch-case-default和三元运算符(?:)。
    2. 循环:while、do-while、for和foreach。
    3. Labeled block。

    注意:

    1. if else结构简写格式:变量=(条件表达式)?表达式1:表达式2;
    2. switch语句中被选择类型的值只有四种:byte short int char;
    3. switch语句里case和default定义位置随机,执行顺序从case开始。
    4. 语句嵌套形式:循环嵌套(大圈套小圈)

      //尖朝上,可改变条件,让条件随着外循环变化
      //尖朝下,可初始化值,让初始化随着外循环变化

      5. break与continue的区别

      break:应用范围:选择结构和循环体 跳出所在当前循环
      continue:只能用于循环结构 结束本次循环,继续下一次循环

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            task: {
                int age = 25;
    
                System.out.println("start");
    
                if (age < 30) {
                    break task;
                }
    
                System.out.println("end");
            }
        }
    }
    复制代码

    字符串

      String是拥有“值语义”的引用类型,字符串常量实现了“享元模式”,equals会按照内容进行比较,==按照地址比较。

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            String x = "小咕噜";
            String y = new String("小咕噜");
            
            System.out.println(x.equals(y)); // true
            System.out.println(x == y); // false
        }
    
    }
    复制代码

     为了高效的修改字符串Java引入了StringBuffer

    复制代码
    {
                StringBuffer sb = 
                        new StringBuffer()
                        .append("小")
                        .append("咕")
                        .append("噜");
                
                System.out.println(sb.toString());
            }
    复制代码

    数组

     声明语法

      DataType[] name 或 DataType name[]。

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            {
                String[] strs = { "小", "咕", "噜" };
    
                for (String item : strs) {
                    System.out.print(item);
                }
            }
        }
    
    }
    复制代码

     多维数组

      只有不等长多维数组DataType[][],没有DataType[xxx, xxx]。

    方法

      Java中所有的赋值和方法调用都是“按值“处理的,引用类型的值是对象的地址,原始类型的值是其自身。

      Java支持变长方法参数。

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            print("小咕噜", "大咕噜");
            print(new String[] { "小咕噜", "大咕噜" });
        }
    
        private static void print(String... args) {
            for (String item : args) {
                System.out.println(item);
            }
        }
    }
    复制代码

     ① 变量在内存中的存储方式:  

    1. 成员变量作用于整个类中,存储于堆内存中,因为对象的存在,才在内存中存在,必有初始化值,可参与运算
    2. 局部变量作用于函数中或语句中,存在于栈内存中

      this的应用:

    1. 当定义类中功能时,该函数内部要调用该函数的对象 但凡本类功能内部使用了本类对象,都用this表示。
    2. 用于区分局部变量和成员变量重名的情况代表本类的对象,代表它所在函数所属对象的引用。
    3. 只能定义在构造函数的第一行。

     ③ 构造代码块: 给所有对象进行统一初始化,优先于构造函数执行

    注意:(内存机构:分区)
      1.栈内存:用于存储局部变量,当数据使用完,所占空间会自动释放
      2.堆内存:①.数组和对象,通过New建立的实例都存放在堆内存中
           ②.每一个实体都有内存地址值
           ③.实体中的变量都有默认初始化值
           ④.实体不再被使用,会在不确定的时间内被垃圾回收器回收
      3.方法区,本地方法区,寄存器

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            Point point = new Point(100);
    
            System.out.print(point);
        }
    }
    
    class Point {
        private int x = 0;
        private int y = 0;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public Point(int x) {
            this(x, x);
        }
    
        public String toString() {
            return "(x:" + this.x + ",y:" + this.y + ")";
        }
    }
    复制代码

    静态成员

      Java中类似静态构造方法的结构,称之为:静态初始化代码块,与之对应的是实例初始化代码块,见下例:

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            System.out.println(Point.getValue());
            System.out.println(new Point());
        }
    }
    
    class Point {
        private static int value = 0;
    
        public static int getValue() {
            return value;
        }
    
        static {
            value++;
        }
    
        static {
            value++;
        }
    
        private int x = 0;
        private int y = 0;
    
        {
            this.x = 10;
        }
    
        {
            this.y = 10;
        }
    
        public String toString() {
            return "(x:" + this.x + ",y:" + this.y + ")";
        }
    }
    复制代码

    注意:

    1. 静态方法只能访问静态成员 非静态方法既可以静态也可以访问非静态
    2. 静态方法中不可以定义this,super等关键字 因为静态优先于对象存在
    3. 主函数是静态的
    4. 类变量随着类的加载而存在于方法区中 实例变量随着对象的建立而存在于堆内存中

    继承

      继承使用 extends,抽象类和抽象方法使用abstract声明,向下转型使用 (ChildType)instance,判断是否是某个类型使用 instanceof,见下例:

    复制代码
    public class Program {
    
        public static void main(String[] args) {
            printAnimal(new Animal());
            printAnimal(new Dog());
        }
    
        private static void printAnimal(Animal animal) {
            if(animal instanceof Dog){
                System.out.println("I am a " + (Dog) animal);
            }
            else
            {
                System.out.println("I am an " + animal);
            }
        }
    }
    
    class Animal {
        public String toString() {
            return "Animal";
        }
    }
    
    class Dog extends Animal {
        public String toString() {
            return "Dog";
        }
    }
    复制代码

    重写

     Java中的重写规则比较灵活,具体如下:

    1. 除了 private 修饰之外的所有实例方法都可以重写,不需要显式的声明。
    2. 重写的方法为了显式的表达重写这一概念,使用 @Override进行注解。
    3. 重写的方法可以修改访问修饰符和返回类型,只要和父类的方法兼容(访问级别更高,返回类型更具体)。
    4. 可以使用final将某个方法标记为不可重写。
    5. 在构造方法中使用 super(xxx, xxx)调用父类构造方法,在常规实例方法中使用 super.method(xxx, xxx)调用父类方法。
    6. Java不支持覆盖(new)。
    复制代码
    public class Program {
    
        public static void main(String[] args) {
            Animal animal = new Animal();
            Animal dog = new Dog();
    
            animal.say();
            dog.say();
    
            animal.eat(animal);
            dog.eat(dog);
            
            System.out.println(animal.info());
            System.out.println(dog.info());
        }
    }
    
    class Animal {
        private String name = "Animal";
    
        protected void say() {
            System.out.println("Animal" + " " + this.name);
        }
    
        public void eat(Animal food) {
            System.out.println("Animal eat " + food);
        }
    
        public Object info() {
            return "Animal";
        }
        
        @Override
        public String toString() {
            return "Animal";
        }
    }
    
    class Dog extends Animal {
        private String name = "Dog";
    
        @Override
        public final void say() {
            System.out.println("Dog" + " " + this.name);
        }
    
        @Override
        public final void eat(Animal food) {
            super.eat(food);
            
            System.out.println("Dog eated");
        }
    
        @Override
        public final String info() {
            return "Dog";
        }
    
        @Override
        public final String toString() {
            return "Dog";
        }
    }
    复制代码

    Java支持三种导入语法:

    1. 导入类型:import xxx.xxx.xxxClass。
    2. 导入包:import xxx.xxx.xxx.*。
    3. 导入静态成员:import static xxx.xxx.*。
    复制代码
    import static util.Helper.*;
    
    public class Program {
    
        public static void main(String[] args) {
            puts("小咕噜");
        }
    }
    复制代码

    访问级别

      Java支持四种访问级别:public、private、protected 和 default(默认),类型和接口只能使用public 和 default,成员和嵌套类型可以使用所有,下面简单的解释一下 protected 和 default。

    • protected 修饰过的成员只能被自己、子类和同一个包里的(不包括子包)其他类型访问。
    • default 修改过的类型或成员只能被自己和同一个包里的(不包括子包)其他类型访问。

    嵌套类

    Java支持如下几种嵌套类:

    1. nested class,定义在类型内部的类型。
      1. static nested class,使用 static 声明的 nested class,static nested class 可以访问所有外部类的静态成员。
      2. inner class,没有使用 static 声明的 nested class,inner class 可以访问所有外部类的实例成员,inner class 不能定义静态成员。
    复制代码
    public class Program {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            OuterClass outer = new OuterClass();
            OuterClass.InnerClass inner = outer.new InnerClass();
            OuterClass.InnerClass.InnerInnerClass innerInner = inner.new InnerInnerClass();
            outer.show();
            inner.show();
            innerInner.show();
            
            OuterClass.StaticNestedClass staticNested=new OuterClass.StaticNestedClass();
            OuterClass.StaticNestedClass.StaticNestedNestedClass staticNestedNested=new OuterClass.StaticNestedClass.StaticNestedNestedClass();
            
            staticNested.show();
            staticNestedNested.show();
        }
    }
    
    class OuterClass {
        int x = 1;
        static int i = 1;
    
        void show() {
            System.out.println(x);
            System.out.println(i);
        }
    
        class InnerClass {
            int y = 2;
    
            void show() {
                System.out.println(x);
                System.out.println(y);
            }
    
            class InnerInnerClass {
                int z = 3;
    
                void show() {
                    System.out.println(OuterClass.this.x);
                    System.out.println(y);
                    System.out.println(z);
                }
            }
        }
    
        static class StaticNestedClass {
            static int j = 2;
    
            void show() {
                System.out.println(i);
                System.out.println(j);
            }
    
            static class StaticNestedNestedClass {
                static int k = 3;
    
                void show() {
                    System.out.println(i);
                    System.out.println(j);
                    System.out.println(k);
                }
            }
        }
    }
    复制代码

    特殊的inner class:local class

    复制代码
    public class LocalClassExample {
    
        static String staticValue = "static value";
        String instanceValue = "instance value";
    
        public void test() {
    
            final String finalLocalValue = "final local value";
    
            class LocalClass {
                void test() {
                    System.out.println(staticValue);
                    System.out.println(instanceValue);
                    System.out.println(finalLocalValue);
                }
            }
    
            LocalClass local = new LocalClass();
            local.test();
        }
    }
    复制代码

    除了inner class的规则之外,local class可以访问局部final变量,在Java8中有更多的改进。

    注意:

    1.被final修饰的类不可以被继承,为了避免被继承,被子类复写功能

    2.被final修饰的方法不可以被复写

    3.被final修饰的变量是一个常量,只能赋值一次i,既可以修饰成员变量,也可以修饰局部变量 作宏定义使用

    4.内部类定义在类中的局部位置上时,只能访问该局部被final修饰的局部变量

    特殊的local class:anonymous class

    复制代码
    public class Program {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            execute(new Action() {
                @Override
                public void execute() {
                    System.out.println("执行业务逻辑");
                }
            });
        }
    
        static void execute(Action action) {
            System.out.println("事物开始");
            action.execute();
            System.out.println("事物结束");
        }
    }
    
    interface Action {
        void execute();
    }
    复制代码

    常量

    复制代码
    public final class Program {
        static final String STATIC_CONSTANTS = "STATIC_CONSTANTS";
        final String INSTANCE_CONSTANTS = "INSTANCE_CONSTANTS";
    
        public static void main(String[] args) {
            final String LOCAL_CONSTANTS = "LOCAL_CONSTANTS";
    
            System.out.println(STATIC_CONSTANTS);
            System.out.println(new Program().INSTANCE_CONSTANTS);
            System.out.println(LOCAL_CONSTANTS);
            new Program().test("PARAMETER_CONSTANTS");
        }
    
        public final void test(final String msg) {
            System.out.println(msg);
        }
    }
    复制代码

    有一点需要注意的是:只有一种情况Java的常量是编译时常量(编译器会帮你替换),其它情况都是运行时常量,这种情况是:静态类型常量且常量的值可以编译时确定。

    接口

    Java的接口可以包含方法签名、常量和嵌套类,见下例:

    复制代码
    public final class Program {
        public static void main(String[] args) {
            Playable.EMPTY.play();
    
            new Dog().play();
        }
    }
    
    interface Playable {
        Playable EMPTY = new EmptyPlayable();
    
        void play();
    
        class EmptyPlayable implements Playable {
    
            @Override
            public void play() {
                System.out.println("无所事事");
            }
    
        }
    }
    
    class Dog implements Playable {
    
        @Override
        public void play() {
            System.out.println("啃骨头");
        }
    
    }
    复制代码

    异常

    Java中的异常分为checked和unchecked,checked异常必须声明在方法中或被捕获,这点我觉得比较好,必定:异常也是API的一部分,见下例:

    复制代码
    public final class Program {
        public static void main(String[] args) {
            try {
                test();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    
        public static void test() throws Exception {
            throw new Exception("I am wrong!");
        }
    }
    复制代码

    所有继承Exception的异常(除了RuntimeException和它的后代之外)都是checked异常。

     注意:

      throws:使用在函数上,后面跟的是异常类,可以跟多个,用逗号隔开
      throw:使用在函数内,跟的是异常对象

  • 相关阅读:
    自己回答自己的问题
    作业5 四则运算 测试与封装 5.1 5.2
    2015430 加法阶段一的封装测试
    20150423 提问2
    Java 泛型中的<? super T>和<? extends T>
    ssh 查看端口占用
    Java 回调
    SpringMVC4+MyBaits3配置
    Selenium 远程执行和监听类
    Intellij idea主题
  • 原文地址:https://www.cnblogs.com/liubin1988/p/9076049.html
Copyright © 2020-2023  润新知