• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data v. Reference

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tricia Lemay
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend that you take a look at Bruce Eckels book The first chapter describes what objects represent very well.
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  •  
    Daniel Mayer
    Ranch Hand
    Posts: 103
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic