Sunday, February 7, 2016

Java Basic Elements

Program Comment


Program comment is part of the program code that will not be executed during program compilation process. This means that the presence of program comment will not affect the course of the program. Program comment is usually used to write information about the program code, for example: creator, version, program description, and others. Program comment also frequently written to write the flow or algorithm of the program.

As explained earlier when we write Hello World program code, in Java, program comments are divided into three. Two of them are adopted from C++ language. Those three comments are as follows:

  1. Comment for one line

    Just as in C++, Java also uses the // sign to make a comment that applies only to a single line of code. Here is an example of its use:

    // this is a Java comment

    If the // sign applied for comment more than one line, it will cause error when compiling the program, as shown in the following code sample:

    // this is
    a Java comment   (FALSE)


    In the code above, the second line will be considered as an unknown identifier that will cause error in the compilation process.

  2. Comment for more than one line

    In Java, comment that has more than one line is made by using the /* and */ sign. Here is an example of its use:

    /* This is one line comment */

    /* This is
    two lines comment */


    As you can see in the example above, the type of comments that use /* and */ can also be applied to one line comment. This comment is a C/C++ comment style.

  3. Comment for documentation purpose

    Besides these two types of the comments above, Java has a special kind of comment that is used for documentation purpose, using /** and */ sign. This type of comment will be used by javadoc program to generate an HTML file. To make the comment, we also need to use special tags that have been provided, for example: @author, @version, @param, and so on. Here is an example of its use:

    /**
    * "Hello World" program
    * @author Aula Muttaqin
    * @version 1.0
    */

Program Block


Java allows us to group one or more statements in a program block. In Java, a program block begins with { and end with }. By the time we write code, we will be a lot of work with program block. In Java, a program block can be applied to the definition of the class, method, loop structure, decision making, and others. Here is an example of making the program block in Java:

//program block in class definition
class ClassSample {   //begin of program block
   ...
}   //end of program block

//program block in loop structure
for (int i=0; i<7; i++) {   //begin of program block
   System.out.println(i);
   System.out.println((i+1));
}   //end of program block

//program block in decision making
if (a < b) {   //begin of program block
   System.out.println("Bigger value: " + b);
}   //end of program block

//program block in method definition
public int getValue() {   //begin of program block
   return value;
}   //end of program block

To a single statement in the loop and decision making block, we actually do not need to use a program block. Consider the following example:
if (p == null) {
   exit;
}

for (int i=0; i<7; i++) {
   System.out.println(i);
}  
The code above can also be written as follows:
if (p == null)
   exit;

for (int i=0; i<7; i++)
   System.out.println(i);


Separator


Separator is used to separate one part of the program with other parts. The most often used separator in any program code is semicolon, which is used to separate statements with each other. Here is the list separator that is contained in the Java:
  1. Braces: { }

    Used to make the program block (class, method, decision making, and loop control) and to fill in the initial value in array declaration.

    Example:
    Class MyClass { ... }
    
    int[] a = {1, 2, 3};
    

  2. Parentheses: ( )

    Used to fill in the list of parameters in the method, to flank an expression in certain operations (e.g. arithmetic operatios), flanking the expression in control statement, and to do typecast.

    Example:
    public int add(int a, int b) {
       return a + b;
    }
    
    int value = (1 * 2) + 3;
    
    if (result = 7) { ... }
    
    for (int i=0; i<3; i++) { ... }
    
    byte b = (byte) value;
    

  3. Bracket: [ ]

    Used to declare an array and to take/fill value of array element.

    Example:
    int[] c = new int[5];
    c[0] = 7;
    

  4. Semicolon: ;

    Used to separate statements.

    Example:
    c = a;
    a = b;
    b = c;
    

  5. Comma: ,

    Used to separate the variables during the declaration process.

    Example:
    int a, b, c;
    

  6. Period: .

    Used to separate the package name, sub package, and class. Also used to separate data or method of an object reference.

    Example:
    import java.io.IOException;
    
    obj.x = 4;
    
    obj.getX();
    


Keyword


Keywords are words that have been defined by the compiler and have a specific meaning and purpose. Java does not allow us to create an identifier (variable name, constant, class, or method) by using keywords. Here is a list of keywords that are contained in Java:

abstractcontinuefornewswitch
assertdefaultgoto*packagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
const*floatnativesuperwhile

* Although provided, the const and goto keywords are not used.

6 comments:

  1. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    best java institute in chennai |
    Core java training in chennai

    ReplyDelete
  2. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete