Java Novice's common problem (programming random blog)

Source: Internet
Author: User

One: Unfamiliar with algorithms and data structures Why do I take "data structures and algorithms" to say things? This thing is the most basic writing program stuff. Whether you use Java or any other language, you can't do without it. And this thing is cross-lingual, after learning, no matter in which language can be used.

Since "Data structures and algorithms" are so important, why are many Java novices unfamiliar with pinching? I figured out that there are two possible possibilities. Some people graduate from the computer department, but did not learn the course at the beginning of the work, and back to the teacher, and some people are halfway through the process of programming, career change and did not properly lay the groundwork (all hope that the crash).
Here are a few very basic questions, if you make it clear that every problem, it means that you pass this, you can go to see the next post. Otherwise, you hurry to find this algorithm and the data structure of the Book of Evil to fill it.

★ When should use the array type container, when should use the Chain table type container?
★ What is a hash function? What is the implementation principle of HASHMAP?
★ What is recursion? If you've never written a recursive function before, try writing one (such as a recursive function for tree traversal).
★ What is algorithmic complexity?
★ Do you understand the idea of space-changing time?
★ Write a bubble sort function for an array of integers to see how many times you have to modify to run through.
★ Write a binary lookup function for an array of integers to see how many times you need to modify to run through. Second: lack of object-oriented basic skills Java is a very OO language, the Java community has always been full of "object" atmosphere. But I was interviewing the Java programmer, but repeatedly encountered something that surprised people. I have encountered more than one job seeker, even what is "polymorphic" is not clear. Many people claim to have used design patterns, but more than half are limited to single-key mode and abstract Factory mode. A lot of people vague when I asked him/her about the benefits of abstract Factory mode.

Why do many Java programmers lack object-oriented fundamentals? It's the Java framework that's weird. Now Java's various frameworks are too developed, too stupid, so many programmers only need to step-by-step, according to the framework of the code to fill in the blanks, basically has lost the ability of OOA and OOD. I have a Java programmer who knows the framework of Spring, Hibernate, etc, but if you give him a simple need to write a standalone application out of the Web frame, he is overwhelmed. Such developers, in the future can only become the so-called "software blue Collar", jobs are difficult to improve.

As with the above, I will mention the following questions this time:
★ What are the advantages and disadvantages of interface-based inheritance and implementation-based inheritance?
★ What are the "drawbacks" of inheritance (including extend and implement)?
★ What are the "drawbacks" of polymorphic (polymorphism)?
★ Why Java can inherit interface more, and can not inherit class more?
★ If you want to write a small game (such as Gobang), how would you design the class structure?
★ How to consider extensibility when designing a class structure?

If you can get a clearer picture of all of these problems, your OO base is passable. Otherwise, I suggest you look for some OOAD and design patterns, and write some simple small programs (without relying on those frameworks), and combine the theory of the learned model into practice. In this way to improve their ability to OOAD, the effect will be better. Three: lack of good programming habits ★ randomly named


Some new handwriting programs, when you need to define a variable name (also may be the function name, class name, package name, etc.), casually hit the keyboard, the name is OK ... A few weeks later, when I came across a bug and looked at the code I wrote, I whispered, "Is this code I wrote?" I can't even read it. ”
So I often talk to the new rookie said, the naming is not standardized to kill people ah! Given the prevalence of this problem, I have collated several typical examples, as follows: using single-letter named variables, using variable names that do not make much sense (for example, S1, S2, S3), and using different terms/abbreviations for the same business concept (easy for people who read code to divide the nerves) Use Pinyin to name it (if you have an RTHK or a foreigner on your team).

★ Get used to the code copy & paste

This is a very common problem. A lot of new handwritten code, if found to write a function and a function written a few days ago, the original function is pasted, and then a few changes, the heart still Anxi: "and quickly finished a function" ...
Students, if you also like to do so, you have to pay attention to. This approach is a major source of code odor (a reference to refactoring-improving the design of existing code), leading to a significant reduction in code maintainability. When you need to add functionality or modify bugs in the future, you have to change multiple places at the same time, and then you might not remember that the mound code has several clones.

★magic number Flying

If you have not heard of "Magic number", First look at "here" to understand.
To illustrate the question of magic number, let's take an example: Suppose you have a 10-second timeout waiting in a business logic, how would you write the sleep statement? I guess most people are the following three ways to do it:
1, directly write the incident sleep(10*1000);
2. Define a constant TIMEOUT_XXX = 10*1000; andsleep(TIMEOUT_XXX);
3, add a timeout in the configuration file, then the program reads the configuration file to get the timeout value, and then call sleep. (the "configuration file" mentioned here is generalized and refers to a variety of mechanisms that can be used to store configuration information, such as: XML files, ini files, databases ...) )
If your approach is similar to writing 1, you probably prefer hard coding. Hard coding is not only a lack of readability, but also has a code similar to "code copy paste" odor (there may be more than one Magic number clone), not conducive to future maintenance.
As for the 2, it is better than 1 (at least readability). However, if demand changes occur in the future, requiring that the time-out interval be adjusted at run time (even requiring the user to make a time-out interval), the disadvantage of 2 is immediately exposed.

★ Code coupling is too large

When it comes to MVC or design patterns, almost every Java developer can speak well? But when it comes to writing code, the code that someone writes is clearly layered. As to what is the case for code coupling? What is the orthogonal design? (With regard to coupling and orthogonal design, I will discuss it later) there are fewer people who can fully understand it.
So it's not surprising that a lot of Java novices have very high-level code coupling. I have been checking the code of the probation staff, all kinds of business logic entangled together, the code stinks are to smoke dead. Want to refactor all the impossible, had to let him overturn rewrite.

★ Spoiled by GC

Because Java provides a garbage collection mechanism for memory at the language level, programmers can apply for memory without having to worry about releasing issues. So many novices develop bad habits, and for other resources (such as database connections) only apply not to release (some people even naively assume that the JVM will help you with resource recycling).
Some people know that resources need to be released, but often forget (such as writing to open the database connection and related code, "soon" to write off the database connection, suddenly someone told you to eat lunch, back to the stubble to forget).
This bad habit can lead to resource leaks, and resource leaks are often more deadly than memory leaks. If you write a program that is long-running (for example, running on a Web Server), it can cause problems in the process due to resource exhaustion.

Four: Exception handling improper use ★ Empty CATCH statement block

There are fewer people who make this mistake, usually in people who have just learned Java or have just come to work soon.
The so-called "empty catch statement block" is that there is no processing of exceptions in the catch statement block (such as an error log), which causes the exception information to be discarded/ignored. Once the program does not run correctly, because no log information can be found, have to look at the code from the beginning, relying on the naked eye to find bugs.

★ No use of finally

Many people do not use the finally statement after a catch statement. Because the request and release of the resource may be involved in a try statement. If an exception is thrown after the resource request and before the resource is released, a resource leak occurs.
(The severity of the resource leak has been discussed above)

★ General CATCH statement block

For the sake of convenience, some people only package a try statement block in the outermost code of their module, and then catch (Exception). No matter what anomalies are caught, make a unified log. This is slightly better than the empty catch statement block, but the chance of recovery is lost due to the inability to deal specifically with specific exceptions and to some recoverable exceptions (mentioned below). It may also lead to the disclosure of resources mentioned above.

★ Use function return value for error handling

Some people put Java's exception mechanism unused, and use the function return value to indicate success/failure (for example: Return TRUE indicates success, false means failure), is simply "holding a golden Bowl beggar." Personally, people who go from C to Java are more likely to have this problem. This approach can lead to the following issues:
1. The return value is generally represented by an integer value or a Boolean value, and the information passed is too simple;
2. Once the caller ignores the error return code, it causes a problem similar to the "Empty catch statement block";
2. Multiple calls to the same function require repeated judgment of the return value, resulting in code redundancy (the disadvantage of code redundancy, which has been discussed above).

★ Not clear the difference between "Checked Exception" and "Runtime Exception"

This phenomenon is more common, I found that many 2 years of Java work experience has not fully understood the difference between the two. It seems that the question should be described in detail.
The original Java designers deliberately distinguish between these two kinds of anomalies, it is not meaningful. where "Checked Exception" is used to represent a recoverable exception (that is, the exception you have to check), and "Runtime Exception" means an unrecoverable exception (that is, a run-time exception, mainly a program bug and a fatal error, which you "do not need" to check). However, this approach has led to a lot of controversy (including many Java Daniel), because this post is mainly for the novice, and later to talk about this controversial topic.
For the sake of understanding, let me give an example to illustrate. Suppose you want to write a Download function that returns the content text of the corresponding Web page based on the incoming URL (String parameter). There are two situations in which you need to deal with:
1. If the passed-in URL parameter is NULL, this indicates that the caller of the function has a bug, and that the bug in the program itself is difficult to recover at run-time. At this point the Download function must throw Runtime Exception. And the caller of the Download function "should not" try to handle the exception, it must be exposed "as soon as possible" (such as letting the JVM itself terminate the operation).
2. If the passed-in URL parameter is not NULL, it contains a string that is not a valid URL format (possibly due to user input errors). At this point the Download function must throw Checked Exception. And the caller of the Download function must catch the exception and handle it appropriately (such as prompting the user to reenter the URL).

V: Don't understand jvm★ about basic types and reference types

Many novices do not understand what the basic and reference types of Java differ in nature. Please see the following questions:
◇ What is the difference between these two types in memory storage?
◇ What are the differences between the two types in performance?
◇ What is the difference between these two types of GC?
For the first two questions, see the previous post, "Java performance Tuning [1]: Basic type vs Reference type".

★ About garbage collection (garbage Collection)


Many novices do not understand the implementation mechanism of GC. Please see the following questions:
How does ◇GC determine which objects have been invalidated?
What are the effects of ◇GC on performance?
◇ How to tune GC performance with JVM parameters?
Questions about GC can be found in the previous post "Java performance optimization [3]: About garbage collection (GC)".

★ About Strings

For the string and StringBuilder provided by Java, it must be known to many: string is used for constant strings, and StringBuilder is used for mutable strings. Why did Java have to design such a pinch? Why not use a class to unify the pinch?

★ About Model (Generic programming)

Starting with JDK 1.5, Java introduced a heavyweight syntax: Paradigm. However, many novice only know the fur of the model, and for many of the essence of the east, not very understanding.
is ◇GP implemented at compile time or at run time? Why do you do this?
What is the type erase mechanism for ◇GP? What are the pros/cons?
◇ What is the performance impact of using a generic container (as opposed to a traditional container)? Why?

★ About Multithreading

In addition, multithreading is also a short board for most Java novices. So I'm going to mention a few more questions about multithreading.
How does the ◇synchronized keyword work?
What is the granularity (or scope) of the ◇synchronized? Is it for a class or for an instance of a class object?
Does ◇synchronized have any effect on performance? Why?
What's the ◇volatile keyword sent for? When do you need to use this key word to pinch?

Java Novice's common problem (programming random blog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.