Java Programming Syntax

 Syntax is the structure of programming Language it can also be refer to the grammatically of way constructing sentence in programming language. how valid a sentence is been constructed?

Statements in programming language has rules guiding the way it is been constructed. There is a grammatical rule guiding the construction of the statement.
Syntax is the formal grammar of the language, which specifies a well-formed statement the compiler will recognize
Syntax Rules in Java .
Below rules must be adhere to in Java Programming
Case Sensitivity − Java is case sensitive for example
int NAME;
int Name;
which means identifier NAME and Name would have different meaning in Java.
Class Names − The first letter of class name must be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
public class TimedComputarion {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
Example: class TimedComputarion
Method Names − Method names must start with a Lower Case letter
Example: public void myMethodName()
Program File Name − Name of the program file should be the same with the class name.
Example: if your class name is TimedComputarion. the file should be saved as TimedComputarion.java'
public static void main(String args[]) − Java program processing starts from the main() method which is a mandatory part of every Java program.
Semantics Error.
Semantics is about the meaning of the programing statement. Is The Answer to the question right or not.
If programing contains semantics it will run but the result will be wrong, because the statement is not put in the right way.
Example of a Java Programing containing semantics error
/**
* This program find the area of perimeter rectangle
* perimeter= 2(lengnth+breath)
* */

public class UserInfo {

public static void main(String[] args) {
int lengnth, breath, perimeter;

lengnth=12;
breath=30;
perimeter=2* lengnth +breath;
System.out.println(perimeter)

}

}
The above program will be compiled and executed because there is no syntax error, but will not give the correct answer.
It contains Semantics error
perimeter=2* lengnth* breath;
Output
54
wrong
the above expression is wrong
should be
perimeter=2*( lengnth+breath);
Output 84
Correct
Stephen Olubanji Akinpelu

Previous Post Next Post