1.Topics that discussed in java1
Declaring a Variable
In Java, all variables must be declared before they can be used. The basic form of a
variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
A.Arrays
-An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Declaring a Variable in an Array
int[] anArray;
Initializing Array
anArray = new int[10];
B.Buffered Reader
Steps to get Input
1. Add this at the top of your code:
import java.io.*;
2. Add this statement:
BufferedReader dataIn = new BufferedReader( new
InputStreamReader( System.in) );
Declare a temporary String variable to get the input, and
invoke the readLine() method to get input from the
keyboard. You have to type it inside a try-catch block.
try{
String temp = dataIn.readLine();
}catch( IOException e ){
System.out.println(“Error in getting input”);
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
● The lines,
indicate that we want to use the classes BufferedReader,
InputStreamReader and IOException which are inside the
java.io package.
● These statements can also be written as,
import java.io.*;
BufferedReader dataIn = new BufferedReader(new InputStreamReader
( System.in) );
String name = "";
declares a String variable with identifier name.
C.JOptionPane
-JOptionPane Contains classes used for a graphical user interface (GUI) Facilitates data entry and data output. class JOptionPane contains methods that display a dialog box. The JOptionPane class provides static methods to display each type of dialog box.To get input from the use we use the code variable=JOptionPane.showInputDialog("Enter the first number");and to display the output we use the code JOptionPane.showMessageDialog(null,variable).
Class JOptionPane
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JOptionPane
D.Control Statements
if
The ifstatement is Java's conditional branch statement. It can be used to route programexecution through two different paths. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed. In no case will both statements be executed. For
example, consider the following:
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Nested ifs
A nested if is an if statement that is the target of another if or else. Nested ifs are very
common in programming. When you nest ifs, the main thing to remember is that an else
statement always refers to the nearest if statement that is within the same block as the
else and that is not already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c;
// associated with this else
}
else a = d;
// this else refers to if(i == 10)
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-
else-if ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;
switch
The switch statement is Java's multiway branch statement. It provides an easy way to
dispatch execution to different parts of your code based on the value of an expression.
As such, it often provides a better alternative than a large series of if-else-if statements.
Here is the general form of a switch statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
while
The while loop is Java's most fundamental looping statement. It repeats a statement or
block while its controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
do-while
As you just saw, if the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all. However, sometimes it is desirable to
execute the body of a while loop at least once, even if the conditional expression is false
to begin with. In other words, there are times when you would like to test the termination
expression at the end of the loop rather than at the beginning. Fortunately, Java supplies
a loop that does just that: the do-while. The do-while loop always executes its body at
least once, because its conditional expression is at the bottom of the loop. Its general
form is
do {
// body of loop
} while (condition);
for
The for loop operates as follows. When the loop first starts, the initialization portion of the
loop is executed. Generally, this is an expression that sets the value of the loop control
variable, which acts as a counter that controls the loop.
As you will see, it is a
powerful and versatile construct. Here is the general form of the for statement:
If only one statement is being repeated, there is no need for the curly braces.
Declaring a Variable
In Java, all variables must be declared before they can be used. The basic form of a
variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
A.Arrays
-An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Declaring a Variable in an Array
int[] anArray;
Initializing Array
anArray = new int[10];
B.Buffered Reader
Steps to get Input
1. Add this at the top of your code:
import java.io.*;
2. Add this statement:
BufferedReader dataIn = new BufferedReader( new
InputStreamReader( System.in) );
Declare a temporary String variable to get the input, and
invoke the readLine() method to get input from the
keyboard. You have to type it inside a try-catch block.
try{
String temp = dataIn.readLine();
}catch( IOException e ){
System.out.println(“Error in getting input”);
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
● The lines,
indicate that we want to use the classes BufferedReader,
InputStreamReader and IOException which are inside the
java.io package.
● These statements can also be written as,
import java.io.*;
BufferedReader dataIn = new BufferedReader(new InputStreamReader
( System.in) );
String name = "";
declares a String variable with identifier name.
C.JOptionPane
-JOptionPane Contains classes used for a graphical user interface (GUI) Facilitates data entry and data output. class JOptionPane contains methods that display a dialog box. The JOptionPane class provides static methods to display each type of dialog box.To get input from the use we use the code variable=JOptionPane.showInputDialog("Enter the first number");and to display the output we use the code JOptionPane.showMessageDialog(null,variable).
Class JOptionPane
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JOptionPane
D.Control Statements
if
The ifstatement is Java's conditional branch statement. It can be used to route programexecution through two different paths. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed. In no case will both statements be executed. For
example, consider the following:
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Nested ifs
A nested if is an if statement that is the target of another if or else. Nested ifs are very
common in programming. When you nest ifs, the main thing to remember is that an else
statement always refers to the nearest if statement that is within the same block as the
else and that is not already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c;
// associated with this else
}
else a = d;
// this else refers to if(i == 10)
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-
else-if ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;
switch
The switch statement is Java's multiway branch statement. It provides an easy way to
dispatch execution to different parts of your code based on the value of an expression.
As such, it often provides a better alternative than a large series of if-else-if statements.
Here is the general form of a switch statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
while
The while loop is Java's most fundamental looping statement. It repeats a statement or
block while its controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
do-while
As you just saw, if the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all. However, sometimes it is desirable to
execute the body of a while loop at least once, even if the conditional expression is false
to begin with. In other words, there are times when you would like to test the termination
expression at the end of the loop rather than at the beginning. Fortunately, Java supplies
a loop that does just that: the do-while. The do-while loop always executes its body at
least once, because its conditional expression is at the bottom of the loop. Its general
form is
do {
// body of loop
} while (condition);
for
The for loop operates as follows. When the loop first starts, the initialization portion of the
loop is executed. Generally, this is an expression that sets the value of the loop control
variable, which acts as a counter that controls the loop.
As you will see, it is a
powerful and versatile construct. Here is the general form of the for statement:
If only one statement is being repeated, there is no need for the curly braces.
