7.15实习培训日志 java题解

周末总结

本周主要学习了markdown,git,docker等工具的使用。在本周的学习中,初步了解了markdown,git,docker的使用。本周的静态博客部署中,对于怎么显示一个博客内容,有两种,一是把public/下的内容放入nginx,然后配置Nginx配置文件,二是在Dockerfile配置hugo服务器,我刚开始把hugo-linux下到本地,传入github,但是太大了,整个项目有30m,于是在dockerfile中下载hugo,但可能网速有些慢。

cmd alias设置

  1. 创建文件C:cmd-alias.bat
  2. 注注册表进入HKEY_LOCAL_MACHINESoftwareMicrosoftCommand Processor,右键新建,字符串值,名为AutoRun,值为C:cmd-alias.bat,保存退出。

java 查看字节码

  1. cd到java文件目录
  2. javac name.java编译
  3. javap -v name.class显示java编译器生成的字节码,也可以反编译

java格式化代码:Shift Alt L

java assert

public class AssertTest {
  public void methodA(int i) {
    assert i >= 0 : methodB();
    System.out.println(i);
  }
  public void methodB() {
    System.out.println("The value must not be negative");
  }
  public static void main(String args[]) {
    AssertTest test = new AssertTest();
    test.methodA(-10);
  }
}

记得idea开启断言调试

RUN -> Edit Configurations -> Configuration -> VM options : 输入-ea,点击确定。

编译通不过,methodB()不能是一个void函数,必须有返回值,如int,String等。或者methodB()可以换成有返回值的东西,如i=1,i++等。

assert用法

  1. assert <boolean表达式>
    如果<boolean表达式>为true,则程序继续执行。
    如果为false,则程序抛出AssertionError,并终止执行。

  2. assert <boolean表达式> : <错误信息表达式>
    如果<boolean表达式>为true,则程序继续执行。
    如果为false,则程序抛出java.lang.AssertionError,并输入<错误信息表达式>。


静态变量

public class Static {
    static {
        int x = 5;
    }
    static int x,y;
    public static void main(String args[]) {
        x--;
        myMethod();
        System.out.println(x + y + ++x);
    }
    public static void myMethod() {
        y = x++ + ++x;
    }
}

首先,需要明白类的加载顺序:

  1. 父类静态对象和静态代码块(并且静态代码块和静态变量的执行顺序只跟代码中出现的顺序有关.)静态变量最好定义在静态代码块的前面
  2. 子类静态对象和静态代码块
  3. 父类非静态对象和非静态代码块
  4. 父类构造函数
  5. 子类非静态对象和非静态代码块
  6. 子类构造函数

接下来是变量的作用范围(作用域(scope))

在C、C++和Java中,作用去由花括号的位置决定

代码块中的局部变量与作用域

public static void main(String args[]) {
    int x = 100;
    {
        int x = 30;
    }
    System.out.println(x);
}

报错重复定义

public static void main(String args[]) {
    {
        int x = 30;
    }
    int x = 100;
    System.out.println(x);
}

代码块中的局部变量已经销毁,能定义

生存周期与作用域的区别:

生存周期: 变量从定义到销毁的时间范围。

作用域: 变量的可见代码域(块作用域,函数作用域,类作用域,程序全局作用域)。

静态变量,全局或局部声明的static变量都存放于程序的全局变量区域,,静态变量的生命周期都是整个源程序。它的作用域决定于它被定义的位置。所以静态变量的作用域<=生存周期。

static函数在内存中只有一份,普通函数在每个被调用中维持一份拷贝

class文件:java字节码文件

static int x;
static {
    x = 5;
}

x结果为5

static {
    x = 5;
}
static int x;

x结果为5

static {
    x = 5;
}
static int x = 1;

x结果为1

static int x = 1;
static {
    x = 5;
}

x结果为5

static int x = 1;
static {
    int x = 5;
}

x结果为1(static块为局部变量)

static {
    int x = 5;
}
static int x = 1;

x结果为1(static块为局部变量)

static String s;
public static void main(String args[]) {
    //String s = "";  1
    if (s instanceof String) {
        System.out.println("I am true String");
    } else {
        System.out.println("I am false String");
    }
}

运行结果为 "I am false String"如果改成1处则为true

instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例

三元运算符自动转换

public static void main(String[] args) {
  int a = 5;
  System.out.println("value is "+((a<5) ? 10.9 : 9));
}

输出9.0,根据运算符的精确度类型进行自动类型转换

char x='x';
int i =10;
System.out.println(false ? i : x);
System.out.println(false ? 10 : x);

输出120,x.

java规范中提到,当后两个表达式有一个是常量表达式(10),另外一个类型是T(char)时,而常量表达式可以被T表示时,输出结果是T类型。

Boolean b = new Boolean("dfas"); #能编译,返回false
int c = 0x1234; #能编译通过

线程问题

class MyThread extends Thread {
    public void run() {
        System.out.println("MyThread: run()");
    }
    public void start() {
        //super.start();
        System.out.println("MyThread: start()");
    }
}
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("MyRunnable: run()");
    }

    public void start() {
        System.out.println("MyRunnable: start()");
    }
}
public class MyTest {
    public static void main(String args[]) {
        MyThread myThread = new MyThread();
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        myThread.start();
        thread.start();
    }
}

A. Prints : MyThread: start() followed by MyRunnable:run()
B. Prints : MyThread: run() followed by MyRunnable:start()
C. Prints : MyThread: start() followed by MyRunnable:start()
D. Prints : MyThread: run() followed by MyRunnable:run()

打印

MyThread: start()
MyRunnable: run()

在MyThread中start方法加上super.start();
打印
MyThread: start()
MyThread: run()
MyRunnable: run()

A正确,MyThread重写了start方法,实际上只是一个普通方法调用,thread.start()不会调用MyRunnable中的start方法.

线程启动

抛出异常格式

static void throwMethod() throws IllegalAccessException {
  System.out.println("Inside throwMethod.");
  throw new IllegalAccessException("demo");
}

public static void main(String args[]) {
  try {
      throwMethod();
  } catch (IllegalAccessException e) {
      System.out.println("Caught " + e);
  }
}

注意throwMethod() throws IllegalAccessException

父类子类方法执行

class Base {
    int i = 99;
    public void amethod() {
        System.out.println("Base.amethod()");
    }
    Base() {
        amethod();
    }
}

public class Derived extends Base {
    int i = -1;
    public static void main(String argv[]) {
        Base b = new Derived();
        System.out.println(b.i);
        b.amethod();
    }
    public void amethod() {
        System.out.println("Derived.amethod()");
    }
}

**初始化:先成员变量再构造方法,先父类再子类

多态表现:有同名方法执行子类的**

class Parent {
    private void method1() {
        System.out.println("Parent's method1()");
    }
    public void method2() {
        System.out.println("Parent's method2()");
        method1();
    }
}
class Child extends Parent {
    public void method1() {
        System.out.println("Child's method1()");
    }
    public static void main(String args[]) {
        Parent p = new Child();
        p.method2();
    }
}

注意Parent的method1是私有的,所以会调用父类方法

Parent's method2()
Parent's method1()
String str = "Java";
  StringBuffer buffer = new StringBuffer(str);
  if(str.equals(buffer)) {
    System.out.println("Both are equal");
  } else {
    System.out.println("Both are not equal");
}

返回false,都不是同一个对象,可以看一下equals的源码

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            ...
        }
        return false;
    }
原文地址:https://www.cnblogs.com/sufferingStriver/p/9332287.html