aspose file tools*
The moose likes Beginning Java and the fly likes Data v. Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data v. Reference" Watch "Data v. Reference" New topic
Author

Data v. Reference

Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
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.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Hi Tricia,

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 think this will clear.

Regards
Sandeep Jindal


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
So, if I understand this correctly, the values 2 and 14 for the x-y coordinates are not considered data. They are just references to a point at that location?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I recommend that you take a look at Bruce Eckels book The first chapter describes what objects represent very well.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Hi,

Not exactly.

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.

Regards
Sandeep
Tricia Lemay
Greenhorn

Joined: Sep 16, 2004
Posts: 15
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!

Thanx again for your help,

Tricia
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

What the code does it it create a point object and stores a reference to that object in the variable p1.

The point object created contains the following data: x = 2, y = 14.

The data is the values contained in the objects; the variables are references to those objects.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
In your line of code, a whole bunch of things happen.

First, new Point creates an instance of the Point class (with other words, an object of type Point). It reserves some memory to store x and y values.



Then the call to the constructor sets x and y to the specified values (and yes, you can consider that to be data):



The declaration Point p1 creates a reference of type point in memory.



Last but noz least, the assignment lets p1 reference the newly created object:



(Keep in mind that this is a rough conceptual model of what happens - in reality the details are a little bit more complicated and timing might be different.)

Hope this helps...
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Nice diagrams, Daniel! I do wish we had a picture drawing feature in our forums.

Tricia, I thought you might like to have the following list of free on-line Java tutorials and books that I have found useful:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist


  • [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Daniel Mayer
    Ranch Hand

    Joined: Sep 09, 2004
    Posts: 103
    Originally posted by Dirk Schreckmann:
    Nice diagrams, Daniel!


    Thanks!


    Tricia, I thought you might like to have the following list of free on-line Java tutorials and books that I have found useful:


    I added them to http://faq.javaranch.com/view?JavaBeginnerLinks
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Data v. Reference