● Tell me the difference between an Array and an ArrayList. When should you use Array instead of ArrayList?

Investigation point: Array

Reference Answer:

Differences between Array and ArrayList:

  • Array can contain both primitive and object types, and ArrayList can contain only object types.

    • When an ArrayList stores a base type, it is automatically boxed into the corresponding wrapper class, storing only its references, not the base type!
  • The Array size is fixed, and the size of an ArrayList changes dynamically (dynamically expanding the Array).

  • ArrayList provides more methods and features, such as addAll(), removeAll(), iterator(), and so on. For primitive type data, collections use automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with basic data types of fixed size.

Extension: ArrayList source code analysis

● Please explain what value passing and reference passing are.

Point of investigation: JAVA reference passing

Reference Answer:

  • Value passedIs theBasic type variablesIs passed a copy of the variable, changing the copy does not affect the original variable;
  • referenceGenerally forObject (reference) type variablesIs passed a copy of the object’s address, not the original object itself. So operating on a reference object changes the original object;
  • Generally speaking, passing in Java is all about value passing;

Case study:

public class StringBase {
 
    public static void main(String[] args) {
        int c = 66; //c is called an argument
        String d = "hello"; //d is called an argument
 
        StringBase stringBase = new StringBase();
        stringBase.test5(c, d); // where c and d are called arguments
 
        System.out.println("The value of c is:" + c + "-- d is:" + d);// the value of c is 66 -- the value of d is hello
    }
    
    public void test5(int a, String b) { // A and b are called parameters
        a = 55;
        b = "no"; }}Copy the code

We can see that the values of int and String are not affected by test5. This form is often referred to as value passing. If the original value is changed after the test5 method, this pattern is often described as passing by reference.

Blog.csdn.net/bntX2jSQfEH…

Blog.csdn.net/xiaojinlai1…

● Please explain why 4.0-3.6=0.40000001 occurs?

Research point: Computer fundamentals

Reference Answer:

The reason is simply as follows: the decimal in base 2 cannot accurately express the decimal in base 10, so the computer must first convert to base 2 for calculation in the process of calculating decimal in base 10, which causes errors in the process.

● Tell me how a decimal number is stored in memory.

Research point: Computer fundamentals

Reference Answer:

The form of the complement.

● do you know the new features of java8? Please give a brief introduction

Topic: Java8

Reference Answer:

  • Lambda expressions: Allow functions to be passed as arguments to a method.
  • Stream: Functional programming, streaming computing
  • Default method: The default method is a method that has an implementation in the interface. Reference article: blog.csdn.net/hello_IT_/a…
  • Method references: Method references provide a very useful syntax for directly referencing methods or constructors of existing Java classes or objects (instances). Used in conjunction with lambda, method references make language construction more compact and less redundant code. Blog.csdn.net/pipizhen_/a…

Examples of default methods:

/ / interface
public interface MyInterface {

     // Abstract methods
     String abstractMethod(a);

     // The default method
     default String defaultMethod(a){
         return "MyInterface---->defaultMethod";
     }

     // Static method
     static String staticMethod(a){
         return "MyInterface---->staticMethod"; }}// Interface implementation class
public class MyInterfaceImpl implements MyInterface{
	
    // Implement abstract methods in the interface
    @Override
    public String abstractMethod(a) {
        return "MyInterface---->abstractMethod"; }}/ / test
public class MyInterfaceTest {

    public static void main(String[] args) {

        MyInterfaceImpl myInterface = new MyInterfaceImpl();

        // The interface implementation class calls the default methods in the interface
        System.out.println(myInterface.defaultMethod());// MyInterface---->defaultMethod

        // The interface implementation class calls abstract methods in the interface
        System.out.println(myInterface.abstractMethod());// MyInterface---->abstractMethod

        // Call interface static methods directly
        System.out.println(MyInterface.staticMethod());// MyInterface---->staticMethod}}Copy the code

Method reference code examples:

  • Reference static methods:Class name :: Static method name
  • Methods that reference an object:Object :: instance method
  • To reference a method of a particular type:Class-specific :: instance methods
  • Reference constructor:The name of the class: : new
public class Test04 {
    public static void main(String[] args) {

        Class name :: Static method name
        Inter1<Integer, String> inter1 = String :: valueOf;
        String str1 = inter1.m1(100);
        System.out.println(str1);  / / 100

        // A normal method that references an object: object :: instance method
        Inter2<String> inter2 = "HELLO" :: toLowerCase;
        String str2 = inter2.m2();
        System.out.println(str2);  // hello

        // Reference a specific type of method: specific class :: instance method
        Inter3<String> inter3 = String :: compareTo;
        int res = inter3.m3("aa"."bb");
        System.out.println(res);  // -1

        // Reference constructor: class name ::new
        Inter4<Book> inter4 = Book :: new;
        Book b = inter4.m4("Introduction to Java Programming".25.36);
        System.out.println(b);  // Book{name=' Java ', price=25.36}}}interface Inter1<T.E> {
    public E m1(T t);
}

interface Inter2<T> {
    public T m2(a);
}

interface Inter3<T> {
    public int m3(T t1, T t2);
}

interface Inter4<T> {
    public T m4(String s, double d);
}

class Book {
    String name;
    double price;

    public Book(a) {}public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString(a) {
        return "Book{" + "name='" + name + '\' ' +
                ", price=" + price + '} '; }}Copy the code

● What does the symbol “==” compare?

The basics

Reference Answer:

  • if= =The basic types on both sides, justComparing numericalIs it equal?
  • if= =Object types on both sides, justCompare two objects based on memory references, returns true if the references to two objects are identical (pointing to the same object), false otherwise.

● Explain how the hashCode() in Object is calculated.

The basics

Reference Answer:

Object’s Hashcode methods are native methods, that is, implemented in C or C ++, that directly return the memory address of the Object.

● Please explain why you want to rewrite hashCode as well as equals.

Java Basics

Reference Answer:

Equals simply checks whether the object attributes are the same. Hashcode checks whether the two addresses are the same. To determine whether two objects are equal, both the address and attribute must be the same!

Refer to the article: jiming.blog.csdn.net/article/det…

  • If two objects are the same (that is, using equals returns true), their hashCode values must be the same;
  • If two objects have the same hashCode, they are not necessarily the same (i.e., using equals to compare returns false);

For example:

Override only equals(), not hashCode () :

public class Student {
	private String name;
	private int age;
 
	public Student(String name, int age) {
		super(a);this.name = name;
		this.age = age;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if(getClass() ! = obj.getClass())return false;
		Student other = (Student) obj;
		if(age ! = other.age)return false;
		if (name == null) {
			if(other.name ! =null)
				return false;
		} else if(! name.equals(other.name))return false;
		return true;
	}
	// omit get, set methods...
}
Copy the code

Execute the following program to see the effect:

public class hashTest {
	@Test
	public void test(a) {
		Student stu1 = new Student("Jimmy".24);
		Student stu2 = new Student("Jimmy".24);
		
		System.out.println("Are they the same person?"+stu1.equals(stu2));
		System.out.println("stu1.hashCode() = "+stu1.hashCode());
		System.out.println("stu1.hashCode() = "+stu2.hashCode()); }}Copy the code

Execution Result:

Are these two students the same person?true
stu1.hashCode() = 379110473
stu1.hashCode() = 99550389
Copy the code

If you override equals() but not hashCode (), you might end up with two unrelated objects equal (because equal is overridden based on the characteristics of the object) but different Hashcodes. The Student hashcode method is the default hashcode method of the Object. The default hashcode method is hash based on the memory address of the Object. = stu2, so the hashcode values of the two are not necessarily equal.

According to the rules of HashCode, two objects that are equal must have the same hash value, and this creates a contradiction. We’ve explained why we use the HashCode algorithm above, so even if the literals are equal, producing two different Hashcode values is obviously not the desired result.

2. If we override hashCode() when we override equals() :

public class Student {
	private String name;
	private int age;
	
	public Student(String name, int age) {
		super(a);this.name = name;
		this.age = age;
	}
	@Override
	public int hashCode(a) {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null)?0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if(getClass() ! = obj.getClass())return false;
		Student other = (Student) obj;
		if(age ! = other.age)return false;
		if (name == null) {
			if(other.name ! =null)
				return false;
		} else if(! name.equals(other.name))return false;
		return true;
	}
	// omit get, set methods...
}
Copy the code

Execution Result:

Are these two students the same person?true
stu1.hashCode() = 71578563
stu1.hashCode() = 71578563
Copy the code

As you can see from the Student class’s overwritten hashCode () method, the new hash value returned by the overwritten Student is associated with the two attributes of Student, thus ensuring a correlation between the object and its address.

Implements the concept that two objects must be equal to the same address.