Syntax errors -- Errors in spelling and grammar.
1 You can use the compiler or interpreter to uncover syntax errors.
example==>>
public class stringmethod{
public static void main(String[] args){
String string1 = "Hi";
String string2 = new String("Hello")\\ forgot to put semicolon
if (string1 == string2) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}
}
}
2 You must have a good working knowledge of error messages to discover the cause of the error.
Logical errors -- Errors that indicate the logic used when coding the program failed to solve the problem.
1You do not get error messages with logic errors.i.e. computer will not warn you.
2Your only clue to the existence of logic errors is the production of wrong solutions.
Example==>> You want to got to theater. Now there are two ways to reach there way 'A' and way 'B' one is shor cut and one is little long. According to your idea you will pick up way 'A' which is long. you will reach to your destination but your idea is not right. This is logical error.
To be a good programmer this is essential to think in right direction.
System.exit
Terminates the currently running Java Virtual Machine.
a nonzero status code indicates abnormal termination.
It might be thought that a programme that reads in two user entered integers and prints out their sum would be a simple piece of code with little of real interest. This assumption is wrong, once the programmer wishes to ensure that errors are detected and also wants to handle the user input channel in a reasonably general fashion. Java provides all these facilities. Unfortunately for those who like simple "proof-of-concept" programmes, the Java programmer has to do it properly - or not at all.
Here's a Java programme that prompts the user to enter integer values and prints out the sum of the integer values.
public class Addup
{
static public void main(String args[])
{
InputStreamReader stdin =
new InputStreamReader(System.in);
BufferedReader console =
new BufferedReader(stdin);
int i1 = 0,i2 = 0;
String s1,s2;
try
{
System.out.print("Enter first number ");
s1 = console.readLine();
i1 = Integer.parseInt(s1);
System.out.print("Enter second number ");
s2 = console.readLine();
i2 = Integer.parseInt(s2);
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}
catch(NumberFormatException nfex)
{
System.out.println("\"" +
nfex.getMessage() +
"\" is not numeric");
System.exit(1);
}
System.out.println(i1 + " + " +
i2 + " = " + (i1+i2));
System.exit(0);
}
}
The System.exit method forces termination of all threads in the Java virtual machine. This can be drastic. It might, for example, destroy all windows created by the interpreter without giving the user a chance to record or even read their contents.
Programs should usually terminate by stopping all non-daemon threads; in the simplest case of a command-line program, this is as easy as returning from the main method.
System.exit should be reserved for a catastrophic error exit, or for cases when a program is intended for use as a utility in a command script that may depend on the program's exit code.
import :is used to import java packages.
/**util is java package and date is class. Without util date related method will not work and it will give copilation error
that is syntax error*/
/*This comment line shows what work this this part of programme is doing and it will be used for reference pupose*/
import java.util.Date;
public class Message2 {
public static void main(String []args) {
// Make sure the command
// line argument was passed in
if(null == args || args.length < 1) {
// Command line argument
// doesn't exist, so exit with
// an error flag
System.exit(1);
}
// Extract the only command line
// argument from the first element
// of the array
String message = args[0];
String formattedMessage = prependTimestamp(message);
System.out.println(formattedMessage);
}
public static String prependTimestamp(String str) {
Date now = new Date();
String timeStamp = now.toString();
return timeStamp + ": " + str;
}
}
Semicolon if you will not put where
if(x==y) is condition after that statement will come.if you put semicolon it will give you syntax eror its a java syntax rule
you need to learn not only java but real logic and programming from scratch