Saturday, November 5, 2016

Top Common Java Programmer Mistakes

By the end of this Session, you will know the most common mistakes happens by java programmer. These are common mistakes every beginner makes while coding in java programming. It is natural to make mistake by humans,whether you are new to java or expert in coding. you will make more than likely make the same mistakes that others do,over and over again. Let we discuss common java programming mistakes one by one.

1. Comparing two objects(== instead of equals())
 It is important to understand that the equals() method  and == perform two different operations.The equals() method is applicable for only object reference whereas == is a operator applicable for both primitive and object reference. We can not override == operator,but we can override equals() method for content comparison. == operator is used to compare object reference whereas equals() method is used content comparison of String objects.

The fallowing program will shows you when to use equals() method

class Student
{
String name;
int rollno;
Student(String name,int rollno)
{
this.name = name;
this.rollno = rollno;
}
public static void main(String arg[])
{
Student s1 = new Student ("lucky", 101);
Student s2 = new Student ("pavan", 102);
Student s3 = new Student ("mahesh", 102);
System.out.println(s1.equals(s2)); // false
System.out.println(s2.equals(s3)); //false
}

}

Output:





In the above Program Object class .equals() has executed which is meant for reference comparison but based on our requirement it is recommended for override equals() method for content comparison. 

class EqualTest 
{
public static void main(String args[]) 
{
String s1 = "Lucky";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}

}

Output:





2.Missing break statement in the switch constructor

When there are several options and we have to choose only one option from the available ones then we can use switch statement. As a Beginner programmer while writing code about on this topic he might be missing break statement . If we would miss the break statement in the switch block then the output of the program is not as expected.

Now, Let me explain with example to make understand more clear

Example:

class weekdays
{
public static void main(String args[])
{
int days=5;
switch(days)
{
case 1: System.out.println("Monday");
case 2: System.out.println("Tuesday");
case 3: System.out.println("Wednesday");
case 4: System.out.println("Thursday");
case 5: System.out.println("Friday");
case 6: System.out.println("Saturday");
case 7: System.out.println("Sunday");
default: System.out.println("Invalid day");
}
}
}

Output:






In the above program, since days value is 5,we expect that it displays Friday as output. But it displaying all the fallowing days. What might be the reason? For this purpose we use break statement.

One of the uses of break is to terminate a loop and come out of it. Another use of break statement is to come out of the switch block.

Now, let us rewrite the preceding program using break statement

class weekdays
{
public static void main(String args[])
{
int days=5;
switch(days)
{
case 1:System.out.println("Monday");
            break;   
case 2:System.out.println("Tuesday");
            break;
case 3:System.out.println("Wednesday");
            break;
case 4:System.out.println("Thursday");
            break;
case 5:System.out.println("Friday");
           break;
case 6:System.out.println("Saturday");
            break;
case 7:System.out.println("Sunday");
            break;
default: System.out.println("Invalid day");
}
}

}

Output:







3) Non-static variable can not be referenced from static context

A static method is a method that does not act upon instance variables of a class. Static method is declared by using the keyword 'static'. The reason why static methods can not act on instance variables is that the JVM first executes the static methods and then only it creates the objects. Since the objects are not available at the time of calling the static methods,the instance variables also not available.

In the fallowing program,we are trying to read and display the instance variable 'a'of Demo class in static method 'access()'.

Example:
class Demo
{
int a;//instance variable
Demo(int a)
{
this.a=a;
}
static void access()
{
System.out.println("a="+a);

}
}
class Test
{
public static void main(String args[])
{
Demo d=new Demo();

d.access();
}
}

Output:



4. NullPointerException 

NullPointers are the one of the most common errors that java programmer makes. Because compiler will not display any error, it will surface at runtime only. Whenever we are performing any operation on null then JVM automatically thrown NullPointerExcetpion.

Example:
String s=null;
System.out.println(s.length());//NullpointerException

5) capitalization errors

This is one of the most common errors that the java programmer makes mistake. 
In the java API all the methods and variables in begin with all lower case letters. Starts with lower case and every inner words starts with uppercase  this convention is also called camel  case convention.

Example: getName(),getMessage(),toString()

6. Missing closing braces:

As a Beginner Programmer frequently do mistake while writing java program. So must check all opening and closing braces in your program otherwise compiler time error will raised.

Example:
class Human
{
public void eat()
{
System.out.println("humans do eat");
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
//reference is of parent class
Boy b=new Boy();
b.eat();
}
}
Output:




To come out from this error we should check opening and closing braces.

The fallowing program shows correct output:

class Human
{
public void eat()
{
System.out.println("humans do eat");
}
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
//reference is of parent class
Boy b=new Boy();
b.eat();
}
}

Output:


1 comment:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...