Core Java - Java Basics


When we consider a Java program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and instance variables mean.
  • Object: Objects have states and behaviors. Eg: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
  • Class: A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
  • Methods: A method is basically a behavior. A class can contain many methods.There will be many methods where the logics are written, data is manipulated and all the actions are executed.
  • Instance Variables: Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

  • Now we are going to write first Java Program:

    Let us look at simple code that would print the word Hello World.
        public class SampleProgram{
             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 and add the code as above.
  • Save the file as: SampleProgram.java.
  • Open command prompt window and go to directory where you saved class file. Assume it's C:\.
  • Type ' javac SampleProgram.java ' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line
  • Now, type ' java SampleProgram ' to run your program.
  • You will be able to see ' Hello World ' printed on the window.

  • Output:
    C :> javac SampleProgram.java
    C :> java SampleProgram
    HelloWorld

    Basic Syntax:


    The following points are the rules of java created by java programmer
  • Case Sensitivity: Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
  • Class Names : For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
    Eg: class SampleProgram
  • Method Names : All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be inUpper Case.
    Eg: public void myMethodName()
  • File Name : Name of the program file should exactly same as class name. While saving, the file should save same as class name and append '.java' to the end of the name.
    Eg: Assume 'SampleProgram' is class name, then the file should be saved as'SampleProgram.java'
  • public static void main(String args[]) : Java program processing starts from the main() method, which is a mandatory part of every Java program.

  • Java Identifiers:

    All Java components require names. In java, we have components such as class, methods, variables, interface and etc., for whom we can give the names to components are called as identifiers. In Java, there are several points to remember about identifiers where as:
  • All identifiers should begin with a letter (A to Z (or) a to z), currency character ($) and underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A keyword cannot be used as an identifier.
  • Most importantly Identifiers are case sensitive.
  • Eg using special characters:$salary, _money, __1_rupees
  • Eg using numerical characters: -salary, 95money

  • Java Modifiers:

    In other language, it is used to set the access level for classes, attributes, methods and constructors. We divide modifiers into two groups:
  • Access Modifiers: controls the access level.
    Eg:public,private,default,protected.
  • Non-Access Modifiers: do not control access level, but provides other functionality.
    Eg:final,abstract,static etc.,

  • Java Variables:

    In java, A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type. Variable is a name of memory location. The types of variables in java are:
  • Local Variable(Method level Variable)
  • Global Variable(Class level Variable)
  • There are two types of data types in Java: primitive and non-primitive.

    Java Keywords:

    The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

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

    Java Comments:

    Java supports single-line and multi-line comments very similar to c and c++. All characters available inside any comment are ignored by Java compiler.
    Eg:
        public class SampleProgram{
            // This is an example of single line comment
            
            /* This program is of creating SampleProgram and printing the word Hello World.
            This is called as Multiline comment */
    
            public static void main(String[]args){
                System.out.println("Hello World");
            }
        }
    


    (Core Java - Objects and Class)