Let's explore java

Monday, August 30, 2010

LOOPING STATEMENTS


ITERATION STATEMENTS
As we have studied in the previous programming languageslike in c or c++ that iteration statements consists of for,while,do-while statements and these are also called as looping statements.Exactly same things are applied in java programming language.
                                                 Sometimes we need to execute some code of instruction over again and again till the termination condition is met so in that case iteration statements are used.First of all let us start from for loop
For loop
For loop are used for the iteration statements and its format are as follows
For(initialisation;condition;increment/decrement)
{
Body to be executed
}
First of all it initialises the variable to some constant and then it checks for the condition and then it executes the body and finally variables are incremented or decremented according to the statements.I think one sample program will make you understand in a better way.
Int a;
For(a=0;a<4;a++)
{
System.out.println(“you are now learning core java”);
}
Here this code will be executed 4 times untill the condition is false.
While statement
this is the second type of iteration statement which java programming follows for the looping .the main thing which one has to keep in mind that the body inside the while loop will not be executed atleast once if the condition is false at the bigining itself.the format of the while statement are as follows:-
while(condition)
{
Body to be executed
}
Let’s look into the following code which use while statement
Int i=0;
While(i<=5)
{
System.out.println(“you are so smart buddy”);
I++;
}
Do-while statement
Do-while statement is the another type of looping statements which is slightly different from those two looping statements which have been discussed .Do-while statement executes its body at least once no matter its condition is false at the biggining itself.the body of this is as follows:-
Do
{
Body to be executed;
}
While(condition);
Note:-hello friends!!!!! Here I would like to give one suggetion that whatever we are learning from this blog we should practice it with own because untill and unless we don’t practice it we will not learn.


Saturday, August 28, 2010

SWITCH STATEMENT


SWITCH STATEMENT
The switch statement is a generalisation of the if statement.
The syntax is
switch
statement
The switch statement is a compound statement which specifies alternate course of actions. Each alternative is expressed as a group of one or more statements which are identified by one or more labels called case labels.The main difference between if-else statement and the switch-case statement is that if statements checks  for each and every condition before evaluating the output but it is not the same with switch-case statements. In the switch-case statement case checks for the matching condition which has been given to the switch.switch search the appropriate condition matching with the condition which it contain and when it is found then that statement is evaluated.
One example programs will make you undearstand in the  better way
Class check
{
System.out.println(“enter the catagory”);
{
Char catagory;
Catagory=getchar();
switch(category)
{
case ‘B’ :
printf(“B.TECH Students \n”);
/* B. TECH Processing */
break;
case ‘M’ :
printf(“M.S
c. Student \n”);
/* M.Sc. Processing */
break;
case ‘T’ :
printf(“M.TECH Student \n”);
/* M. TECH Processing */
break;
case ‘P’ :
printf(“Ph. D. Student \n”);
/* Ph. D. Processing */
break;
default :
printf(“ERROR\n”);
/* Error Processing */
}
}


CONTROL STATEMENTS

CONTROL STATEMENTS
Now let us dive into the world of control statements where we will have some fun with these statements.java supports two types of control statements:if and switch.these statements allows us to have control over our program executions based on the condition known only during runtime.
IF
As we have seen the uses of if statements in the previous class it follows thw same rule here in java also.If statements are use whenever we have many condition and we have to follow any one not all.the formate of IF statements are as follows:
If(condition)
{
Expression
}
Else
Expression;
Here in we will see that we give one condition to the if statement and if that condition happens to be true then the expression inside that if statement will be executed but if that condition is not true than the expression containig else will be executed.
The example program will make you understand in a better way.so here is the example program
class a
{
Public static void main(string args[])
{
Int p;
P=2;
If(p==2)
System.out.println(“this statement is executed”);
Else
System.out.println(“this statement is executed again”);
}
}
NESTED IF
The elaborate type of if statement is called as nested if.it is very frequent in programming.whenever a program contains two or many if statement continuously then it is said to be as nested if.One has to keep in mind that in nested if statement else statement alwas refers to the nearest if .let’s have a look of one sample program
Class a
{
Public static void main(string args[])
{
Int b,g,j;
If(b==2)
If(g==b)
If(j!=0)
System.out.println(“you are very good boy”);
else
System.out.println(“you are a good boy”);
else
System.out.println(“you are just ok”);
}
}
Ihope you have understood what I wanted to convey to you through this program.
IF-ELSE-IF LADDER
This is the special type of statement in which if –else statements are used as the ladder and execute step by step.here each if statemen t is associated with one stetement and the statements are executed if the condition satisfies but if the condition is not true then again control refers to some other if statement and checks for the condition and it goes on.one has to keep in mind that if whenever the condition satisfies then all other if-else statement is bypassed if all the condition is false then the dafault value is evaluated.one
Example programm will make you understand in a better way.
Class statement
{
Public static void main(string srgs[])
{
Int I,a,b;
If(i==0)
{
System.out.println(“the sum of a and b =”+(a+b));
)else if(i==1)
{
System.out.println(“the diffrence of a nad b is=”+(a-b));
}else if(i==2)
{
System.out.println(“the multiplication of a and b =”+(a*b));
}
}
}
Note:-dear friends please let me know the feedback from your side so that I can know the further requirements






Thursday, August 26, 2010

SOME MORE IN OPERATORS


RELATIONAL OPERATORS
Today we will discuss about the relations that one operand has with other.the outcome of the relational operations are always in boolean.some of the relational operators are as follows.
Operators            results
==                        equal to
!=                         not equal to
>                    Greater than
<                       less than
>=                   greater then or equal to
<=                   less than or equal to
As stated earlier that the output of relational expressions are always in boolean so here is the examples.
Int a=4;
Int b=1;
Boolean c=a<b;
In this example the result of a<b(which is false) is stored in c.
NOTE:- By now I am sure that you will be curious to make your own programs with the help of these relational operators,so in hoping that we are moving to the next topic.
LOGICAL OPERATOR
These operators  compare the one operands to the other in order to give the exact output.some of the logical operators are as follows.
Operators                    results
&                                              logical and
|                                               logical or
^                                              logical xor
||                                             short-circuit OR
&&                                             short-circuit  AND
!                                                 logical unary  NOR
&=                                          AND assignment
|=                                             OR assignment
^=                                           XOR assignment
==                                             equal to
!=                                             not equal to

I hope that you must be knowing the uses of these logical operators and if I am wrong then please refer to some good books which can make you understand the uses of these operators.I would like to give you the examples of how these operators are being used.The logical operators ! inverts the boolean states: !true==false, !false==true.HERE is ine example programs which shows the uses of these operators so let us have a glance at that program.
Class  a
{
Public static void main(string args[])
{
Boolean a=true;
Boolean b=false;
Bolean c=a|b;
Boolean d=a&b;
Boolean e=a^b;
Boolean f=(a&b)|(a&!b);
System.out println(“a=:”+a);
System.out println(“b=:”+b);
System.out println(“c=:”+c);
System.out println(“d=:”+d);
System.out println(“e=:”+e);
System.out println(“f=:”+f);
}
}
Short-circuit logical operators
Java provides two interesting boolean operators not found in many other languages,these are secoundry verson of boolean AND and OR operators and are known as short-circuit logical operators.Till now what we have studied that OR  operator results in true if a is true no matter what the values b contains and same is the case with AND operators results in false if a is false no matter wht b has the value.But in java it not the case with short-circuit logical operators as the expression evaluates both of the terms(left hamd and right hand) and then it gives the output based on both of the ststements.
ASSIGNMENT OPERATORS
The assignment operator has its use in assigning some values into the variables.the format of the assignment operator is as follows.
Variable=value constant
In this expression value constant is being assigned to the  variable.It should be kept in mind that the opposite is not possible and it will give an error if we try to do so.
Int c=45; //it can be used
45=I;// an illigal statement.
THE ? OPERATOR
Java includes the special ternary operators that can replace the certain types of if then else statements.those if-else statements can be evaluted within one line using ?(ternary operators).The format is as follows
Expression1?expression 2:expression 3;
This expression will be evaluated as follows.
If the first expression is true than the expression just following It will be as output but if the firat expression is false then the expression 3 will be evaluated.One example program will make you  understand in the best possible way.
Class  ternary
{
Public static void main(string args[])
{
Int I,k;
I=10;
K=i<o?-i:I;
System.out.println(“the value of I =”+k);
}
}
And here comes the end of operaters in java.buddy !!!!!!1 I need your suggestions how to improve the efficiency so please go through my blogs and do comment on it if possible.







         
  
                         

Tuesday, August 24, 2010

OPERATORS AT A GLANCE IN JAVA


TODAY LAT’S EXPLORE THE OPERATORS IN JAVA
Operators literally means the one which operates on some operands to give output. These operators can be divided into four    groups:Arithmatic,bitwise,relational,and logical.I hope that you are well aware of the difinitions of these operators in previous classes .
Some of the examples of the arithmatic operators are as follows.
Operators                     result
+                                     Addition
_                            Subtraction
*                                Multiplication
  /                                 Division
%                                  Modulus
++                                    Increment
+=                                Addition assignment
-=                                 Subtraction assignment
*=                                  Multiplication assignment
/=                                    Division assignment
%=                               modulus assignment
__                                     Decrement   
I hope that you must be knowing the various use of these operators but then  to have a little glance over it it’s necessary to look upon these operators with the help of one simple program.
Class basicmath
{
Public static void main(string args[])
{
int a=1+1;
int b=a*3;
int c=b/4;
int d=c-a;
int e=-d;
System.out.println(“a=”+a);
System.out.println(“b=”+b);
System.out.println(“c=”+c);
System.out.println(“d=”+d);
System.out.println(“e=”+e);
}
}
You can use any type of data types insteed of int whatever you want.
MODULUS OPERATOR
The modulus operator returns the remainder of the division operations. It can be used into floatin-point and integer as well.The program using modulus operator are as follows.
Class modulus
{
Public static void main(string args[])
{
Int x=42;
Double y=42.25;
System.out.println(“x%10 =”+x%10);
System.out.println(“y%10=”+y%10);
}
}

ARITHMATIC COMPOUND ASSIGNMENT OPERATORS
As we have seen in some of the programs above that every now and then we used   (a=a+10),   b=b-10,   c=c*10;    d=d/10;
Now for more convinience for the programmer to save time and memory space as well compound assignments operators have been introduced.
As for example
a=a+10 can now be written as  a+=10;
b=b-10 can now be written as c-=10;
c=c*10 can now be written as c*=10;
note:- hello buddy what are you looking for???no no now I won’t be giving you the example programs ,you have to try yourself with these compond assignments operators.
INCREMENT AND DECREMENT OPERATORS
In java programming ++ and – are refer to as increment and decrement operators respectively.the increment operators increases the variables by one(1),and just reverse of it the decrement operators decrement the variables by one(1).But these operators can be use differently at different situations like :-
Suppose in one program we do the following:-
Class buddy
{
Public static void main(string args[])
{
Int i=4;
Int j=7;
I++;
System.out.println(“the value of I =”+i);
J--;
System.out.println(“the value of j =”+j);
}
}
Here the output will be as follows
the value of I =5;
the value of j =6;
but watch the 2nd program carefully.you will find the difference.
Class incdre
{
Public static void main(string args[])
{
int i=4;
int j=7;
++i;
System.out.println(‘the value of i=”+i);
--j;
System.out.println(“the value of j=”+j);
}
}
When you run this both program in your system then you will find the two uses of increment and decrement operators.then too for your convienience I should tell you that   i++ first increment the variable by one then it print the output but    --i first print the output then it increment the variables, and same goes with the decrement operators;

Sunday, August 22, 2010

SOME MORE IN JAVA


Now it’s the time to concentrate upon VARIABLES with best of our abilities as it is very very essential part of any programming language.In other words variables can also be called as basic unit of storage in the programming language.Variables are the combination of a type ,identifier,and optional initializer.
DECLARING A VARIABLE
The formate of declaring a variable is as follows
TYPE  IDENTIFIER  =[VALUE],IDENTIFIER=[VALUE]……..[VALUE];
Here type can be of atomic type and it can also be of class type.Identifiers are the names of the variables.
Example
Int a,b,c;
Int d,e=10,f;
Byte c;
Double pi=3.1416;
Char x=’x’;
DYNAMIC INITIALIZATION OF VARIABLES
Some times variables can also be initialize at run time i.e dynamically.
Ex…..      double e=math.sqrt(a*a+b*b);
Here variable e which is of double type are initialize at the run time and contains the value .
THE SCOPE AND LIFE TIME OF THE VARIABLES
In any program block starts with the opening of curly brace and ends with the closing of curly brace. A block defines the scope, so each time you create blocks you creates a scope
                  In previous classes what we studied about scope in other programming language that there are of two types of scopes i.e GLOBAL and LOCAL.but in java these does not fit well.Java has the types of scopes that are  defined by class and 2nd scopes are defined by methodes. Declaring scope inside the scope localising the veriable inside the scope and preventing it from unauthorised access from outside the scope.
                                   NESTED SCOPE
The object declared in the outer scope will be visible to the inner scope,however the reverse is not true.
It will be  better if we know this concept by example program
Class scope
{
Public static void main(string args[])
{
Int x=10;  //known to all code within main
If(x==10)  //start new scope
{
Int y=20;  // known only to this scope
System.out.println(“x and y”+x+” “+y);
X=y*2;
Y=100; //error:y is not known here
System.out.println(“x is” +x);
}
}
Now we know that variables are declared when their scopes are entered than it’s also necessary to that variables looses its values outside the scope.
Again I would like to explain it with an example
Class lifetime
{
Public static void main(string args[])
{
Int x;
For(x=0;x<3;x++)
{
Y=-1;
System.out.println(“the value of y is:”+y);
Y=100;
System.out.println(“the value of y is”+y);
}
}
}
TYPECASTING
As in previous programming language what we studied that there is a facilities of typecasting  i.e converson of one data type to another data type.when the two types are compatible with one another then the conversion takes place emplicitely,but if both the data types are not compatible with one another then also we can convert one data types to another explicitely.now for example we can conver int to long but not double to byte.
Java automatic conversion takes place due to two reason
1>the two types are compatible
2>The destination type is larger than the source type
If we want to convert int type to byte so here is the format for that
B=(byte)I;
ARRAY
As studied earlier array is the list of like-typed variables.
Int month_days[];
It establish the fact that month_days is an array veriable,no actual array exist till now.To link month_days with an actual array new will be asigned to the variable
Formats are as follows
Int month_days[];
Month_days=new type[12];
Now bidding you by for today in hoping that you all understood it in a very good manner.if not than feel free to ask me.