Translate

Sunday, December 29, 2013

KNOW ABOUT JAVA PART II

For previous post read know about Java and know more here
The Logical Operators :
.
Operator----------------------- Description ------------------------------------Example
&&                                ..Called Logical AND operator. If both the operands
                                      ..are non zero then then
                                     ..condition becomes true.                                                           ..(A && B) isfalse.
.
||                                   ..Called Logical OR Operator. If any of the two
                                     ..operands are non zero then then
                                     ..condition becomes true.                                                           ..(A || B) istrue.
.
!                                    ..Called Logical NOT Operator. Use to reverses
                                     ..the logical state of its operand. If
                                     ..a condition is true then Logical NOT operator
                                      ..will make false.                                                                          ..!(A && B) is true.
.
The Assignment Operators :
.
Operator---------------------- Description ------------------------------------------Example
=                                    ..Simple assignment operator, Assigns values
                                      ..from right side operands to left side operand                        ..C = A + B will assigne
                                                                                                                                              ..value of A + B into C
.
+=                                 ..Add AND assignment operator, It adds right
                                     ..operand to the left operand and assign the
                                     ..result to left operand                                                      ..C += A is equivalent to C =C + A
.
-=                                 ..Subtract AND assignment operator, It
                                     ..subtracts right operand from the left
                                     ..operand and assign the result to left operand            ..C -= A is equivalent to C =C - A
.
*=                                 ..Multiply AND assignment operator, It multiplies
                                     ..right operand with the left operand and assign
                                     ..the result to left operand                                               ..C *= A is equivalent to C =C * A
.
/=                                 ..Divide AND assignment operator, It divides
                                     ..left operand with the right operand and assign
                                     ..the result to left operand                                                ..C /= A is equivalent to C =C / A
.
%=                                ..Modulus AND assignment operator, It
                                     ..takes modulus using two operands and
                                     ..assign the result to left operand                                   ..C %= A is equivalent to C= C % A
.
<<=                               ..Left shift AND assignment operator                             ..C <<= 2 is same as C = C<< 2
.
>>=                               ..Right shift AND assignment operator                           ..C >>= 2 is same as C = C>> 2
.
&=                                 ..Bitwise AND assignment operator                                ..C &= 2 is same as C = C &2
.
^=                                  ..bitwise exclusive OR and assignment operator           ..C ^= 2 is same as C = C ^2
.
|=                                   ..bitwise inclusive OR and assignment operator            ..C |= 2 is same as C = C |2
.
Misc Operators
.
There are few other operators supported by Java Language.
.
Conditional Operator ( ? : ) :
.
Conditional operator is also known as the ternary operator. This operator consists of three operands and
is used to evaluate boolean expressions. The goal of the operator is to decide which value should be
assigned to the variable. The operator is written as :
variable x = ( expression) ? value if true : value if false
.
instanceOf Operator :
.
This operator is used only for object reference variables. The operator checks whether the object is of a
particular type(class type or interface type). instanceOf operator is wriiten as:
( Object reference variable ) instanceOf ( class/ interface type )
.
Precedence of Java Operators :
.
Category----------------- Operator---------------------------- Associativity
Postfix                                  ..() [] . (dot operator)                                 ..Left to right
.
Unary                                   .. ++ - - ! ~                                                  ..Right to left
.
Multiplicative                      ..* / %                                                           ..Left to right
.
Additive                               .. + -                                                              ..Left to right
.
Shift                                     .. >> >>> <<                                                   ..Left to right
.
Relational                           .. > >= < <=                                                    ..Left to right
.
Equality                               ..== !=                                                           ..Left to right
.
Bitwise AND                       ..&                                                                  ..Left to right
.
Bitwise XOR                       . ^                                                                   ..Left to right
.
Bitwise OR                         .. |                                                                   ..Left to right
.
Logical AND                      ..&&                                                                 ..Left to right
.
Logical OR                        .. ||                                                                    ..Left to right
.
Conditional                       .. ?:                                                                   ..Right to left
.
Assignment                      .. = += -= *= /= %= >>= <<= &= ^= |=             ..Right to left
.
Comma                 .        .           ,                                                                ..Left to right
.
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
}
.
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 );
.
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
}
.
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
}
.
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.
.
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;
.
The if Statement :
.
An if statement consists of a Boolean expression followed by one or more statements.
.
Syntax :
.
The syntax of an if statement is:
if( Boolean_expression )
{
//Statements will execute if the Boolean expression is true
}
.
The if ... else Statement :
.
An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.
.
Syntax :
.
The syntax of a if...else is:
if( Boolean_expression ){
//Executes when the Boolean expression is true
} else{
//Executes when the Boolean expression is false
}
.
The if ... else if ... else Statement :
.
An if statement can be followed by an optional else if...else statement, which is very usefull to test various
conditions using single if...else if statement.
.
Syntax :
.
The syntax of a if...else is:
if( Boolean_expression 1 ){
//Executes when the Boolean expression 1 is true
} else if(Boolean_expression 2 ){
//Executes when the Boolean expression 2 is true
} else if(Boolean_expression 3 ){
//Executes when the Boolean expression 3 is true
} else {
//Executes when the one of the above condition is true.
}
.
Nested if. .. else Statement :
.
It is always legal to nest if-else statements. When using if , else if , else statements there are few points
to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of he remaining else if's or else's will be tested.
.
Syntax :
.
The syntax for a nested if...else is as follows:
if( Boolean_expression 1 ){
//Executes when the Boolean expression 1 is true
if( Boolean_expression 2 ){
//Executes when the Boolean expression 2 is true
}
}
.
The switch Statement :
.
A switch statement allows a variable to be tested for equality against a list of values. Each value is called
a case, and the variable being switched on is checked for each case.
.
Syntax :
.
The syntax of enhanced for loop is:
switch( expression){
case value :
//Statements
break ; //optional
case value :
//Statements
break ; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
.
Java Methods :
.
A Java method is a collection of statements that are grouped together to perform an operation. When you
call the System.out.println method, for example, the system actually executes several statements in order
to display a message on the console.
In general, a method has the following syntax:
modifier returnValueType methodName ( list of parameters ) {
// Method body;
}
A method definition consists of a method header and a method body. Here are all the parts of a method:
Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the
access type of the method.
.
Return Type: A method may return a value. The returnValueType is the data type of the value the
method returns. Some methods perform the desired operations without returning a value. In this case,
the returnValueType is the keyword void .
.
Method Name: This is the actual name of the method. The method name and the parameter list
together constitute the method signature.
.
Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a method. Parameters are optional; that is, a method may
contain no parameters.
.
Method Body: The method body contains a collection of statements that define what the method does.
.
Java Classes & Objects :
.
Object - Objects have states and behaviors. Example: A dog has states-color, name, breed as well as
behaviors -wagging, barking, eating. An object is an instance of a class.
.
Class - A class can be defined as a template/ blue print that describe the behaviors/states that object
of its type support.
.
A sample of a class is given below:
public class Dog {
String breed ;
int age ;
String color ;
void barking (){
}
void hungry (){
}
void sleeping (){
}
}
A class can contain any of the following variable types.
.
Local variables . variables defined inside methods, constructors or blocks are called local variables. The
variable will be declared and initialized within the method and the variable will be destroyed when the
method has completed.
.
Instance variables . Instance variables are variables within a class but outside any method. These
variables are instantiated when the class is loaded. Instance variables can be accessed from inside any
method, constructor or blocks of that particular class.
.
Class variables . Class variables are variables declared with in a class, outside any method, with the
static keyword.
.
Exceptions Handling :
.
A method catches an exception using a combination of the try and catch keywords. A try/catch block is
placed around the code that might generate an exception. Code within a try/catch block is referred to as
protected code, and the syntax for using try/catch looks like the following:
try
{
//Protected code
} catch (ExceptionName e1 )
{
//Catch block
}
.
Multiple catch Blocks :
.
A try block can be followed by multiple catch blocks. The syntax for multiple catch blocks looks like the
following:
try
{
//Protected code
} catch (ExceptionType1 e1 )
{
//Catch block
} catch (ExceptionType2 e2 )
{
//Catch block
} catch (ExceptionType3 e3 )
{
//Catch block
}
.
The throws /throw Keywords :
.
If a method does not handle a checked exception, the method must declare it using the throws keyword.
The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using
the throw keyword. Try to understand the different in throws and throw keywords.
.
The finally Keyword
.
The finally keyword is used to create a block of code that follows a try block. A finally block of code
always executes, whether or not an exception has occurred.
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter
what happens in the protected code.
.
A finally block appears at the end of the catch blocks and has the following syntax:
try
{
//Protected code
} catch (ExceptionType1 e1 )
{
//Catch block
} catch (ExceptionType2 e2 )
{
//Catch block
} catch (ExceptionType3 e3 )
{
//Catch block
} finally
{
//The finally block always executes.
}
For a complete detail of the Java Programming language, it is recommended to go through our simple
Java Tutorial .

No comments:

Post a Comment

ग़ज़ल

rizw4nkh4n *ख़ुलूस-ए-दिल = purity of heart *एहतराम = आदर, सम्मान *मिज़ाजपुरसी = हाल-चाल पूछना *बाम = घर में सबसे ऊपर का कोठा और छत *इक़राम =...