JavaRush /Java Blog /Random EN /The most common problems of those who start learning Java...

The most common problems of those who start learning Java

Published in the Random EN group
Level of knowledge required to understand the article: beginner. You are in the process of studying the Java Syntax quest, and closer to its initial levels. The most common problems of those who start learning Java - 1You've just started learning Java. You are bombarded with terms as if from a cornucopia. You are loaded with tons of technical information, and sometimes you get confused in three pines. Believe me, since you have already gotten involved in programming, IT WILL ALWAYS BE SO (at this moment, an ominous laugh sounds off-screen, and a quiet voice, cut off mid-sentence, whispers “Run... save yourself before it’s too late...”). Bugs will haunt you for the rest of your programming life, even if you are the best developer. However, this is not at all as scary as it sounds.
If you have just started learning JavaRush, then most likely you have already encountered the problems raised in the article. In any case, we would welcome comments describing the problems you had in your first months of learning Java.

Level 0 problem: Confusion with the comparison operator (==) and assignment operator (=).

This is a very common problem for those who switch from other programming languages. And in general, since elementary school we have become accustomed to the fact that (=)this is an equal sign. A simple trick that can be used to remember is this: if you know that you only care about one of the values ​​(you assign one to the other) you use a single " =" sign, otherwise if you care about all the variables and values ​​(you compare them ) you need a double " ==" sign.

Problem: Using the comparison operator (==) to test string equivalence

What to do: Remember, no Java objects can be checked for equivalence using(==) , since this operator is designed to test equality of primitive types.
The most common problems of those who start learning Java - 2
For objects you need to use the .equals(). And Stringin the Java language it is nothing more than an object (and immutable).
An object marked as Immutable is an object whose state cannot be changed after creation. In Java , this means that all instance fields of a class are marked as finaland are primitives (or also immutable).

Problem: String concatenation in a loop

Don't try this yourself! Each time strings are concatenated, a new object is created String(as we found out above, strings immutableare immutable). At some point you will run out of memory or the program will start taking too much time to complete. Solution. Use StringBuilderinstead Stringwhen concatenating content in a loop. The class StringBuilderis used when you need to make a lot of changes to a character string. And such lines, unlike String, are not immutable.

Problem: NullPointerException

This kind of exception occurs when we try to use a null pointer nullwhen in fact we need a real object.
All primitive types have default values. So, y intis 0, and y booleanis false. The default value of any reference types, that is, objects, is null. It's not an object or a type, it's a special value. Which essentially means “there is nothing here.”
The most common problems of those who start learning Java - 3
What to do: If your code is written in such a way that at some point your object can accept the value null, you need to add a “check for null”, especially if you are not creating the object yourself. Also remember that it is better to return empty collections than a set of null's. It would also be nice to add validation for getters and setters.

Problem: for some reason the if / while / for loop does not work

if (something.equals («что-то еще»)); {// <---- корень зла!!!
     System.out.println («Почему я не работаю???»);
}
What's wrong here? It's simple: an extra semicolon has spoiled the blood of hundreds of thousands of novice Java programmers. Well, you don't need it after if (). From a language point of view, such syntax is quite acceptable, so the compiler will not complain about it. But it terminates the conditional statement, and the code inside the block {}will never satisfy the condition above.
The most common problems of those who start learning Java - 4
Solution: Remember the design logic. The “ ;” sign means the end of a certain block, but here everything is just beginning.

Problem: Trying to access non-static member variables from static methods (such as main)

Solution . This problem is related to the understanding of the concept itself static. If a method is marked with this word, it means that we do not need to instantiate the class to call the main method. So the solution is to have a good understanding of “statics” and understand their essence.

Problem: Trying to reinvent the wheel by developing already existing libraries and tools

Solution. It's actually not that much of a problem. For a beginner, this is more of a useful exercise. However, if you want to focus on the result, for example, creating a convenient subscriber database, then searching for a subscriber in the phone book is just one of the results of your application. In this case, there is no need to write a binary search algorithm. Use an existing one (we assure you, someone has already written it before you and it is in the standard libraries). So study the Java standard libraries in the Oracle documentation. Learn to Google, get used to reading Stack Overflow and ask questions on help JavaRush ! ...Of course, these are not all the problems of beginners. Describe yours in the comments! Are you interested in reading Java related articles? Join the Java Developer group . There's a lot of interesting stuff here.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION