Thursday, July 24, 2014

Continue statement

continue ;andcontinue Lablename ;

1) used to break regular flow of control.
2) used to perform a early part of iteration. 

If the continue is used in the innermost for loop then control will reach first at the increment / decrement part of for loop and then condition part of for loop.
For example: 

for(int i=0; i<=5 ; i++)
{
    if(i%2 == 0)
   {
       continue;
   }
   else
  {
       System.out.println("i=" + i);
   }
}

Output: i=1
i=3
i=5

In this code when compiler reach at the continue statement then it breaks the regular flow and transfer the control to the i++ statement in the for loop and after incrementing the value of i it get transfered to the i<=5 statement.
Now we will discuss the role of continue with lablename.

For example: 

lbl : for(int i=0; i<2 ; i++)
{
    for(int j=0; j<3 ; j++)
   {
    if(i==j)
       continue lbl;
    System.out.println("j=" + j);
   }
       System.out.println("i=" + i);
}

Output: j=0

If we want to move the control to the outer loop then we use the lablename . lablename is user-defined . In this case we have used the lable lbl right befor the outer for loop.

How to run a Java Program ?

 There are 3 steps to run a java program .


STEP 1 : Save the java program by the any name and with .java extension. 
            For example A.java. 

STEP 2 : Now next step is to compile the source code file i.e. the .java file.
             Open the command prompt and set the path where the java program              has been saved (by cd command). 
             To compile the source file, type as
             javac filename 
             
             For example , 
             javac A.java 
             
             After pressing enter a class file get generated (with .class                            extension) , like in above example source file A.java get compiled                  into a new class file A.class .

STEP 3 : To Run a java program we type a command as,
             java name of the class containing the main() method

             For example , 
             java myclass 

             After pressing enter, execution of its main() method starts and keep              running till all the code in main() method is finished.

In java we can save the source file by any name. The new class file generates of the class which contain the main() method. And in java we run the .class file so thats why during running the file we run it by the class name containing the main() method.  
             
             

Friday, July 18, 2014

Simple Java Program

Java Program

Java is case sensitive language. we make classes in java program . All code reside in classes. Access specifiers and qualifiers are optional. 
Here is a simple java program.


class A
{
      public static void main(String a[])
{
     System.out.println("java programming");
}
} 

now we will explain this program:

class A : A is the name of the class, this is user defined , we can give any                    name to the class. 

A java program can contain more than one classes.

public : public is the access specifier. Access specifier maintain the visibility of            the data member or member functions.

static : static is the qualifier. We declare the class as the static so that an                object get created. So if we want to invoke a class's method without              creating the object, then declare that class as static. 

void : This is the return type of the method.

main : This is the name of the method. Execution of the program starts from             the main method.

String : This is the predefined class. Any predefined class's first letter will be               in capital letter.

a[] :  This is the array instance of String class. This is used to recieve                      command line parameter. Command line parameters passed at the                runtime. always separated by spaces.

System : This is the predefined class.

out : This is the object of the System class.

println : This method is used to display string or any type of constant &                     content of variable on the screen.
parul

About Java

Java 

Java is a portable language. It is platform independent language. It favours "Write once run everywhere" rule. It is very secure and robust. Java has the following features.

1) Object oriented
2) Secure
3) Robust
4) Interpreted
5) Distributed
6) Applet
7) Multi-threading
8) Dynamic
9) Architectural Neutral
10) Portable