Hi, I'm really new to Java and object based programming so forgive me if I sound clueless here because I probably am! When I am constructing a point using specific parameters, for example, Point p1 = new Point (2,14); am I storing data in p1 or am i just creating a reference to p1 as I would with ()? Thanx for any assistance.
When you are writing Point p1= new Point(2,14) you are doin two things: - You are declaring a variable p1 of type Point, saying that p1 can take the refrence of object or type Point or its base classes(Object in this case) - when you are writing new Point(2,14), you are creating a new object of type Point, allocating memory to it, which will return u some reference of the newly created Point object, that you are directing to p1 , by writing p1= new Point();
I recommend that you take a look at Bruce Eckels book The first chapter describes what objects represent very well.
Joined: Aug 25, 2003
See the code of Point class will be soemthing like this:
Now, when you do new Point(1,2), this will create a new Object for you. That means a new momory space that will have say 8bytes of memory(4 bytes for eac int)(This is dummy information to make clear!!).
Now JVM have some object in memory that is of type Point. Now, this object i saved somewhere in memory and that somewhere's ref is with p1, cause you have written: p1= new Point(1,2)
I think i will make more clear. Read the first explaination again.
Joined: Sep 16, 2004
OK, I am reading the first chapter of the book, thanx for the site, I think I will be needing the rest of the chapters! I've also gone back and read everything written on the topic. I am also very grateful today that I am an IS major and not a CS major because I would be in a lot of trouble! I have total respect for all of you and truely appreciate your time.
I think I am beginning to understand the concepts but it is slow going. The word "data" is actually what is hanging me up.
The question I am actually trying to answer is:
What does the following code do?
Point p1 = new Point(2,14);
I have narrowed the answer to either
Constructs a Point object at location(2,14) and stores the data in p1. or Constructs a Point object at location(2,14) and stores the reference in p1.
Before I posted, my answer was the first choice. Now I'm not so sure. I'll go read that chapter!