In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the common interview questions in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. String s = new String ("xyz"); how many StringObject have been created? Can I inherit the String class?
Two or one of them is possible. "xyz" corresponds to an object that is placed in a string constant buffer, and the constant "xyz" is the one in the buffer no matter how many times it appears. Each time NewString is written, it creates a new object, which uses the contents of the constant "xyz" object to create a new String object. If you've used 'xyz',' before, you won't create a "xyz" here, just take it from the buffer, and a StringObject; will be created, but if you haven't used "xyz" before, an object will be created and put into the buffer, in which case it will create two objects. As for whether the String class inherits or not, the answer is no, because the String default final decoration is not inheritable.
2. The difference between String and StringBuffer
The JAVA platform provides two classes: String and StringBuffer, which can store and manipulate strings, that is, character data that contains multiple characters. This String class provides a string whose values are immutable. The string provided by this StringBuffer class can be modified. You can use StringBuffer when you know that the character data is about to change. Typically, you can use StringBuffers to dynamically construct character data.
3. How many objects have been created in the following statement: String s = "a" + "b" + "c" + "d"
For the following code:
String S1 = "a"
String S2 = S1 + "b"
String S3 = "a" + "b"
System.out.println (S2 = = "ab")
System.out.println (S3 = = "ab")
The result printed by the first statement is false, and the result printed by the second statement is true, which shows that javac compilation can optimize the expression of adding string constants directly. It is not necessary to wait for the runtime to carry out addition operation, but to remove the plus sign at compile time and directly compile it into a result connected to these constants.
The first line of code in the title is optimized by the compiler at compile time, which is equivalent to directly defining a "abcd" string, so the above code should only create a String object. Write these two lines of code
String s = "a" + "b" + "c" + "d"
System.out.println (slots = "abcd")
The final printed result should be true.
4. There is a return statement in try {}, so will the code in the finally {} immediately after the try be executed, when will it be executed, before or after the return?
We know that the statement in finally {} must be executed, so this may be blurted out normally before return and after return. God knows where it went, but it should be executed in the middle of return more accurately. Please see the running result of the following program code:
Public classTest {
Public static void main (String [] args) {
System.out.println (newTest () .test ())
}
Static int test ()
{
Intx = 1
Try
{
Returnx
}
Finally
{
+ + x
}
}
}
-execution result-
one
The result of the operation is 1. Why? The process of the main function calling the subfunction and getting the result, such as the main function preparing an empty jar, when the subfunction wants to return the result, first put the result in the jar, and then return the program logic to the main function. The so-called return is that the subfunction says, I will not run, your main function will continue to run, there is no result to speak of, the result is put into the jar before saying this.
5. The difference between final, finally and finalize.
Final is used to declare properties, methods, and classes, respectively, indicating that properties are immutable, methods are not overridden, and classes are not inheritable. For an inner class to access a local variable, the local variable must be defined as a final type.
Finally is part of the structure of the exception handling statement, which means that it is always executed.
Finalize is a method of the Object class, which is called when the garbage collector executes, and can override other resource collection when this method provides garbage collection, such as closing files, and so on. But JVM does not guarantee that this method will always be called
6. What are the similarities and differences between runtime exceptions and general exceptions?
The exception indicates the abnormal state that may occur during the running of the program, and the run-time exception represents the exception that may be encountered in the usual operation of the virtual machine, which is a common running error. The java compiler requires methods to declare that non-runtime exceptions may occur, but not to declare that uncaught runtime exceptions must be thrown.
7. What's the difference between error and exception?
Error says recovery is a serious problem in situations that are not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation. Exception represents a design or implementation problem. That is, it means that if the program works properly, it will never happen.
8. Briefly talk about the simple principle and application of exception handling mechanism in Java.
An exception refers to an abnormal situation or error that occurs when a java program is running (non-compiled), which is very similar to events in real life. Events in real life can contain information such as time, place, character, plot, etc., which can be expressed by an object. Java handles exceptions in an object-oriented way, which encapsulates each exception in the program into an object. The object contains information about exceptions.
Java classifies exceptions. Different types of exceptions are represented by different Java classes, and the root classes of all exceptions are derived from two subclasses under java.lang.Throwable,Throwable:
Error and Exception,Error represent a serious problem that the application itself cannot overcome and recover, and the program only collapses, for example, system problems such as memory overflows and thread deadlocks.
Exception indicates that the program can also overcome and recover problems, which can be divided into system exceptions and normal exceptions:
System exceptions are problems caused by defects in the software itself, that is, problems caused by poor consideration by software developers. Software users are unable to overcome and recover this problem, but under this problem, they can also keep the software system running or make the software fail, for example, array script out of bounds (ArrayIndexOutOfBoundsException), null pointer exception (NullPointerException), class conversion exception (ClassCastException).
A common exception is a problem caused by changes or anomalies in the running environment, and it is a problem that users can overcome, for example, the network is offline and the hard disk space is insufficient. After such an exception, the program should not die.
Java provides different solutions for system exceptions and ordinary exceptions. The compiler forces ordinary exceptions to be handled by try..catch or continue to be thrown to the upper calling methods with throws declaration, so ordinary exceptions are also called checked exceptions, while system exceptions can be handled or not handled, so the compiler does not force try..catch handling or throws declaration, so system exceptions are also called unchecked exceptions.
9. What's the difference between heap and stack in Java?
In JVM, heap and stack belong to different memory areas and are used for different purposes. The stack is often used to hold method frames and local variables, while objects are always allocated on the heap. The stack is usually smaller than the heap and is not shared among multiple threads, while the heap is shared by all threads of the entire JVM.
Stack: some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack. When it exceeds the scope of the variable, Java will automatically release the memory space allocated for the variable, which can be used for other purposes immediately.
Heap memory: heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After generating an array or object in the heap, you can also define a special variable in the stack so that the value of the variable in the stack is equal to the first address of the array or object in the heap memory. This variable in the stack becomes a reference variable of the array or object. Later, you can use the reference variable in the stack to access the array or object in the heap. A reference variable is equivalent to a name for an array or object.
10. Can you cast int into a variable of type byte? What happens if the value is greater than the range of the byte type?
We can do a cast, but in Java int is 32-bit and byte is 8-bit, so if cast, the high 24 bits of the int type will be discarded because the byte type ranges from-128 to 128.
This is the end of the content of "what are the common interview questions in Java"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.