Wednesday, March 5, 2014

Top 10 Basic Coding mistakes in java

Coding Tips for Java Developer
In this post, we'll see the most basic coding mistake made by Java freshers or who started learning java. It is important for Java fresher to remember these common mistake as such mistake can comes in an interview. If you know such mistakes, it save time understanding the flow of the code before answering the question.  



Putting Capital Letters Where They Belong
Java is a case-sensitive language, so you really have to be careful. Here are some things to keep in mind as you create Java programs:
  •  Java’s keywords are all completely lowercase. For instance, in a Java for statement, the word for can’t be For or FOr.
  • -You also need to make sure that the names you make up yourself are capitalized the       same way throughout your entire program. If you declare a myAccount variable, you can’t     refer to it as MyAccount, myaccount, or Myaccount. If you capitalize the variable name two    different ways, Java thinks you’re referring to two completely different variables
  • When you use names from the Java API, the case of the names has to match what appears in the API.



Forget to Break Out of a switch Statement.
If you don’t break out of a switch statement, you get fall-through. For instance, if the value of i is 2, the following code prints all three lines.

Comparing Values with a == Sign
When you compare two values with one another, you use a double equal sign. The line
if (inputNumber == randomNumber)
is correct, but the line
if (inputNumber = randomNumber) is not


Comparing two objects with == instead of equals
When we use ==  basically we comparing two object reference to see whether they point to the same object. We can't compare two string content with ==, we must use equals() method instead. Check this post more understanding

Forget to Define the Required Constructors
When you define a constructor with parameters, as in
public Temperature(double number)
then the compiler no longer creates a default parameter less constructor for you. In other words, you can no longer call
Temperature roomTemp = new Temperature();
unless you explicitly define your own parameter less Temperature constructor. Click here for more detail

Accessing Non-Static References from static Context
If you try to compile the following code, you get an error message. You get an error message because main is static, but blogName isn’t static. Click here for more detail

Staying within Bounds in an Array
When you declare an array with ten components, the components have indices 0 through 9. In other words, if you declare
int guests[] = new int[10];
then you can refer to the guests array’s components by writing guests[0], guests[1], and so on, all the way up to guests[9]. You can’t write guests[10], because the guests array has no component with index 10

Anticipating Null Pointers
Null pointers are one of the most common errors that Java programmers make. Compilers can't check this one for you - it will only come at runtime. Generally it comes when either you haven't initialized the object or you haven't checked the return value of a function.

Helping Java Find Its Files
If you don’t have your code in the appropriate directories, you get a repulsive and disgusting NoClassDefFoundError. Believe me, this error is never fun to get. When you see this error, you don’t have any clues to help you figure out where the missing class is or where the compiler expects to find it. If you stay calm, you can figure out all this stuff on your own. If you panic, you’ll be poking around for hours.

To tame this problem, Java defines some-thing called a CLASSPATH. The CLASSPATH is a list of places where the compiler and the JVM look for code. There are several ways to set a CLASSPATH. Some programmers create a new CLASSPATH each time they run a Java program. Others create a system-wide CLASSPATH variable.

One way or another, the compiler and the JVM need a list of places to look for code. Without such a list, these Java tools don’t look anywhere.

Access Modifiers for Java Classes
A class can be either public or nonpublic. If you see something like
public class NonPublicClassAccess  you’re looking at the declaration of a public class. 
If a class is public, you can refer to the class from anywhere in your code

But, if you see plain old
class Test  the class that’s being declared isn’t public.
If a class is not public, you can refer to the class only from code within the class’s package.

Confusion between pass by value of and pass by reference
When you pass a primitive data type, such as a char, int, float, or double, to a function then you are passing by value. That means that a copy of the data type is duplicated, and passed to the function. If the function chooses to modify that value, it will be modifying the copy only.

When you pass a Java object, such as an array, a vector, or a string, to a function then you are passing by reference. Yes - a String is actually an object, not a primitive data type.  So that means that if you pass an object to a function, you are passing a reference to it, not a duplicate. Any changes you make to the object's member variables will be permanent - which can be either good or bad, depending on whether this was what you intended.




If you know anyone who has started learning java, why not help them out! Just share this post with them. 
Thanks for studying today!...

No comments:

Post a Comment