javabymanish

Search results

Thursday, 31 October 2013

Chapter - 5 (Java Class and Programming Structures)

Class : -  The class is a logical construct upon which the entire java language is built because it defines the nature of an object. Class is defines a new data type. Once defined, this new type can be used to create objects of that type.Thus, a class is a template for an object and an object is an instance of a class. A class is declared by the use of the class keyword. The general form of a class definition is shown here :

                  class <class-name> {
                         type <instance-variable 1>;                         
                         type <instance-variable 2>;
                         //.........................................;
                         type <instance-variable n>;

                         type methodname 1(parameter-list) {
                             // Body of the method
                          }
                         type methodname 2(parameter-list) {
                                // Body of the method
                          }
                          //......................................
                        
                         type methodname n(parameter-list) {
                                // Body of the method
                          }
                 }

 The data or variables defined within class are called instance variable. The methods and variables defined within a class are called member of the class.

                             class Abc
                                 {
                                      int a;
                                      int b;
                                 }
 Here a and b are instance variables and Abc is a new data type.

Declaring Objects:- 

Declaring objects of a class is a two step process as follows :

1 - You declare a class type variable that refer to an object.

                           Abc a;
                          
2 - In second step you must acquire an actual, physical copy of the object and assign it to that variable with the help of  new operator. The new operator dynamically allocates memory for an object and returns a reference to it.

                                        Abc a = new Abc();

Variables :-

Variables are locations in the memory that can hold values. Before assigning any value to the variable, it must be declared. The content of the memory may be change during the execution of the program.

Types Of Variables :-

1 - Instance Variable :- Variables declared inside the class scope are known as instance variable.

                                                   class Abc
                                                    {
                                                          int x, y;
                                                    }
2 - Local Variable :- These variables are declared inside methods or constructors.

                                        class Abc {
                                           void show() {
                                                 int x=10;
                                               }
                                          }
3 - class variable :- These variables are global to a class and to all instances of the class. The keyword static is used to declare a class variable.

Data Type :-
                                                                           
     


                                                               

Control Statement :- 

A programming language uses control statements to cause the flow of execution to advance and brand based on changes to the state of a program. In java, there are three categories of control statements :
selection, iteration, jump.

Selection Statements :- Java supports two selection statements : if and switch.
1 - 
             if(condition)
              {
                 statement 1;
                ...................
                ...................
                statement n;  
              }
            else
              {
                statement 1;
                ...................
                ...................
                statement n; 
               }

 2 - 
        switch(variable_name)
         {
           case exp1:
                             statements;
                             break;
           case exp2:
                             statements;
                             break;
           ...........................
           ...........................    
           case expn:
                             statements;
                             break;
           default :
                         statements;
                             }

Looping Constructs: - A loop causes a selection of a program to repeated a certain number of times. There are three types of loops : -
1 - while loop :-
                               while(boolean_exp)
                                  {
                                       statements;
                                  }
2 - do-while loop :- 
                                   do
                                     {
                                        statements;
                                     }while(boolean_exp);
3 - for loop:-
                       for(initialization; condition; iteration)
                         {
                            statements;
                         }

 Arrays :- 

Array is representation of data in contiguous space in the memory of your computer. Indexing of an array begins with zero(0). All the variables of an array are same data type. There are three types of array :-

1 - One-Dimensional Array : -  
                                         data_type [] variable_name;
                                         int [] a;
2 - Two-Dimensional Array :- 
                                     data_type [][] variable_name;
                                     int [][] a;
3 - Multidimensional Array :- More than two-dimensional array called multidimensional array.

To create an array in the memory, use the following statement : -

                                        a = new int[3][3];


A simple program in java :-

                            class Abc
                              {
                                 public static void main(String []args)
                                  {
                                     System.out.println("My First Java Program.");
                                   }
                               }

Explanation :-  In java java.lang,  package is by default imported, package , container of the classes and interfaces(discuss later). Every java program must inherited a class named as Object, root class of all java classes.
Here class is a keyword and Abc, name of class. public, is an access specifier, static is a keyword(discuss later), void means no return type and main, is the name of function or method. String, is a class defined in java.lang package and System, is also a class defined in java.lang package and System.out is used output on console and println, is a method defined in java.io package used for print the output.

 
 
 
                         

                          

1 comment:

  1. its A very Good for students sir thank you so much for creating this blog..........

    ReplyDelete