You are on page 1of 8

History of Java Programming Java was started as a project called "Oak" by James Gosling in June 1991.

Gosling's goals were to implement a virtual machine and a language that had a familiar C-like notation but with greater uniformity and simplicity than C/C++. The first public implementation was Java 1.0 in 1995. It made the promise of "Write Once, Run Anywhere", with free runtimes on popular platforms. It was fairly secure and its security was configurable, allowing for network and file access to be limited. The major web browsers soon incorporated it into their standard configurations in a secure "applet" configuration. popular quickly. New versions for large and small platforms (J2EE and J2ME) soon were designed with the advent of "Java 2". Sun has not announced any plans for a "Java 3". In 1997, Sun approached the ISO/IEC JTC1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process. Java remains a proprietary de facto standard that is controlled through the Java Community Process. Sun makes most of its Java implementations available without charge, with revenue being generated by specialized products such as the Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and Runtime Environment (JRE) which is a subset of the SDK, the primary distinction being that in the JRE the compiler is not present. There were five primary goals in the creation of the Java language: 1. It should 2. It should 3. It should 4. It should 5. It should languages. use the object-oriented programming methodology. allow the same program to be executed on multiple operating systems. contain built-in support for using computer networks. be designed to execute code from remote sources securely. be easy to use by selecting what was considered the good parts of other object-oriented

To achieve the goals of networking support and remote code execution, Java programmers sometimes find it necessary to use extensions such as CORBA, Internet Communications Engine, or OSGi. IF Statement All but the most trivial computer programs need to make decisions. They test a condition and operate differently based on the outcome of the test. This is quite common in real life. For instance you stick your hand out the window to test if it's raining. If it is raining then you take an umbrella with you. If it isn't raining then you don't. All programming languages have some form of an if statement that tests conditions. In the previous code you should have tested whether there actually were command line arguments before you tried to use them. Arrays have lengths and you can access that length by referencing the variable arrayname.length You test the length of the args array as follows.

// This is the Hello program in Java class Hello {

public static void main (String args[]) { if (args.length > 0) { System.out.println("Hello " + args[0]); } } } System.out.println(args[0]) was wrapped in a conditional test, if (args.length > 0) { }. The code inside the braces, System.out.println(args[0]), now gets executed if and only if the length of the args array is greater than zero. The arguments to a conditional statement like if must be a boolean value, that is something that evaluates to true or false. Integers are not permissible. In Java numerical greater than and lesser than tests are done with the > and < operators respectively. You can test whether a number is less than or equal to or greater than or equal to another number with the <= and >=operators. Switch Statement Switch statements are shorthands for a certain kind of if statement. It is not uncommon to see a stack of if statements all relate to the same quantity like this: if (x else else else else else == 0) doSomething0(); if (x == 1) doSomething1(); if (x == 2) doSomething2(); if (x == 3) doSomething3(); if (x == 4) doSomething4(); doSomethingElse();

Java has a shorthand for these types of multiple if statements, the switch-case statement. Here's how you'd write the above using a switch-case: switch (x) { case 0: doSomething0(); break; case 1: doSomething1(); break; case 2: doSomething2(); break; case 3: doSomething3(); break; case 4: doSomething4(); break; default: doSomethingElse(); }

In this fragment x must be a variable or expression that can be cast to an int without loss of precision. This means the variable must be or the expression must return an int, byte, short or char. x is compared with the value of each the case statements in succession until one matches. This fragment compares x to literals, but these too could be variables or expressions as long as the variable or result of the expression is an int, byte,short or char. If no cases are matched, the default action is triggered. Once a match is found, all subsequent statements are executed until the end of the switch block is reached or you break out of the block. This can trigger decidedly unexpected behavior. Therefore it is common to include the break statement at the end of each case block. It's good programming practice to put a break after each one unless you explicitly want all subsequent statements to be executed. It's important to remember that the switch statement doesn't end when one case is matched and its action performed. The program then executes all statements that follow in that switch block until specifically told to break.

There may be a sitution when we need to execute a block of code several number of times, and is often referred to as a loop. Java has very flexible three looping mechanisms. You can use one of the following three loops:

while Loop do...while Loop for Loop

As of java 5 the enhanced for loop was introduced. This is mainly used for Arrays. The while Loop: A while loop is a control structure that allows you to repeat a task a certain number of times. Syntax: The syntax of a while loop is: while(Boolean_expression) { //Statements } When executing, if the boolean_expression result is true then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed. Example: public class Test { public static void main(String args[]){ int x= 10;

while( x < 20 ){ System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } } This would produce following result: value value value value value value value value value value of of of of of of of of of of x x x x x x x x x x : : : : : : : : : : 10 11 12 13 14 15 16 17 18 19

The do...while Loop: A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Syntax: The syntax of a do...while loop is: do { //Statements }while(Boolean_expression); Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false. Example: public class Test { public static void main(String args[]){ int x= 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("\n"); }while( x < 20 ); }

} This would produce following result: value value value value value value value value value value of of of of of of of of of of x x x x x x x x x x : : : : : : : : : : 10 11 12 13 14 15 16 17 18 19

The for Loop: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated. Syntax: The syntax of a for loop is: for(initialization; Boolean_expression; update) { //Statements } Here is the flow of control in a for loop: 1. The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. 2. Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop. 3. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression. 4. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step,then Boolean expression). After the Boolean expression is false, the for loop terminates. Example: public class Test { public static void main(String args[]){ for(int x = 10; x < 20; x = x+1){ System.out.print("value of x : " + x );

System.out.print("\n"); } } } This would produce following result: value value value value value value value value value value of of of of of of of of of of x x x x x x x x x x : : : : : : : : : : 10 11 12 13 14 15 16 17 18 19

Enhanced for loop in Java: As of java 5 the enhanced for loop was introduced. This is mainly used for Arrays. Syntax: The syntax of enhanced for loop is: for(declaration : expression) { //Statements }

Declaration . The newly declared block variable, which is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element. Expression . This evaluate to the array you need to loop through. The expression can be an array variable or method call that returns an array.

Example: public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print("\n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); }

} } This would produce following result: 10,20,30,40,50, James,Larry,Tom,Lacy, The break Keyword: The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax: The syntax of a break is a single statement inside any loop: break; Example: public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ if( x == 30 ){ break; } System.out.print( x ); System.out.print("\n"); } } } This would produce following result: 10 20 The continue Keyword: The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression.

Syntax: The syntax of a continue is a single statement inside any loop: continue; Example: public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ if( x == 30 ){ continue; } System.out.print( x ); System.out.print("\n"); } } } This would produce following result: 10 20 40 50

You might also like