In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the problems encountered by Java developers". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the problems encountered by Java developers"?
1. The most difficult problem in Java's "death contest"
Let's start with the hardest bones. The question was provided by Alexandru-Constantin Bledea from Bucharest, Romania. This question is really a brain teaser, and only about 20% of the participants answered it correctly, which means that even a blind election can improve your chances of getting it right. This question is about Java generics.
Main idea of the topic:
What's wrong with this code?
a. Compilation error because no SQLException was thrown
b. Throw ClassCastException because SQLException is not an instance of RuntimeException
c. Without error, the program prints out the thrown SQLException stack trace information
d. Compilation error because we cannot convert SQLException types to RuntimeException
OK, what information can we get from the title? Generics in the title involve type erasure, as well as some exceptions. Here is some knowledge to recall:
Both RuntimeException and SQLException inherit from Exception, but in this code RuntimeException is an unchecked exception and SQLException is an checked exception.
2.Java generics are not materialized. This means that the type information of the generic type is "lost" at compile time, and the generic parameter is replaced by its qualified type, or when the qualified type does not exist, the generic parameter is replaced with Object. This is what people call the type of "erase".
We naively hoped that line 7 would produce a compilation error because we couldn't convert SQLException to RuntimeException, but that wouldn't happen. What happens is that T is replaced with Exception, so we have:
Throw (Exception) t; / / t is also an Exception
The pleaseThrow method expects an Exception, and T is replaced with Exception, so the type conversion is erased as if the code had not been written. We can prove this from the following bytecode:
Private pleaseThrow (Ljava/lang/Exception;) V throws java/lang/Exception
L0
LINENUMBER 8 L0
ALOAD 1
ATHROW
L1
LOCALVARIABLE this LTemp; L0 L1 0
/ / signature LTemp
/ / declaration: Temp
LOCALVARIABLE t Ljava/lang/Exception; L0 L1 1
MAXSTACK = 1
MAXLOCALS = 2
Let's take a look at what the compiled bytecode looks like if generics are not involved in the code. We see the following code before ATHROW:
CHECKCAST java/lang/RuntimeException
Now, we can be sure that type conversions are not involved in the code, so we can rule out the following two options:
"compilation error because we cannot convert SQLException type to RuntimeException"
"throw ClassCastException because SQLException is not an instance of RuntimeException"
So after all, we throw SQLException, and then you want it to be captured by the catch code block, and then print its stack trace information. However, things went against one's wishes.
This code is deceptive and makes the compiler as confused as we are. This code makes the compiler think that catch code blocks are unreachable. For unwitting bystanders, there is no SQLException in the code. So, the correct answer is: the compilation failed because the compiler thought that SQLException would not be thrown from the try block of code-but it did!
Thanks again to Alexandru for sharing this question with us! We can use another cool way to see the errors in the code and how SQLException is actually thrown by modifying the catch code block to receive a RuntimeException. So you can see the stack information of SQLException. (in fact, SQLException is not captured by the catch code snippet, but is captured by the virtual machine and prints out the exception stack information. )
2. The crux of the question is whether toString () is used.
The correct rate of this question is only 24%, and its difficulty is the runner-up of the 20 questions.
What is the print result of this program?
A.m1 & new name
b. All of the above is wrong.
C.m1&m1
D.new name & new name
This problem is actually much simpler. All we have to do is look at line 12, which directly prints M1 and m2 instead of m1.name and m2.name. The tricky part of this code is that when we print an object, Java uses the toString method. We added the "name" attribute ourselves, and if you forget that and everything else is right, you may choose the wrong answer, m1&new name.
This line assigns the name property of both objects to "M1".
M1.name = m2.name = "M1"
Then the callMe method sets the name property of the m2 object to "new name", and the code ends.
However, this code snippet will actually print the following information, including the class names and their hash codes:
MyClass@3d0bc85 & MyClass@7d08c1b7
So the correct answer is "None of the above."
3. Sets in Google Guava class library
Main idea of the topic:
What is wrong with this question?
a. Cannot compile
b. No problem
c. May cause memory overflow
d. May cause a * cycle.
This question actually doesn't require much expertise in the Guava sets class library, but it confuses most developers. Only 25% of the participants gave the right answer, which was the same as that of the blind election.
So what can we see from this code? We have a method that returns a collection that contains someone's circle of friends. Method has a loop that checks whether the bestfriend property of a person object is null. If not null, bestfriend is added to the results collection. If a person object does have a bestfriend, repeat the above process for the bestfriend of the person, so we can keep adding the person object to the bestfriend collection until there is a person that does not have a bestfriend, or its bestfriend is already in our result collection. * this part is a bit subtle. We cannot add duplicate elements to the Set collection, that is, person objects, so this method does not cause a * loop.
The real problem is that this code is likely to cause an out of memory exception that runs out of memory. This loop actually has no boundaries, so we can keep adding person objects to set until we run out of memory.
By the way, if you want to learn more about Google Guava, you can take a look at our blog: the lesser known yet useful features about it
4. Initialize with two curly braces
Main idea of the title: where is the error in this code?
a. No mistakes.
b. It is possible to get null values
c. The code cannot be compiled
d. Print out incorrect results
This problem is one of the least code problems, but it is enough to confuse most developers. Only 26% of the respondents answered this question correctly.
Few developers know this simple syntax for initializing a collection of constants, although it has some side effects. But in fact, it is not a good thing that this grammar is little known. After sighing, you can see that we added an element to the list and then printed the list. Normally, you expect the printed result to be [John], but there is another initialization process with two curly braces for initialization. Here, we use an anonymous class to initialize a List. When we want to print the NAMES, what is actually printed is the null, because the initialization program is not yet complete and the list is empty.
For initialization of the container with two curly braces, see here (right here).
5. Bizarre events for the runtime Map container
This is another matter of community contribution, and the contributor is Barak Yaish from Israel. Only 27% of the respondents could answer this question.
What is the output of this code?
a. Cannot compile
b. Type conversion exception
C. [] true
D. ["bar", "ber"]
All right, let's look at the code. The compute method looks for a value in map through key. If the value is null, insert (key, value) and return value. Because at the beginning, the list is empty, the "foo" value does not exist, and v is null. Then, we insert a "foo" into the map and the "foo" points to new ArrayList (), and the ArrayList object is empty, so it prints out [].
In the next line, the "foo" key value exists in the map container, so we evaluate the expression on the right. The ArrayList object was successfully converted to type List, and the "ber" string was inserted into the List. The add method returns true, so true is the second line of print.
So the correct answer is "[] true". Thanks again to Barak for sharing this question with us.
Encouragement: let's take a look at the simplest questions
Which method is the easiest way to initialize Java strings
A.A
b. None of them.
C.C
D.B and C cannot be compiled
Now, let's take a look at the questions provided by Peter Lawrey. He works on the OpenHFT open source project and blogs on Vanilla Java. Peter ranks top 50 on StackOverflow, and this time he asks you a question that 76% of developers can answer.
C answer is simpler than A, B and D cannot be compiled.
Thank you for your reading. The above is the content of "what are the problems encountered by Java developers". After the study of this article, I believe you have a deeper understanding of the problems encountered by Java developers, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 215
*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.