473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

BlueJ learner quick question on java

3
Hi!
Having studied a bit of programming years back (bit of Basic and Pascal) I\'m currently trying to learn Java using BlueJ and don\'t quite understand what is going on in one particular situation! My previous programming experience leads me to expect a totally different answer!

The problem is that I have some code that looks like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Bird beaky = new Bird ();
  3. beaky.setWingspan (4);
  4. Bird puffy = beaky;
  5. beaky.setWingspan (6);
  6.  
  7.  
All the classes and methods are set up and the program executes fine but when I poll the variables after executing I would have expected puffy\'s wingspan to be 4 and beaky\'s to be 6 - in fact they both have a wingspan of 6. Can anyone explain to me why this is?

Thank you in advance for your help! It\'s a silly little question but it\'s got me totally flummexed!

Jay
Nov 9 '07 #1
4 1401
JosAH
11,448 Expert 8TB
Hi!
Having studied a bit of programming years back (bit of Basic and Pascal) I\'m currently trying to learn Java using BlueJ and don\'t quite understand what is going on in one particular situation! My previous programming experience leads me to expect a totally different answer!

The problem is that I have some code that looks like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Bird beaky = new Bird ();
  3. beaky.setWingspan (4);
  4. Bird puffy = beaky;
  5. beaky.setWingspan (6);
  6.  
  7.  
All the classes and methods are set up and the program executes fine but when I poll the variables after executing I would have expected puffy\'s wingspan to be 4 and beaky\'s to be 6 - in fact they both have a wingspan of 6. Can anyone explain to me why this is?

Thank you in advance for your help! It\'s a silly little question but it\'s got me totally flummexed!

Jay
Think of 'beaky' and 'puffy' being pointers to a bird or birds. Your program fragment
shows that both beaky and puffy point to the same single bird. So if you change
properties of that bird (such as the wing span) both beaky and puffy notice that
change just because they both point to that same bird.

kind regards,

Jos
Nov 9 '07 #2
jay89
3
Thank you very much, Jos! That clears that up! So does that mean that beaky and puffy are essentially the same object and that that object has 2 names?
Or am I just confusing myself?!! (Not as difficult as I previously thought, it appears!)
Thanks again for your reply.
Jay
Nov 9 '07 #3
JosAH
11,448 Expert 8TB
Thank you very much, Jos! That clears that up! So does that mean that beaky and puffy are essentially the same object and that that object has 2 names?
Or am I just confusing myself?!! (Not as difficult as I previously thought, it appears!)
Thanks again for your reply.
Jay
Every variable or parameter in Java that is not a primitive (int, long, byte etc.)
is actually just a pointer to an object. Basically the only way you can create
an object is by using the 'new' operator. One single object per one single 'new'
in your code; if you use the 'new' operator in a loop, of course you have that
many objects created by that 'new' operator. If you do 'foo= bar' then both foo
and bar point to the same object. Java is a simple language.

kind regards,

Jos
Nov 9 '07 #4
jay89
3
Once again, thank you Jos (think I accidentally pressed report instead of reply just now - sorry mods!) for a concise and speedy reply! That has cleared up that little muddle and should keep me going for the next week or so (she says!). I\\\'m really grateful that there are people like you happy and willing to take the time and effort to help others - especially in this day and age! Please know that everything that you do is much appreciated!
Thank you again
Jay
Nov 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simon | last post by:
I've had enough of C# Learner. I've had enough of his complaining about using parentheses for every 'if' statement. I've had enough of his complaining about having to mix assignment of return...
0
by: Chris Thomasson | last post by:
<wkaras@yahoo.comwrote in message news:1156863246.290976.191710@i3g2000cwc.googlegroups.com... volatile has nothing to do with the memory model... Well, except in Microsoft... ...
2
by: Rakia | last post by:
Hi folks, can anyone of you please tell me where i can download free java software called BlueJ? Thank you all.
0
by: lenin42001 | last post by:
Can you help please. I am trying to import a text file a simple notepad sample with a few words into a blueJ project. I have tried: N:Text N: Bigone6.Text N: Bigone.Text.txt None seem to work,...
0
by: Lloydm | last post by:
once again.. hello to all I have installed BlueJ on my pc but I can't run it. I dumped the JDK 1.6 .jar file in same folder as blueJ but it's not detected or perhaps I'm doing it wrong. The readme...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.