JAVA

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Basic Syntaxes Of Java

A Java program can be defined as a collection of objects that communicate by invoking each other's methods. Let us look at a simple java program and digg briefly into basic java concepts.

class HelloWorld
{
	public static void main(String[] args) 
	{
		System.out.println ("Welcome to Hello World program");
	}
}

Class and Objects

A Class groups variables and operations together in coherent modules. A class can have fields, constructors and methods.Objects are instances of classes. When you create an object, that object is of a certain class. The class is like a template (or blueprint) telling how objects of that class should look..

Variables and DataTypes

Computer programs, read data from input devices, processes it and write it to screen, file or network.In a Java program data is kept in variables. Your Java program first declares the variables, then read data into them, execute operations on the variables, and then write the variables somewhere again.

Each variable has a data type. The data type determines what kind of data the variable can contain, and what operations you can execute on it.

Fields

A field is a variable that belongs to a class or an object.

Operations

In Java, Operations are the instructions you can run to process the data in variables.Some operations read and write the values of variables, while other operations controls the flow of program.

Methods

A Java method is a collection of java statements that are grouped together to perform an operation. When you call a method, it actually executes several statements in order. Methods are typically used when you need to group operations together, that you need to be able execute from several different places.

Constructors

Constructors are a special kind of java method that is executed when an object of that class is created. Constructors initializes the objects internal fields.

Interfaces

An interface is a programming structure that enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.In its most common form, an interface is a group of related methods with empty bodies.

Packages

A package is a directory containing Java classes and interfaces. Packages groups related classes and interfaces, thus making modularization of your code easier.

Java Files

All Java code must reside inside a file with the extension .java . For instance, your first java class HelloWorld.java. An application can consist of many such .java files.

Syntax

Lets take a look at an example of .java file.

package corejavaguru;

import java.io.FileReader;

public class MyClass {
	
	protected final String fileName = "/opt/coreJava";
	static {
	    //static class initializer
	}

    public MyClass() {
    	
    }
    
    public static void main(String[] args) {
    	
    }

    public String readFile() {
        //File read statements
    }
}

A typical Java file may contain the following elements:

Package Declaration

The first line in our .java file example shown above is the package declaration.
package corejavaguru;
The package declaration consists of the word package, a space, and then the name of the package the type is to be located in. The .java file should be located in a directory structure that matches the package name. Like for example, if package name is com.abc.xyz then .java file shouuld be in /com/abc/xyz folder.

Import Statements

Next line in our .java file is an import statement.
import java.io.FileReader;
An import statement tells the compiler which other Java files this current Java file is using. There can be multiple import statements in one java file.

Type Declaration

Type can be either a class, an interface, an enum.The type declaration is delimited by a { and a }.
public class MyClass {
}

Fields declaration

A Java field is a variable inside a class.The field declaration ends with a semicolon ;.
protected final String fileName = "/opt/coreJava";

Class initializers

Class initializers begins with a { and ends with a }. Inside this block you can put initialization code that is to be executed when a instance of the class is created. Class initializers can also be static. Then they are executed already when the class is loaded, and only once because the class is only loaded in the Java Virtual Machine once.The keyword static before the block makes the class initializer block static.
static {
//static class initializer
}

Constructors

Constructors are a special kind of java method that is executed when an object of that class is created. Constructors initializes the objects internal fields.Constructors are similar to class initializers, except they can take parameters.
public MyClass() { }

Methods

A method is a group of Java statements that perform some operation on some data, and may or may not return a result.Java methods are where you put the operations on variables in your Java code. In other words, you group Java code inside Java methods. Java methods must be located inside a Java class.Java methods are similar to what is called functions or procedures in other programming languages.
public String readFile() {
//File read statements
}
A method can be static also.A static method belongs to the class, not objects of the class. That means that you can call a static method without having an object of the class the static method belongs to. public static void main(String[] args) {
}