new operator and methods.15

27
http:// improvejava.b logspot.in/ 1 new operator and methods

Post on 22-Oct-2014

380 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: New operator and methods.15

http://improvejava.blogspot.in/

1

new operator and methods

Page 2: New operator and methods.15

2

Objective

On completion of this period, you would be able to know

• new operator• Methods

http://improvejava.blogspot.in/

Page 3: New operator and methods.15

3

Recap

Class : • is blue print or plan of an object• class consists both methods and instance variables• syntax of class declaration :

class class_name {

instance variables;

methods;

}

Object :

is an instance of a class

http://improvejava.blogspot.in/

3

Page 4: New operator and methods.15

4

new Operator

• You know how to declare a primitive variable and assign it a value

eg. : int x = 10;• What about non-primitive variables? i.e. what about objects?• When you create a class, you are creating a new data type

• You can use class to declare objects of that type

syntax of class declaration :

class class-name {

instance variables;

methods;

}

Example :

class Student {

int sno, m1, m2, m3;

void total_mks() {….}

}

http://improvejava.blogspot.in/

4

Page 5: New operator and methods.15

new operator

5

• Creating objects of a class is a two-step process

Step 1

• Declare a variable of the class type.

• eg. : Student s;

Step 2

• Allot the required memory and initialize its data members

• You can do this using the ‘new’ operator

• eg. s = new Student();

http://improvejava.blogspot.in/

null

sno

m2

m1s

s

Student object

Fig. 14.1 Creating object

m3

Page 6: New operator and methods.15

Use of new Operator

6

• The new operator dynamically allocates memory for an object

and returns a reference to it

• This reference is the address in memory of the object allocated

by new. This reference is stored in the variable

• Thus, in Java all objects will be dynamically allocated

http://improvejava.blogspot.in/

Page 7: New operator and methods.15

7

Another Way Of Creating Object

• In the preceding example,

Student s; // declares a reference to object

s = new Student(); // allocates a Student object

• By combining above two statements ,we can also write

Student s = new Student(); // this is same as above

• Object reference is similar to pointers, but the difference is you cannot manipulate references as you can do with pointers

http://improvejava.blogspot.in/

Page 8: New operator and methods.15

Closer Look At ‘new’

8

• ‘new’ operator dynamically allocates memory for an object

• The general form of it is

ClassName objVar = new ClassName();• here, objVar is a variable of the class type being created

• ClassName is the name of the class that is being instantiated

• ClassName followed by parenthesis ‘( ) ‘ specifies the constructor

of the class

http://improvejava.blogspot.in/

Page 9: New operator and methods.15

Advantages Of ‘new’

• Program can create as many or as few objects as it needs during the execution of your program

• No memory wastage

• No insufficient memory message (unless total memory is used )

9http://improvejava.blogspot.in/

Page 10: New operator and methods.15

• Classes usually consisting of two things

• Instance variables

• Methods

• Instance variables : these are the variables declared inside

a class

eg.: class A {

int x; // x is an instance variable

}10

Introduction To Methods

http://improvejava.blogspot.in/

Page 11: New operator and methods.15

• Methods are functions declared in a class• A method will perform a specified task related to the class

eg. : class A {

int x; // x is an instance variable

void display( ) {//display is a method to print value of x

System.out.println(“value of x is”+x);

}

}

11

Introduction To Methods

9CM604.15

11http://improvejava.blogspot.in/

Page 12: New operator and methods.15

The general form of a method

12

General Form Of A Method

http://improvejava.blogspot.in/

type method-name([parameter-list]) {

// body of method

} // end of method

eg. : int add(int x, int y)) { int res; res = x + y; return(res); } // end of method

Page 13: New operator and methods.15

13

• A method should return a value

• The type of value returned is called ‘return type’ of the method

• If the method does not return a value, its return type must be void

• Name of the method must be a legal identifier

• Parameter-list is a sequence of type and identifier pairs separated by

commas

• Parameters are essentially variables that receive the value of the

arguments passed to the method when it is called

General Form Of A Method contd..

http://improvejava.blogspot.in/

Page 14: New operator and methods.15

14

General Form Of A Method contd..

•If the method has no parameter, then the parameter list will be

empty

• Method that have a return type other than void return a value to the

calling routine using the following form of the return statement

return x;

Here, ‘x’ is the value returned

http://improvejava.blogspot.in/

Page 15: New operator and methods.15

15

Adding A Method To Class• Methods to do specified task are defined by the class

eg. :

class Student {

int sno, m1, m2, m3;

void total_mks() {

int total_mks;

total_mks = m1 + m2 + m3;

System.out.println(“total marks of the student”+total-mks);

}// end of method

} // end of class

• total_mks() is a method

• Its task is to find the total of m1,m2 and m3http://

improvejava.blogspot.in/

Page 16: New operator and methods.15

class UseStudent {

public static void main(String args[]) {

Student s = new Student();

s.m1 = 99; s.m2 = 80; s.m3 = 35;

s.total_mks();

}// end of method

} // end of class

16

Calling A Method

http://improvejava.blogspot.in/

Page 17: New operator and methods.15

Calling A Method contd..• Look closely at the following line

s.total_mks();

• The above statement invokes the total_mks() method on ‘s’

i.e it calls total_mks() relative to the ‘s’ object,

using the object name followed by the dot(.) operator

17

Dot operator is used to access methods in class

http://improvejava.blogspot.in/

Page 18: New operator and methods.15

Calling A Method contd..

• When we call s.total_mks() JVM transfers control to the code defined inside the class

• After execution of the above method, control return back to the calling routine

9CM604.15

18http://improvejava.blogspot.in/

Page 19: New operator and methods.15

Calling A Method contd..

• A method is always invoked relative to some object

of its class

• Thus, within a method, there is no need to specify the

object a second time

19http://improvejava.blogspot.in/

Page 20: New operator and methods.15

Discussion

• Classify Methods based on return type and parameter passing• Methods that have no return type, no parameters

• Methods that have no return type, but have parameters

• Methods that have return type, but no parameters

• Methods that have both return type, parameters

9CM604.15

2020http://improvejava.blogspot.in/

Page 21: New operator and methods.15

Summary

In this class, you have learnt • ‘new’ operator• Usage of new operator• Methods

9CM604.15

2121http://improvejava.blogspot.in/

Page 22: New operator and methods.15

1. Objects memory is created

a. statically

b. dynamically

c. none

22

Quiz

http://improvejava.blogspot.in/

22

Page 23: New operator and methods.15

2. What is new?

a. operator

b. method

c. variable

d. none

23

Quiz contd..

9CM604.15

23http://improvejava.blogspot.in/

Page 24: New operator and methods.15

3. What is reference?

a. variable

b. kind of pointer

c. object

d. none

24

Quiz contd..

http://improvejava.blogspot.in/

24

Page 25: New operator and methods.15

4 . What operator is used to access

a. @

b. ^

c. *

d. .(dot)

25

Quiz contd..

9CM604.15

25http://improvejava.blogspot.in/

Page 26: New operator and methods.15

Explain the syntax of a method What is .(dot) operator? What is the use of it? Explain the mechanism of invoking a method in

Java?

26

Frequently Asked Questions

http://improvejava.blogspot.in/

26

Page 27: New operator and methods.15

1. Write a class called Student consisting of

pin_no, sname, fname, city as instance variables, get_stu_details and disp_stu_details as methods

Create an object for the above class, and access both methods to do the above operations

2. Write a class called Employee consisting of

eno, ename, desgination, tot_salary as instance variables, get_emp_details and disp_emp_details as methods. Create an object for the above class, and access both methods to do the above operations

27

Assignment

http://improvejava.blogspot.in/