www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Java Classes vs C++ Classes Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
In C++, when we declare a variable whose type is a class, storage is allocated for an object of that class, and the class's constructor function is called to initialize that instance o f the class. On the other hand, in Java when we declare a variable whose type is a class, we really declare a pointer or reference to a class object; no storage is allocated for the class object, and no constructor function is called unless we use the "new" keyword. This is elaborated with the help of the following example:

Let us assume we have the following class:

class MyClass {
// class definition goes here
}

Let us first look at C++

MyClass m; // m is an object of type MyClass
// the constructor function is called to initialize M.
MyClass *pm; // pm is a pointer to an object of type MyClass
// no object exists yet, no constructor function has been called
pm = new MyClass; // now storage for an object of MyClass has been
// allocated and the constructor function has been called


Now the same thing in Java

MyClass m; // pm is a pointer to an object of type MyClass
// no object exists yet, no constructor function has been called
m = new MyClass(); // now storage for an object of MyClass has been
// allocated and the constructor function has been called. Note
// that you must use parentheses even when you are not
// passing any arguments to the constructor function
// Also note that there is a simple ‘.’ (dot) operator used to
// access members or send message. Java does not use -> operator.

Next >>> Lesson No. 16: Parameter Passing in Java







Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.