translate in your language

Tuesday, September 16, 2014

Lexical Issues



(*) Java programs are a collection of  1) whitespace, 
                                                                2) Identifiers, 
                                                                3) literals, 
                                                                4) Comments, 
                                                                5) Operators,   
                                                                6) Separators, and 
                                                                7) Keywords



                                    (*) Whitespace (*)

     @) In Java, whitespace is a space, tab, or newline.

     @) Java is a free-form language. This means that you do not need to follow any 
           special indentation rules. 


           For instance, the Example program could have been written all on one line or 
           in any other strange way you felt like typing it, as long as there was at least one
           whitespace character between each token that was not already delineated by 
           an operator or separator.

USING BLOCKS OF CODE



(*) Java allows two or more statements to be grouped into blocks of code, also called 
      code blocks.This is done by enclosing the statements between opening and 
      closing curly braces.

(*) Once a block of code has been created, it becomes a logical unit that can be used 
      any place that a single statement can.

      For example :-  A block can be target for Java's if and for statements. Consider 
                                 this if statement :

      if ( x < y )   {   // begin a block
            x = y ;
            y = 0 ;
     }  // end of block


     @) Here, if x is less than y, then both statements inside the block will be executed.
           Thus, the 2 statements inside the block form a logical unit, and 1 statement
           cannot execute without the other also executing.

     @) The key point here is that whenever you need to logically link two or more
            statements, you do so by creating a block.



(*) Let's look at another example. The following program uses a block of code as the 
      target of a for loop.

      /*
         Demonstrate a block of code.

         Call this file " BlockTest. java "
     */
      class BlockTest  {
          public static void main ( String args [ ] )    {
             int x,  y ;

             y = 20 ;

       // the target of this loop is a block

       for ( x = 0; x < 10;  x++ )     {
           System.out.println ( " This is x: " + x );
           System.out.println ( " This is y :" + y );
           y = y - 2;
         }
        }
      }


(*) Output :- This is x : 0
                       This is y : 20
                       This is x : 1
                       This is y : 18
                       This is x : 2
                       This is y : 16
                       This is x : 3
                       This is y : 14
                       This is x : 4
                       This is y : 12
                       This is x : 5
                       This is y : 10
                       This is x : 6
                       This is y : 8
                       This is x : 7
                       This is y : 6
                       This is x : 8
                       This is y : 4
                       This is x : 9
                       This is y : 2


     @) In this case, the target of the for loop is a block of code and not just a single 
           statement. Thus, each time the loop iterates, the three statements inside the 
           block will be executed.

Tuesday, September 9, 2014

TWO CONTROL STATEMENTS


*) The if statement :- The Java if statement works much like 
                                                       the IF statement in any other language.

     *) Further, it is syntactically identical to the if statements in C, C++, and C#.
         Its simplest form is shown here :

             if(condition) statement ;

       Here, condition is a Boolean expression. If condition is true, then the 
       statement is executed.If condition is false, then the statements is bypassed. 
       Here is an example:

          if ( num < 100 ) System.out.println ( " num is less than 100");


  *) In this case, if num contains a value that is less than 100, the conditional
      expression is true, and println ( ) will execute.

  *) If num contains a value greater than or equal to 100, then the println ( )
      method is bypassed.

  *) Java defines a full complement of relational operators which may be used in
     a conditional expression. Here are few :



         (*) Notice that the test for equality is the double equal sign.


@) Program that illustrates the if statement :

      /*
         Demonstrate the if .
         Call this file " Ifsample.java".
     */

      class Ifsample  {
           public static void main (String args [ ] ) {
               int x, y ;

      x = 10 ;
      y = 20 ;

      if ( x < y ) System.out.println ( " X is less than Y " ) ;

      x = x * 2 ;
      if ( x == y ) System.out.println ( " X now equal to Y ") ;

      x = x * 2 ;
      if ( x > y )  System.out.println  ( " X now greater than Y " ) ;


      // this won't display anything

     if ( x == y ) System.out.println ( " you won't see this " );

          }
    }

(*) Output :- The output generated by this program is shown here :

                       X is less than Y
                       X now equal to Y
                       X now greater than Y

     Notice one other thing in this program. The line

     int x, y ;

     declares two variables, x and y, by use of a comma-separated list.





(*) The for Loop   :- *) Java supplies a powerful assortment of loop 
                                                 constructs.
                                   

(*) The simplest form of the for loop is 

           for(initialization; condition; iteration) statement;

      @) The initialization portion of the loop sets a loop control  variable to an initial 
            value.
      @) The condition is a Boolean expression that tests the loop control variable.If
            the outcome of that test is true, the for loop continues to iterate.If it is false,
            the loop terminates.
      @) The iteration expression determines how the loop control variable is changed
             each time the loop iterates.

     
      (*) A short program that illustrates the for loop.

           /*
               Demonstrate the for loop.

               Call this file " Fortest.Java".
          */

           class ForTest  {
              public static void main ( String args [ ] )   {
                 int x ;

                 for ( x = 0; x < 10; x = x+1 )
                    System.out.println ( " This is x:= " + x ) ;
               }
            }

        (*) This program generates the following output :

              This is x : 0
              This is x : 1
              This is x : 2
              This is x : 3
              This is x : 4
              This is x : 5
              This is x : 6
              This is x : 7
              This is x : 8
              This is x : 9


     @) In this example, x is the loop control variable.It is initialized to zero in the
           initialization portion of the for.

     @) At the start of each iteration ( including the first one ), the conditional test
           x< 10 is performed.

     @) If the outcome of this test is true, the println ( ) statement is executed, and 
           then the iteration portion of the loop is executed.

     @) This process continues until the conditional test is false.

     @) As a point of interest, in professionally written Java programs you will almost
           never see the iteration portion of the loop written as shown in the preceding
           program.That is, you will seldom see statement like this :

            x = x + 1 ;

      @) The reason is that Java includes a special increment operator which performs
            this operation more efficiently.

      @) The increment operator is ++. ( That is, two plus signs back to back.) The
            increment operator increases its operand by one.

      @) By use of the increment operator, the preceding statement can be written like
            this :

            x ++ ;

       @) Thus, the for in the preceding program will usually be written like this :

              for ( x = 0; x < 10; x++ )

        @) The loop still runs exactly the same as it did before.Java also provides a 
              decrement operator, which is specified as --. This operator decreases its
              operand by one.