Web Tutor

Other Entries

Java Syntax

Every programming language has a syntax. Java syntax refers to a set of rules that describes how a java program should be written and interpreted. The syntax is mostly based on C and C++. Unlike C++, in java, there are no global functions or variables, but there are data members that are also considered global variables.
 
Java Basic Syntax
 
Identifiers In Java
These are the basic building blocks of a java program. In Java, we use identifiers to give names to different parts of a program such as variables, objects, classes, methods, arrays, etc.
 
Just like other programming languages, Java has rules for naming identifiers. Below are the rules:
  1. Identifiers can contain alphabets, digits, and some special characters such as underscore(_) and dollar sign($).
  2. A java Identifier can contain a reserved word or keyword but cannot be a keyword. Some of the keywords in java are true, false, while, etc.
  3. Identifiers must not begin with a digit.
  4. Identifiers can be of any length.
  5. Java is case sensitive so uppercase and lowercase letters are treated differently.
 
Keywords in Java
 
Keywords are reserved words in java that convey a different meaning to the compiler. Keywords in Java are used for a special purpose. There are many keywords in Java. Some of them are default, final, case, this, private, public,
return, continue, break, do, for, while, new, package, interface, implements, extends, finally, int, short, long, char, byte, boolean, abstract, etc.
 
 
Simple Java Program
 
We are going to start with the simplest Java program. Using this example, we will understand the basic concepts of Java Syntax and then take a closer look at them. I will be using Eclipse IDE for Java Developers. You can download it from the link  here 
Here is a simple Java Program
 
package FirstProgram;

public class firstProgram {

	public static void main(String[] args) {
		System.out.println("Hello world");

	}

}

This program prints "Hello World" to the console.

Now let's take this program line by line, but omit some details that are not necessary for a beginner.

public class firstProgram

Each java program is a class or more often many classes. The line above means that we create a new class and name it firstProgram. A class is a kind of template or blueprint, it describes the behavior and state of the class' objects. It could be tough for novice programmers, you will learn this concept a little bit later. For now, the class firstProgram is just the beginning of your first program.

A pair of curly braces {} denotes a block, a group of programming statements that are treated as one single unit. Where { means the beginning of the unit and } means the end of the unit. Blocks can be nested within each other or they can be sequential.

There are two nested blocks in the above programs. The external one contains the body of the firstProgram class, the inner one contains the body of the main() method. 

public static void main(String[] args)

Here is the beginning of the main method. A method is a behavior or a sequence of commands that allow you to perform an operation in a program. For example, add two numbers or output a string. In other words, a method is a function. Methods, just like all elements of a java program, are located within a class. Each class can have one, many, or no methods.

public is an access modifier. A variable, method, or class marked with a public modifier can be accessed from anywhere in the program.

Void is the return type of the method. Void means that it doesn't return any value.

main represents the starting point of the program. This is the name of the method.

String [] args is the main method argument. Almost every Java program has the main method.

Static methods are ones to work with the class. Methods that use the static keyword in their declaration can only work directly with local and static variables.

System.out.println("Hello world");

Formally, this line executes the println method of the out object. The out object is declared in the OutputStream class and statically in the system class. The line above prints "Hello World" on the screen.

So if you run the program in the eclipse, you will get an output as follows:

450_441ef74a7d645dbed4e202ca2336ea10.png



Posted in Java on September 09 2022 at 08:52 AM

Comments (0)

No login

Popular Blogs