You have to follow strict syntax of Java, only then Java Compiler can understand and compile the program properly. In this tutorial you will learn the basic syntax that you need to know to write java programs
There will be several keywords & terminology used as part of java syntax, you can first give a read and understand what are those for getting clear understanding of the syntax.
Before we get started in to more details, there are 2 things which are very primitive to be aware of
- java is case sensitive, so you should be very careful when writing java programs.
- Every statement / instruction ends with a semicolon, except for flow constructs
1. Syntax in java to create a class
class { }
In the above syntax, there are three parts to be considered:
- access specifier
- keyword “class” will indicate java compiler that a new class is getting defined
- class name – the name you want to give for your class
Access specifier is the part which determines the accessibility of a class member within a class or an inner class or class in same package or classes in another package.
List of access specifiers:
S.NO | ACCESS SPECIFIER |
---|---|
1 | private |
2 | public |
3 | protected |
Example to declare a class in java:
public class DemoClass { // code goes here }
In the above example, public is one of the access specifiers and DemoClass is the name of the actual class that is being defined.
Now that you know how to define or create a class, you should also know how to save this file, so when you save a class file in java, it should be saved with the name of the class which has access specifier as public and the file should have an extension as “.java” so for the above example, you need to save the class file name as DemoClass.java
You should also know that access specifier is not mandatory when you define a class, we can even declare classes without giving any access specifier and java compiler can still accept it. But the only thing you should understand is that providing access specifier will decide the places in which the class can be accessed.
So you can also define a class as below without access specifier:
class DemoClass { }
2. Syntax in java to declare variables in a class
class < Class Name > { < Access specifier > < Non Access specifier > < Data Type > < Variable Name > ; }
As per the above syntax you can declare a variable in a java class by providing 4 parts in a sequence, and the four parts are:
- Access specifier.
- Non access specifier.
- Data type.
- Variable name.
a. Access specifier is the part which determines the accessibility of a class member within a class or an inner class or class in same package or classes in another package, and the list of access specifiers are mentioned in the earlier section.
b. Non Access specifier is the part which determines the behaviour of a variable it is associated to.
List of non access specifiers:
S.No | Non Access specifier |
---|---|
1 | static |
2 | final |
3 | volatile |
4 | transient |
5 | abstract |
6 | synchronized |
c. Data Type is the part which specifies the type of java supported variable you want to declare. There are 3 types of data types as below:
- Primitive Data Types.
- Java Specific Data Types – Java predefined classes.
- User defined Data Types – User Defined classes.
List of primitive Data Types :
S.No | Primitive Data Type |
---|---|
1 | int |
2 | boolean |
3 | long |
4 | double |
5 | char |
6 | byte |
7 | short |
8 | float |
There is a huge list of Java predefined classes that you can use, just to state a few we have String, Integer, Boolean, Double, HashMap, List, ArrayList, etc..
User defined Data Types are the classes that you create on your own tfor your project specific, and can be declared as variables i.e. Objects.
d. Variable Name is the part which you want to use in your class or project and which holds data.
Now knowing all of the above details, you can use different combinations of values to declare a variable in java, only thing you need to take care is the order in which you write them.
For example:
class DeclareVariables { public static int rowCount; }
In the above example:
- public is the access specifier.
- static is the non access specifier.
- int is the data type
- rowCount is the variable name.
Lastly, you should also know that the first two parts are non mandatory / optional and can be used without having them in the variable declaration statements.
Example without the Non Access specifier
class DeclareVariables { public int rowCount; }
Example without the Access specifier
class DeclareVariables { static int rowCount; }
Example without the Access & Non Access specifier
class DeclareVariables { int rowCount; }
The above examples are all specified using the primitive data types, similarly you can declare variables for java predefined classes or user defined classes.
Example to declare a variable with Java predefined class:
class DeclareVariables { //String is a predefined Java Class public static String userName; }
Example to declare a variable with User defined Java class:
// User Defined Class class FileOperations { //code goes here } //User defined class class DeclareVariables { public static String userName; //declaring a variable for a user defined class public FileOperations fo; }
3. Syntax in java to declare a method in a class
class ClassName { //declaring a method in a class < access specifier > < non access specifier > < return type > < function Name > ( < list of parameters separated in comma > ) { // function body goes here } }
In the above syntax we have 5 parts to declare a method in a java class, namely:
- access specifier – covered in earlier sections of this article
- non access specifier – covered in earlier sections of this article
- return type – it is the type of data you want to return can be any primitive data types or predefined classes in java or any user defined classes you might have created for the project
- function name – name of the function you want to give for the re usable functionality
- list of parameters separated by comma – list of input parameters that are required to perform the business logic written in the function body
for example:
class ClassName { //declaring a method in a class public static String concatenate ( String rs[] ) { // function body goes here } }
In the above example:
- public is the access specifier
- static is the non access specifier
- return type is the return type, and String is one of the predefined class in Java. String can hold array of characters
- concatenate is the method name, the name with which you would want to call this functionality at different other places of your program
- String rs[] is one input parameter to this method, this parameter holds an array of Strings. Function will use this input and perform the operation based on the code written in function body.
4. Syntax in java to include other libraries and packages in your class
//--------- //importing external libraries using import command and the hierarchy of package structure //--------- import ...... class { pubic static void main(String rs[]) { ; } }
In the above syntax you see that there are 2 parts.
1. The keyword used to import any package is import
2. The path of packages that you want to include, each child package is separated with a “.”, this is continued till the final class is reached or all the classes in a package.
Examples
If you want to import a single class “File” of java.io package, please check below example:
//--------- //importing external libraries using import command and the hierarchy of package structure //--------- // io is child package in java, File is a class defined in io package. import java.io.File; class FileOperations { pubic static void main(String rs[]) { //Now you will be able to access class File File f; } }
If you want to import all classes of a particular package, please check below example:
//--------- //importing external libraries using import command and the hierarchy of package structure //--------- // this syntax means you have imported all classes of io package. import java.io.*; class FileOperations { pubic static void main(String rs[]) { // You will be able to access all classes in java.io package. File f; BufferedInputStream bis; } }
In case you want to know the list of classes in java.io package, you can refer to this link java.io.*
5. Syntax in java to call a method
class { public static void main(String rs[]) { = new (); //Calling a method on an object of class .(); } }
6. Syntax in java to initialise variables
class { public int = ; }
7. Syntax in java to create a class Object
class { public static void main ( String rs[] ) { // Create object for a class using new keyword = new (); } }
8. Syntax in java for writing comments
class { // Single line comments /* Multi line Comments Comment line 2 Comment Line 3 */ }
9. Syntax in java for writing conditional constructs
class { public static void main ( String rs[] ) { //simple if condition if() { //body of steps are executed when is pass } //simple if else condition if() { //body of steps are executed when is pass } else { //body of steps are executed when is not passed } // if , else if , else condition if() { //body of steps are executed when is passed } else if() { //body of steps are executed when is not passed and is passed, if is pass, this block of code is never executed. } else { //body of steps are executed when & are not passed, if or is passed, this block of code is never executed. } //nested if condition if() { //body of steps are executed when is passed if() { //body of steps are executed when & are passed } else { //body of steps are executed when is pass & is not passed } } switch() //holds a case selection variable { case : //body of steps are executed when the value of the case variable is break; case : //body of steps are executed when the value of the case variable is break; case : //body of steps are executed when the value of the case variable is break; default: //body of steps are executed when the value of the case variable is not matched with any of the cases above. } } }
In the above syntax you can see what are the various conditional structures that can be used in java, which are if, if else, if else if if, nested if and switch case. Each conditional construct has a body of instructions, which would be executed based on the condition pass or failure.
10. Syntax in java for Looping Constructs
class { pubic static void main(String rs[]) { for(;;<increment/decrement/etc>) { } while() { } do { }while() } }