Search This Blog

If you like any posts in this blog then click this icon which is present under the post
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, 12 October 2011

Creating Objects


                 Creating objects means to allocate memory space for all the instance variables of the objects. Step to create an object are,

1. Declare an object.
2. Define the object using new operator. The new operator dynamically allocates memory for an object and returns a reference to it.
The general form is
1. Declare an object.
2. Define the object using new operator. The new operator dynamically allocates memory for an object and returns a reference to it.

Adding Variables and Methods


Adding variables
                 To a variable in a class it must be defined inside the class. At once a variable is added to a class it becomes instance variable.
Example
                class Student
                {
                                int reg_no;
                                 int mark_1;
                                int mark_2;

Defining a Class


                A class is a user defined data type. It contains data and its related methods. The general form is

class classname
{
     Datatype field-1;
      ……………………….
     Datatype field-n;
     Datatype methodname-1(parameter list)
     {
           Body of the method
      }
     ………………………..
     Datatype methodname-n(parameter list)
     {
          Body of the method
     }
}

Classes and Objects


  1.          Java is a pure object oriented language. 
  2.          So java program contains only classes. 
  3.          Each class has its own data items and functions. 
  4.          The data items are called fields and the functions are called methods.
  5.          A class is a user defined data type and it represents the template of the fields and its related methods. 
  6.          Object is a variable of type class and is used to access the class.

Thursday, 11 August 2011

Labelled Loops


                Java has a facility to give labels to a block of statements.
The general form is

label : Looping statement

Where,
                label                            -  valid java variable name
                looping statement – for,do,while

Continue statement - skipping a part of a loop


               Continue statement is used to skip a part in the body of the loop. When this statement is used inside a loop it skips the execution of the remaining statements in the body of the loop after the keyword continue and continue with the next iteration.

The general form is

continue;

                When this statement is used inside a for loop the control is transferred to the beginning of the loop. If it is used inside while or do loops the control is transferred to the test condition.

Break Statement - Jumping out of a loop


                Break statement is used to exit from a loop while the test condition is true. This statement can be used within a for, while, do-while or switch statement.

The general form is
                                               
break;

                When the break statement is executed inside a loop, the execution of the loop is terminated and the program continues with the statement following the loop. If break statement is used in nested loops, it will exit from the loop containing it.

Friday, 29 July 2011

for looping statement

      for statement is used to execute a statement or a group of statements repeatedly for a known number of times. The general form is

for(variable=initial value;test condition;increment or decrement)
{
             Body of the loop;
}
next statement;

Difference between while and do...while statements


                while is an entry control statement. so if the test condition is false the loop will not be executed.
               
                do ….while is an exit control statement. So irrespective of the condition value, the body of the loop will be executed atleast once. 

do ..... while statement


                The general form is

do
{
      Body of the loop;
}
while(test condition);
next statement;

While statement


This is a simple looping statement. The general form is

while(test condition)
{
        Body of the loop;
}
next statement;

Looping statements


                Looping statements are used to execute a group of statements repeatedly until some condition is satisfied. The looping statements are

                                1. while statement
                                2. do ….. while statement
                                3. for statement

Switch case statement


Switch statement is an extension of if else statement. This permits any number of branches. The general form is

switch(expression)
{
                case label1:
                                statement block-1;
                                break;
                case label2:
                                statement block-2;
                                break;
-----------------------------------------
-----------------------------------------
                case labeln:
                                statement block-n;
                                break;
                default       :
                                default statement;
                                break;
}


Saturday, 23 July 2011

Nested if .... else Statement


                This statement is formed by joining if ….. else statements either in the block or in the else block or both. The general form is

if(test condition-1)
{
    if(test condition-2)
    {
    Statement block-1;
    }
    else
    {
    Statement block-2;

else ........ if Ladder


                else…..if  ladder statement is used to take multi way decision. This statement is formed by joining if ……..else statements in which each else contains another if ….else. The general form is

if(test condition-1)
{
Statement block-1;
}
else if(test condition-2)
{
Statement block-2;
}
-------------------------
-------------------------
else if (test condition-n)
{
Statement block-n;

Friday, 22 July 2011

if ........ else Statement


     if….. else statement is used to  execute one group of statements if the test condition is true or other group if the test condition is false. The general form is

     if(test condition)
     {
           Statement block-1;
     }
     else
     {
          Statement block-2;
     }
     Next statement;

Simple if Statement


     Simple if statement is used to execute or skip one statement or group of statements for a particular condition. The general form is

     If(test condition)
     {
         Statement block;
     }
     Next statement;

Control Structure


DECISION MAKING STATEMENTS

     Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The decision making statements are,

1.       Simple if statements
2.       if……….else statements
3.       else……….if ladder
4.       nested if
5.       switch statement

LOOPING STATEMENTS

Type Conversion in Expressions


AUTOMATIC TYPE CONVERSION

     An expression is defined as a linear combination of operands and operators. If the operands are of different data types (int, float etc) the lower data type operand is automatically converted to the higher data type before execution. This process of conversion is called automatic type conversion. The result is of the higher type.

Example

Let,
     float b=10.7;
     int c=7;
Then b+c=17.7. here int c is converted to float.

The table given below shows the conversion.

Thursday, 21 July 2011

Operators in Java

     An operator is a symbol which represents some operation that can be performed on data. There are eight operators available in java to carry out the operations. They are
1.       Arithmetic operators
2.       Relational operators
3.       Logical operators 
4.       Short hand assignment operators
5.       Increment and decrement operators
6.       Conditional operators
7.       Bitwise operators
8.       Special operators

Followers