About NewTechnoBuzz
Advertise
Contact Us

Java - Basic Syntax

The tutorial will cover the basic entities of Java and its syntax. Before this tutorial, we have gone through the Java features and environment setup. Now, environment has been setup for Java and we can start writing the program in Java.

When we consider a Java program it can be defined as a collection of objects that communicate with each other by invoking each other's methods. Let us look into what is class, object, methods and instance variables mean.
  • Class - A class act as a blue print that describes the behaviors and states of objects of its type.
  • Object - Objects have states and behaviors. The instance variable of an object represent its state and instance methods represent its behavior. Example: A dog has states - color, name, breed (represented by instance variable) and behaviors -wagging, barking, eating (representing by instance methods). An object is an instance of a class.
  • Method - A method is a group of statements that performs some processing. It represent behavior of object. A class can contain many methods. It is the methods where the logic is written, data is manipulated and all the actions are executed.
  • Instance Variable - Each object has its unique set of instance variables. The values assigned to instance variables represent the state of an object.

First Java Program

import com.gsms.console.test;

public class MyTestProgram {

   /* This is my first java program.  
    * This will print 'Hello World' as the output
    */
    public static void main(String []args) {
       System.out.println("Hello World"); // prints Hello World
    }
}
Let's look at how to save the file, compile and run the program. Please follow the steps given below:
  • Open Notepad or any other text editor(like Notepad++ etc.) and add the above code in file.
  • Save the file with name MyTestProgram.java.
  • Open command prompt window and go o the directory where you saved the class. Assume it's D:\gsms\test.
  • Type 'javac MyTestProgram.java' and press Enter to compile your code. If no errors in your code, the it will successfully compiled. (Assumption : The path variable is set).
  • Now, Type 'java MyTestProgram' on console to run your program.
  • You will be able to see 'Hello World' printed on the console window.
D:\gsms\test> javac MyTestProgram.java
D:\gsms\test> java MyTestProgram 
Hello World

Basic Syntax & Points to Remember

Below are some of the points that need to be remember:
  • Case Sensitivity - Java is case sensitive, which means identifier Hello and hello have different meaning in Java.
  • Class Names - For all class names the first letter should be in Upper Case and file name should be same as class name. File name can be different but it will create confusion at later file. So, try to avoid different name for file name. Use camel case naming convention for declaring variable, method and class names. Example class TestClass
  • Method Names - All method names should start with a Lowercase letter. Example public void methodName()
  • Program File Name - The name of file should exactly match the class name. When saving the file, you should save it using the class name.
  • public static void main(String args[]) - The processing starts from the main() method which is a mandatory part of every Java program.

Java Identifiers

The names of class, variable and methods are called Identifier. In Java, there are several points to remember about identifiers. They are as follows:
  • All identifiers should begin with a letter (A-Z or a-z), dollar ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A key word cannot be used as an identifier.
  • Most importantly identifiers are case sensitive.
  • Examples of legal identifiers: age, $employeeAge, _age, __1_discount etc.
  • Examples of illegal identifiers: 123Name, -salary etc.

Java Keywords

The words that we can't use in a Java program as identifier are called Keywords or reserved word. The following list shows the reserved words in Java:

abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
package protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
void while

Comments in Java

Java supports both single-line and multi-line comments. All characters available inside any comment are ignored by Java compiler.
public class MyTestProgram{

   /* This is my first java program.
    * This will print 'Hello World' as the output
    * This is an example of multi-line comments.
    */

    public static void main(String []args){
       // This is an example of single line comment
       /* This is also an example of single line comment. */
       System.out.println("Hello World"); 
    }
} 
This tutorial covers almost all the basics of Java with which a beginner can start. In next tutorial, we'll discuss about the type of variables.

Please feel free to post any comment and feedback to improve the article.


Java Userful Resources

A collection of Java Sites, Books and Articles is given at this page.

Java Programming Books

0 comments