Java - Basic Syntax


Java program  can be defined as a collection of objects that communicate by invoking each other's methods. Let's take a look into class, object, methods and instance variables mean.
Variables : Contains some kind of data , variables might be your name or your age or anything you want.
Methods : A method is basically a function. it does stuff with those variables . a method is simply a sort of following instructions that can make a specific treatment.
Object : An Object include both variables and methods.
Class : a Class is a sort of template that describes the methods and variables that will be in each object.
First Java Program:
This is  simple code that will print  "Hello World" message .
public class MyFirstProgram {

   /* 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"); // print Hello World
    }
} 

Basic Syntax:

Case Sensitivity : Java is case sensitive, which means it makes the  difference between uppercase and lowercase syntax.example "String" and "string"are different in java.
Class Names : The first letter in class name should be in UpperCase           Example class Myclass
Method Names : All method names should start with a Lower Case letter.
Example public void myMethod()
Program File Name : Name of the program file should be the same class .
Example: 'MyProgram' is the class name. Then the file should be saved                       as 'MyProgram.java'

public static void main(String args[]) : This method is usually found in any java program, it is the main method that every program start processing from it .

Java Variables:

  • Local Variables
  • Class Variables (static variables)
  • Instance Variables (Non-static variables)

Java Arrays:

Arrays are objects that store multiple variables of the same type. We will look into how to declare, construct and initialize in the upcoming chapters.

Comments in Java

Comments in java are similar to cpp and c all characters available inside any comment are ignored by Java compiler. 
public class MyFirstProgram{

   /* 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"); 
    }
} 

Share this

Random Posts

Previous
Next Post »