Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 7th, 2012, 01:24 AM
helpneeded22 helpneeded22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 helpneeded22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 46 sec
Reputation Power: 0
New to Java and and trying to understand this code

Here is the example that I can't figure out why it outputs what it does. I have added line numbers to help reference my question.

Code:
[1] Shirt myShirt = new Shirt();
[2] Shirt yourShirt = new Shirt();

[3] myShirt = yourShirt;

[4] myShirt.colorCode = 'R';
[5] yourShirt.colorCode = 'G';

[6] System.out.println("Shirt color: " + myShirt.colorCode);


The above code outputs: Shirt color: G

I thought all programming languages interpreted code line by line?

When the code reaches line 3, myShirt is assigned the properties of yourShirt. At this point there is no colorCode property to pass on.

At line 4, myShirt is now assigned 'R' as the property.

Code:
Should not the code order be as below in order to output the value of 'G' for myShirt color?

Shirt myShirt = new Shirt();
Shirt yourShirt = new Shirt();

myShirt.colorCode = 'R';
yourShirt.colorCode = 'G';

myShirt = yourShirt;

System.out.println("Shirt color: " + myShirt.colorCode);

Reply With Quote
  #2  
Old November 7th, 2012, 02:22 AM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
Hi,

This can be a tricky concept to understand when beginning Java.

Line 1 creates a new Shirt in memory and assigns myShirt as a reference to it.

Line 2 creates another Shirt in a different area of memory and assigns yourShirt as a reference to that.

Line 3 does not actually copy the properties. Instead, the myShirt reference is changed to point at the area in memory that yourShirt is pointing to. So now both myShirt and yourShirt references are pointing to the same thing.

Line 4 changes the color of the referenced Shirt.

Line 5 again changes the color of the same referenced Shirt.

Line 6 prints the color of the shirt through the myShirt reference. You would get the same result if you printed the color of yourShirt, as both references are pointing at the same thing.

Tutorial

Hope this helps
slink

Last edited by slink : November 7th, 2012 at 02:42 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > New to Java and and trying to understand this code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap