Sunday, 9 April 2017

Core Java - 1/10


This blog will help to refresh Core Java Knowledge .

Hadoop Framework was designed using Java . This is because it is a prerequisite for Hadoop Learning.


In this blog we will take useful keywords which will help in your Hadoop Journey. 

Before starting lets learn the naming convention used in Java :


1. Class name & Interface name should start with Upper Case.


2. Method name & Variable name should start with Lower Case.


If the name of the Class,Interface,Method or Variable will be combination of two words then second word should start with Capital Letter . 

Example :  sumCalculator is a Method Name .


1. Break :

This will break the current loop . Lets take the below example 

public class FoodMarket {
   public static void main(String args[]) {
      String [] fruits = {"Apple","Orange","Pineapple"};
      for(String f : fruits ) {

         if( f == "Pineapple"){
            break;
            }
         System.out.print( f );
         System.out.print("\n");
         }

          // After the break statement control will come here.                              
   }
}

Output :
Apple
Orange 


As we see from the above example :

1. We have created a fruits array of String type of length 3 ( we can find that using fruits.length method)
2.for(String f : fruits)  here f is a String variable . f will hold 3 values as fruits array is of 3.
3.First loop f is holding Apple ( f=Apple) in first loop then it compares f with "Pineapple" as condition false so it will ignore the if block and proceed for System.out.println statements which returns print the value of f .
 4.Second loop f is holding Orange ( f=Orange) in second loop then it compares f with "Pineapple" as condition false so it will ignore the if block and proceed for System.out.println statements which returns print the value of f .
5.Third/last loop f is holding Pineapple ( f=Pineapple) in third loop then it compares f with "Pineapple" as condition true so it will  execute the if block , in the if block there is break; so it will go out of current loop i.e for(String f : fruits )

2. Continue :

The difference between Break and Continue is that Continue will break the block and continue from the current loop.

Lets take the below example 

public class FoodMarket {
   public static void main(String args[]) {
      String [] fruits = {"Apple","Orange","Pineapple"};

      for(String f : fruits ) {

             if( f == "Orange"){

            continue;                       // This block will skip and continue  for loop with                                                       // next value
            }

         System.out.print( f );
         System.out.print("\n");

          }

      }
}

Output :

Apple
Pineapple

Explanation : As soon as it see the continue statement in the block only that block will be skipped . And it will continue loop with the next value. 


Next class we will look into Object and Class




No comments:

Post a Comment