Discussion Questions software configuration summary

Because Experimental procedure and reporting workload with the large, high quality in order to complete the experiment, so not much time to write a blog. (Java too because the slag) (such as primary school exam period can write summary, harvest)

Here all the questions posed by the teacher re-think again.此文章也是为了自己复习,所以会从一个问题中引申出许多东西,勿怪

A problem of winter

Here Insert Picture Description

  • The first question obviously has a syntax error, the use of Chinese quotes, and a and b are obviously not equal (estimate teacher wrong). Then the teacher also changed the problem, the Chinese changed quotes in quotation marks, and instead String b = "a". First, let's take a look and String a String b.
    According to our knowledge in Chapter VIII memory tuning we learned to know, create strings in Java, there are two cases, one is new out, while the other is on the map as directly as created. All Java objects are created on the heap (Heap), while String is no exception, so if the new string out, it will be placed on the Heap. However, because the string is very popular, so JVM in the Heap also assigned to string a string constant pool (SCP), the second way to save the string is created. As shown below.
    Here Insert Picture Description
  • Consider again == and equals. In Java == is used to compare the equivalence of references, equals for equivalence comparison of (python just the opposite), it calls the equals method of the object, as if the pair does not override the equals method, then will call Object (all classes are directly or indirectly inherit the Object class) equals method, and Object equals the direct use ==. So if there is no implementing the equals method, then call the object's equals is the direct use == to compare whether two references point to the same piece of memory. When overriding equals to pay attention to the incoming parameters should be Object, that is,
public boolean equals(Object obj)

Otherwise, you realized that equals an overload (overload) instead of rewriting (override), in addition a more important Override, clone method clonealbe interface, the first point to note in the Object method is protected, to when Override to public; second point to be noted that the problem of the shallow vs. deep copy. Here can be further extended to the sub-category method Override parent to comply with the principle of LSP. The method of the parent class overridden in a subclass, the need to implement covariant return value, parameters of the inverter. It is overloaded in Java as a parameter covariance or whether you will become. Below this can be skipped because of time, so writing is not careful, there may be errors. . . . Further, a generic polymorphic particular attention, because when implemented using Java generic 类型擦除, i.e. while generating byte code, the type of the parameter bounds replace all or class Object (if the type parameter is then unbound) Therefore: Generics are type invariant Generics are not covariant ( covariant). I.e. ArrayList <String> is a List <String> subclass, while List <Integer> instead List <Number> subclass. So to implement generic polymorphic, to use wildcards?. There are three.

  • Use directly? , Indicating that the type is unknown. List following figure shows an example of printing elements
    Here Insert Picture Description
  • use? super Integer, Integer expressed as a lower bound
  • use? extends Number, Number expressed as an upper bound. The relationship between these three categories as shown below
    Here Insert Picture Description
  • 好像说的有点远了。回到我们的问题,由于字符串常量池,所以a和b指向同一块内存(该内存存放字符串"a"),==比较其引用是否相同,这两者是相同的,所以输出结果为:a and b: true ?
    要是这么想,你就太天真了。真实结果是:false
    我们来一探究竟,慢慢过一遍才发现。+和==是有优先级的(即使调换顺序变为 a==b + "a and b"依旧先计算字符串拼接),上段代码中先计算 “a and b” + a。然后将计算得到的结果"a and ba"与 b 也就是"a"进行比较,两者当然是不同的,所以只输出false。

所以该问题要是加了括号就是内存分配问题,不加括号就是运算符优先级问题

雨课堂中的一道Overload题

Here Insert Picture Description
D选项如下图所示,将图中的Object 改为String就是问题中的函数。
Here Insert Picture Description

  • 在上一个问题中已经说过了,泛型不是类型协变的,即List<String>不是List<Object>的子类,类型擦除后,两者是一样的。而重载需要参数列表不同(父类和子类也算不同),所以这里不能构成重载。

Java缓存相关问题

Here Insert Picture Description

  • 首先第一个false是因为a和b指向的内存不同,所以false。

  • 改成 Integer a = 200, b = 200之后,和第三种情况调用Integer.valueOf()是相同的,因为前一种情况在编译时也是调用Integer.valueOf()函数。该函数源码如下图
    Here Insert Picture Description
    通过查看源码,可以发现Integer和String一样存在缓存,缓存区间为-128-127.但是a和b都是200.所以它们都是通过new得到的Integer对象,所以两者引用(reference)不等。将200换成2之后自然相等。并且Integer和String类似,当使用构造器new出一个对象时,不会使用缓存,只有在自动装箱时才会使用缓存。此外,int 和Integer在进行比较时,Integer会自动拆箱。

  • 继续问题
    Here Insert Picture Description

  • 使用JVM参数即可设置。-XX:AutoBoxCacheMax

  • 问题+1
    Here Insert Picture Description

  • 通过查阅资料,JVM cache all char and short values, as well as int and long values in the range of -32K to +32K.

还是关于Integer的问题

Here Insert Picture Description
Here Insert Picture Description

  • 首先看两个问题有何区别,第一个是比较a和b之后,再对其++。而由上一个问题可以知道,a和b都是new 出来的,两者的reference是不相同的,所以会输出false。
  • 第二种情况,Integer是Immutable的,对其进行++操作会产生一个新的Integer对象,产生的过程为首先调用intValue()进行拆箱
public int intValue() {
    return value;
}

然后调用Integer.valueOf构造新对象,当结果再-128-127之间时,会利用常量池中的Integer,所以返回相同的对象。所以第二种情况输出true。当然若是

Integer a = new Integer(2);
Integer b = new Integer(2);
a++; b++;
print(a==b);

也为true。若是将2改为127,则结果为false。因为已经超出了常量池,a和b都是new 出来的新对象。

异常相关的问题

Here Insert Picture Description

  • 首先说结论,返回false,下面引用同学的回答
    Here Insert Picture Description

泛型的类型参数命名

Here Insert Picture Description

  • 还是引用同学的回答。。。
    Here Insert Picture Description

Override interface can return a value covariant

Here Insert Picture Description

Override problem

Here Insert Picture Description

  • Print out B.bar. Gangster borrow interpretation of
    Here Insert Picture Description
    different static methods, static methods are called depends on the location (parent class or subclass) is called, it calls the instance method depends on the current instance of the object in the end is which.

Abnormal related issues

Here Insert Picture Description

  • Compilation error because FileNotFoundException is a subclass of Exception, behind the catch never executed, and static checking compiler will not allow dead code, so the error.
    Here Insert Picture Description
  • If the file does not exist, it will print out a. Since the first to be captured first catch, then it will jump out of the code after the catch, the implementation of catch.
Published 29 original articles · won praise 10 · views 7177

Guess you like

Origin blog.csdn.net/weixin_42017042/article/details/92842150