Here will share some of the interview questions that appear extremely frequently, the initial week of a more, when to finish and when to stop.
Github address: github.com/Snailclimb/…
Value passing and reference Passing in Java (very important)
First to be clear: “Object passing (arrays, classes, interfaces) is referential passing, and primitive data passing (integer, floating point, character, Boolean) is value passing.”
So what are value passing and application passing?
Value passing is when an object is passed by value, meaning that a copy of the object is passed, and even if the copy is changed, the source object is not affected. (Because when a value is passed, it actually copies the value of the argument to the parameter.)
Reference-passing is when an object is passed by reference, meaning that a reference to an object is passed instead of the actual object. Therefore, external changes to reference objects are reflected in all objects. (Because when the reference is passed, it actually copies the address value of the argument to the parameter.)
Sometimes an interviewer will ask you something other than “is Java value pass or reference pass”. Instead, they give you an example and ask you to write your answer. This is also common in written exams! So, very important, look at the following example:
Value passing and application passing instances
1. The value
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
swap(num1, num2);
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
}
public static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
Copy the code
Results:
a = 20
b = 10
num1 = 10
num2 = 20
Copy the code
Resolution:
In swap, the values of A and B are swapped without affecting NUM1 and NUM2. The values in a and b are copied from num1 and num2. Num1, num2, num1, num2, num1, NUM2, num1, NUM2, NUM1, NUM2
2. Reference passing
public static void main(String[] args) {
int[] arr = {1.2.3.4.5};
change(arr);
System.out.println(arr[0]);
}
public static void change(int[] array) {
// Change the first element of the array to 0
array[0] = 0;
}
Copy the code
Results:
1
0
Copy the code
Resolution:
Both the main function and the change method operate on an array of the same address value. . Therefore, external changes to reference objects are reflected in all objects.
Some special cases
1. StringBuffer is passed
// Test reference passing: StringBuffer
@org.junit.Test
public void method1(a) {
StringBuffer str = new StringBuffer("Public account: Java Interview Clearance Manual");
System.out.println(str);
change1(str);
System.out.println(str);
}
public static void change1(StringBuffer str) {
str = new StringBuffer("abc");// Output: "public account: Java Interview Clearance Manual"
//str.append(" welcome to follow "); // Output: public number: Java interview clearance manual welcome your attention
//str.insert(3, "(programming)"); // Output: public number (programming) : Java interview clearance manual
}
Copy the code
Results:
Public account: Java interview clearance manual Public account: Java interview clearance manualCopy the code
Resolution:
A lot of questions to ask at this point: Why does StringBuffer still output the same value when it creates an object?
Because we’re creating a new StringBuffer object inside the Change1 method, STR points to another address, and the corresponding operation also points to another address.
So, if you change the change1 method to something like this, you should know what the output should be. If you don’t know, there is probably something wrong with what I said, I reflect (just kidding, the answer is already given in the program above) :
public static void change1(StringBuffer str) {
str.append("Welcome to your attention.");
str.insert(3, "(programming)");
}
Copy the code
2. String transmission
// Test reference passing: Sring
@org.junit.Test
public void method2(a) {
String str = new String("Public account: Java Interview Clearance Manual");
System.out.println(str);
change2(str);
System.out.println(str);
}
public static void change2(String str) {
// str="abc"; // Output: public number: Java interview clearance manual
str = new String("abc"); // Output: public number: Java interview clearance manual
}
Copy the code
Results:
Public account: Java interview clearance manual Public account: Java interview clearance manualCopy the code
You can see that whether you execute STR =” ABC;” STR = new String(” ABC “); The output value of STR is unchanged. As we said above in “StringBuffer passed”, STR =” ABC;” Should leave the value of the output of STR unchanged. Why is that? Because strings are immutable after creation.
3. A similar question
What is the following program output?
public class Demo {
public static void main(String[] args) {
Person p = new Person("Zhang");
change(p);
System.out.println(p.name);
}
public static void change(Person p) {
Person person = new Person("Bill"); p = person; }}class Person {
String name;
public Person(String name) {
this.name = name; }}Copy the code
Obviously it’s still going to output Joe. Because a Person object is recreated in the change method.
So what is the output if you change the change method to the one shown below?
public static void change(Person p) {
p.name="Bill";
}
Copy the code
I won’t say the answer, I think you should be very clear if you read the above content carefully.
Two == equals(important)
== : This is used to determine whether the addresses of two objects are equal. That is, determine whether two objects are the same object. (Base data type == compares values, reference data type == compares memory addresses)
Equals () : Also checks whether two objects are equal. But it is generally used in two ways:
- Case 1: The class does not override equals(). Comparing two objects of that class through equals() is equivalent to comparing them through “==”.
- Case 2: The class overrides equals(). In general, we override equals() to make the contents of two objects equal; Return true if their contents are equal (that is, the objects are considered equal).
Here’s an example:
public class test1 {
public static void main(String[] args) {
String a = new String("ab"); // a is a reference
String b = new String("ab"); // b is another reference, the object has the same content
String aa = "ab"; // Put it in the constant pool
String bb = "ab"; // Search from the constant pool
if (aa == bb) // true
System.out.println("aa==bb");
if (a == b) // false, different object
System.out.println("a==b");
if (a.equals(b)) // true
System.out.println("aEQb");
if (42= =42.0) { // true
System.out.println("true"); }}}Copy the code
Description:
- The equals method on String is overridden, because the equals method on object compares the memory addresses of objects, while the equals method on String compares the values of objects.
- When creating an object of type String, the virtual machine looks in the constant pool for an existing object with the same value as the object to be created, and assigns it to the current reference if it does. If not, create a new String in the constant pool.
Chapter iii hashCode and Equals (Important)
The interviewer may ask you, “Have you overridden hashcode and equals? Why do you have to override hashCode to override equals?”
Introduction to hashCode ()
HashCode () is used to get a hashCode, also known as a hashCode; It actually returns an int. The purpose of this hash code is to determine the index position of the object in the hash table. HashCode () is defined in the JDK’s Object.java, which means that any class in Java contains a hashCode() function.
Hash table stores key-value pairs. It can quickly retrieve the corresponding value according to the key. That’s where the hash code comes in! (Can quickly find the desired object)
Why hashCode
Let’s use “how a HashSet checks for duplicates” as an example of why hashCode is needed:
When you add an object to a HashSet, the HashSet evaluates the object’s Hashcode value to determine where the object was added. It also compares the object’s Hashcode value to the hashcode value of other objects that have already been added. If there is no matching Hashcode, the HashSet assumes that the object is not repeated. But if objects with the same Hashcode value are found, the equals () method is called to check whether objects with hashCode equality are really the same. If they are the same, the HashSet will not let the join succeed. If it is different, it will be rehashed to another location. (From the second edition of my Java primer, Head Fist Java). This significantly reduced the number of equals calls, which in turn significantly increased the execution speed.
HashCode () and equals ()
- If two objects are equal, the Hashcode must also be the same
- If two objects are equal, calling equals on both objects returns true
- Two objects have the same hashCode value, and they are not necessarily equal
- Therefore, if equals is overridden, hashCode must be overridden as well
- The default behavior of hashCode() is to generate unique values for objects on the heap. If hashCode() is not overridden, the two objects of the class will never be equal anyway (even if they point to the same data)
Write in the last
Recommend an open source back-end document of your own
Java Interview Customs Manual (Java Study Guide) Star: 1.4 k.
Github address: github.com/Snailclimb/…
This is an open source document for Java backend engineers, both new to Java and already a Java engineer.
Interview related resources are free to share
Java latest 17 years interview pen questions +Java + resume template employment guidance notes video +Java school recruitment interview Google interview by hand +Java[BAT] interview necessary +2017Google interview by hand Java school recruitment interview
Follow wechat public account “Java Interview Clearance Manual” reply “interview” you can get free!
Reference:
Blog.csdn.net/zhzhao999/a…
www.cnblogs.com/skywang1234…
www.cnblogs.com/skywang1234…
www.cnblogs.com/Eason-S/p/5…
If you want to get more of my original articles and quality learning resources, please follow my wechat official account :”Java Interview Clearance Manual “. No routine, hope to progress together with you, learn from each other.