mb61ab40ce80d9c 2022-01-15 03:26:39 阅读数:447
try { } catch (Exception e) {
return 0;
} finally {
System.out.println("finally returnCatchExec");
return -1;
}
}
public static void exitTryExec() {
try {
System.exit(0);
} catch (Exception e) {
} finally {
System.out.println("finally exitTryExec");
}
}
public static void exitCatchExec() {
try { } catch (Exception e) {
System.exit(0);
} finally {
System.out.println("finally exitCatchExec");
}
}
}
The result of program execution is as follows :
finally returnTryExec
finally returnCatchExec
Third question , Private or static methods can be overridden (override) Do you ?
The reason why Xiao Wang didn't answer this question correctly , Because he's not sure about the relationship between private methods or static methods and rewriting .
The two overridden method names are the same , The number of method parameters is the same ; But a method is in the parent class , The other one is in the subclass .
class LaoWang{
public void write() {
System.out.println(" Lao Wang wrote a book 《 The count of monte cristo 》");
}
}
class XiaoWang extends LaoWang {
@Override
public void write() {
System.out.println(" Xiao Wang wrote a book 《 Camellia woman 》");
}
}
public class OverridingTest {
public static void main(String[] args) {
LaoWang wang = new XiaoWang();
wang.write();
}
}
Parent class LaoWang There is one write()
Method ( No arguments ), The method body is to write a book 《 The count of monte cristo 》; Subclass XiaoWang Overriding the write()
Method ( No arguments ), But the method body is to write a 《 Camellia woman 》.
stay main In the method , We declared a type of LaoWang The variable of wang. During compilation , The compiler will check LaoWang Class contains write()
Method , Find out LaoWang Like , So compile through . During operation ,new One. XiaoWang object , And assign it to wang, here Java Virtual machines know wang The reference is XiaoWang object , So the call is subclass XiaoWang Medium write()
Method instead of parent class LaoWang Medium write()
Method , So the output is zero “ Xiao Wang wrote a book 《 Camellia woman 》”.
Private methods are invisible to subclasses , It is only visible in the class currently declared ,private Keywords meet the highest level of encapsulation . in addition ,Java The private methods in are bound by static binding at compile time , Independent of the object type held by a specific reference variable .
Method rewriting works for dynamic binding , So private methods cannot be overridden .
class LaoWang{
public LaoWang() {
write();
read();
}
public void write() {
System.out.println(" Lao Wang wrote a book 《 The count of monte cristo 》");
}
private void read() {
System.out.println(" Lao Wang is reading 《 Hamlet 》");
}
}
class XiaoWang extends LaoWang {
@Override
public void write() {
System.out.println(" Xiao Wang wrote a book 《 Camellia woman 》");
}
private void read() {
System.out.println(" Xiao Wang is reading 《 Merchant of Venice 》");
}
}
public class PrivateOrrideTest {
public static void main(String[] args) {
LaoWang wang = new XiaoWang();
}
}
The program output is as follows :
Xiao Wang wrote a book 《 Camellia woman 》
Lao Wang is reading 《 Hamlet 》
In the construction method of the parent class , Respectively called write()
and read()
Method ,write()
The method is public Of , can
《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing
To be rewritten , So the subclass's write()
Method ,read()
The method is private , Can't be rewritten , So the execution is still of the parent class read()
Method .
Similar to private methods , Static methods are also bound by static binding at compile time , Independent of the object type held by a specific reference variable . Method rewriting works for dynamic binding , So static methods cannot be overridden .
public class StaticOrrideTest {
public static void main(String[] args) {
Laozi zi = new Xiaozi();
zi.write();
}
}
class Laozi{
public static void write() {
System.out.println(" Laozi wrote a book 《 The count of monte cristo 》");
}
}
class Xiaozi extends Laozi {
public static void write() {
System.out.println(" Boy wrote a book 《 Camellia woman 》");
}
}
The program output is as follows :
Laozi wrote a book 《 The count of monte cristo 》
Reference variables zi The type of Laozi, therefore zi.write()
The execution is in the parent class write()
Method .
Static methods are also called class methods , You can call... Directly through the class name , When called through an object ,IDE There's a warning .
Fourth question ,1.0/0.0
What is the result ? Will it throw an exception , There will still be compilation errors ?
The reason why Xiao Wang didn't answer this question correctly , It's because he didn't go deep into double The type and int Division of type .
The number is Java It can be divided into two kinds , One is plastic surgery , One is floating point . I'm not sure. Let's study it first [ data type ]( ).
When a floating-point number is divided by 0 When , The result is Infinity perhaps NaN.
System.out.println(1.0 / 0.0); // Infinity
System.out.println(0.0 / 0.0); // NaN
Infinity It means infinity in Chinese ,NaN This is not a number (Not a Number).
When an integer is divided by 0 When (10 / 0
), It throws an exception :
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.itwanger.eleven.ArithmeticOperator.main(ArithmeticOperator.java:32)
Usually , When we divide integers , You need to determine whether the divisor is 0, In case the program throws an exception .
Fifth question ,Java Support multiple inheritance ?
The reason why Xiao Wang didn't answer this question correctly , It's because he knows , Multiple inheritance can be achieved through interfaces .
To define two interfaces ,Fly Can fly ,Run Can run .
public interface Fly {
void fly();
}
public interface Run {
void run();
}
Then let a class implement both interfaces at the same time .
public class Pig implements Fly,Run{
@Override
public void fly() {
System.out.println(" Flying pigs ");
}
@Override
public void run() {
System.out.println(" Running pigs ");
}
}
But when it comes to multiple inheritance , The key words discussed are extends, Instead of implements.
Java Only single inheritance is supported , It's because of the diamond problem . If two classes inherit a parent class with a specific method , Then the method may be overridden by two subclasses . then , If you decide to inherit these two subclasses at the same time , So when you call the override method , The compiler doesn't know which subclass's method you want to call .
class C At the same time, it inherits the class A And the class B, class C Object calling class A And the class B Method overridden in , You don't know the calling class A Methods , Or class B Methods .
Sixth question , When in HashMap Put an existing key when , What's going to happen ?
The reason why Xiao Wang didn't answer this question correctly , It's because he didn't go deep into HashMap How it works .
Hash, In general, the translation is “ hash ”, There are also direct transliteration for “ Hash ” Of , What does this mean ? It is to map any length of data to a fixed length field through an algorithm ( Hash value ).
A little more intuitive , It's just a bunch of data wang Hybridity , Output another piece of fixed length data er—— As data wang Characteristics of . We usually use a string of fingerprints to map someone , Don't look down on fingerprints as big as your fingers , It's hard to find a second one that's the same as you in your area ( People's hash algorithm is also powerful , Is there any ).
For any two different data blocks , It's very unlikely that their hash values will be the same , in other words , For a given block of data , It's extremely difficult to find the same block of data as its hash value . also , For a block of data , Even if only one bit of it is changed , The hash value changes will be very large —— That's exactly what it is. Hash The value of existence !
You should know ,HashMap The underlying data structure of is an array , adopt hash()
Method to determine the subscript .
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
When we put in a key value pair , Will call first hash()
Method pair key Hash algorithm , If key It's the same , So the hash result is the same , It means that the subscripts in the array are the same , The new value will override the original value .
Question seven , What will the following code print out ?
public class Test {
public static void main(String[] args) {
char[] chars = new char[]{'\u0097'};
String str = new String(chars);
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));
}
}
The reason why Xiao Wang didn't answer this question correctly , It's because he didn't have in-depth knowledge of character coding .
In this procedure , We create a string object from an array of characters , And then call String Class getByte()
Method to get the byte array and print it to the console .
The core of this interview question is not the final printed result ( The result is uncertain ), It's character coding . Usually , We're calling getBytes()
When the method is used , To specify the encoding , for instance str.getBytes(StandardCharsets.UTF_8)
.
When we don't specify the encoding ,JDK Will call the platform's default character encoding , And different operating systems , The codes are not the same ,bytes And the results will be different .
When using UTF_8 when , The result is -62, -105
, When using GB2312 when , The result is 63
.
The eighth question , When a method is thrown in a parent class NullPointerException
when , Whether you can use throw RuntimeException
To rewrite it ?
The reason why Xiao Wang didn't answer this question correctly , Because he was rewritten (overriding) And overloading (overloading) I'm confused with the concept of .
Method rewriting and method overloading , Method names can be exactly the same , But the fundamental difference is that method rewriting occurs at runtime , Method overloading occurs at compile time .
in addition , The rules for method rewriting and method overloading are also different . stay Java in , Can not rewrite private、static and final Method , But you can overload them .
Let's focus on the rules for method rewriting :
1) Method signature must be the same , Including return types 、 Number of parameters 、 The type and order of the parameters .
2) An overridden method cannot throw an exception at a higher level than in the parent class . for instance , If the method in the parent class throws IOException, Then overridden methods in subclasses cannot throw Exception, It can be IOException Or does not throw any exceptions . This rule only applies to checkable exceptions .
With this , Interview stepping on thunder ? There is no the !
This article has been CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included
版权声明:本文为[mb61ab40ce80d9c]所创,转载请带上原文链接,感谢。 https://javamana.com/2021/12/202112122307167143.html