HomeJava

10 basic syntax in java you should know

10 basic syntax in java you should know
Like Tweet Pin it Share Share Email

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

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:

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:

2. Syntax in java to declare variables in a class

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:

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

Example without the Access specifier

Example without the Access & Non Access specifier

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:

Example to declare a variable with User defined Java class:

3. Syntax in java to declare a method in a class

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:

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

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:

If you want to import all classes of a particular package, please check below example:

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

 

6. Syntax in java to initialise variables

7. Syntax in java to create a class Object

8. Syntax in java for writing comments

9. Syntax in java for writing conditional constructs

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

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *