A Java program is nothing but a collection of objects that co-operate and communicate with each other to its data and behaviour which are nothing but variables and methods.
So in order to write a Java program, we need to first understand basic syntax in Java which consists of how do you write code and what are its meaning conventions and what does they represent. Here, first of all, Java is a case-sensitive language which means it treats uppercase and lowercase letters differently.
Basic Syntax In Java
Case-sensitivity:
Java is case sensitive, which means identifier Car and car are consider different and have different meanings in Java.
Class Names:
for naming a class, the first letter should thus be in upper case, if there are many words that are use to form the class name, each inner word present in the main class name’s first letter should be in upper case. Every single piece of code that you write should be in class. Example: class MyNewJavaClass
Method names:
for naming a method, the first letter should be in upper case, if there are many words that are use to form the method name, then each inner word present in the method name’s first letter should be in upper case. Example: public int myMethodName
Program file name:
Name of the program file should exactly match the class name.
Java Identifiers:
The name that you give to all the classes, methods, variables and packages are nothing but identifiers. These identifiers should satisfy basic syntax in java where the criteria of the naming convention which are as follows:
- These identifiers should start from _(underscore) or any upper case(A-Z) or lowercase(a-z)
- Any of the keywords cannot be used as an identifier which are in term considered to have a predefined meaning.
- Example: _sneha, Swaroopa
Any identifier violating the rules of the naming convention is termed as illegal identifiers.
Java Keywords: Keywords are the words which have certain predefined meaning in Java programming language. One cannot change the meaning of these keywords.
Whitespaces: There are certain spaces that we write in the language. For a character to be a white space character should satisfy one of the conditions i.e either it should be a line separator or it should be a paragraph separator or it needs to have some space.
Comments:
We write comments in our program for our better understanding and proper documentation of the code. Comments are nothing but better explanatory or information of a program, they explain what the program does, why is this program? What is the background that is running behind the program, what we have created a particular variable and it can have an explanation regarding the creation of methods and its uses? The comment section is always ignored by the compiler, they are for user information only.
Single line comments: These comments are written using two forward-slash (//).
Multi-line comments: These comments are written using forward slash pointing with an asterisk(/*), which needs to be closed at the end of the comment.
It is good practice to write comments when you write a program.
main() method:
In a Java program, the main() method is basically executed first. In the program, the class name which matches with the file name must have the main() method inside it.
System.out.println() : This statement helps us print any statement into the console.
Here is a basic Java program that prints good night
public class MyNewCode{// this is a sample program// this program prints goodnight at the end.// let’s write the main methodPublic static void main(String[] args){System.out.println(“good night”);}} // program ends here |
In a Java program, the number of opening flower brackets must be the same as the number of closing brackets else it returns in compilation error.
Written By: ESHWARI SWAROOPA DEVI A
Reviewed By: M SASANK SAI, Bujagawni Sai Teja Goud
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs