Check your skill: Java reference

View previous topic View next topic Go down

Java Check your skill: Java reference

Post by BIT0102-Mohaimin on Mon Apr 04, 2011 3:20 pm

In Java, objects are passed as reference. Understanding this is real important for Java programmers because otherwise you may find a lot of things working in a unexpected way.
Here is a little code that plays with references.
Code:
public class ReferenceSkillChecker {
   private int value = 10;

   public void setValue(int value) {
      this.value = value;
   }

   public int getValue() {
      return value;
   }   
}

The following 8 segments of codes are written sequentially in the main. I am separating them so that you can see all at once. Each part has an commented index. Please refer according to these indices.

Code:
public static void main(String[] args) {
      // 1
      ReferenceSkillChecker a, b, c;
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 2
      a = new ReferenceSkillChecker();
      b = a;
      c = b;
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 3
      a.setValue(20);
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 4
      b.setValue(30);
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 5
      a = new ReferenceSkillChecker();      
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 6
      c = new ReferenceSkillChecker();
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 7
      b = new ReferenceSkillChecker();
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
Code:
// 8
      b = a;
      c = b;
      a = c;
      c.setValue(30);
      System.out.println(a.getValue() + " " + b.getValue() + " "
            + c.getValue());
}

What should be the output?
Please do not post the answer if you have already run the program.
Please use the HIDE tag to hide your answer so that those who does not want to see answers cannot not see.
Add some logics with the answer.


Last edited by BIT0102-Mohaimin on Tue Apr 05, 2011 1:13 am; edited 2 times in total (Reason for editing : 1. The title was confusing, name of the related programming language added. 2. Indexing information of the code segments added.)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Check your skill: Java reference

Post by BIT0112-Rokon on Mon Apr 04, 2011 8:09 pm

Excellent ! Like it. and r++

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Check your skill: Java reference

Post by BIT0210-MJSunny on Mon Apr 04, 2011 9:28 pm

ভাই run করার পরও //৮ নাম্বার টা বুঝতাছিনা। confused confused Crying or Very sad Sad
বিস্তারিত বলবেন প্লিজ। Very Happy Very Happy
avatar
BIT0210-MJSunny
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 42
Points : 63

View user profile

Back to top Go down

Java Re: Check your skill: Java reference

Post by BIT0220-Iftekhar on Tue Apr 05, 2011 12:28 am

I'm giving my answers for the code segments from 2nd to the last,with explanation.
Those who haven't run the code yet don't click on the spoiler.
Spoiler:
2nd segment: answer: compilation error
explanation: a,b,c are not objects of ReferenceSkillChecker class
3rd segment: answer: 10 10 10
explanation: a is an object of ReferenceSkillChecker class and both b and c refers a
4th segment: answer: 20 20 20
explanation: value is set to 20 by object a. b and c is still referring to a
5th segment: answer: 30 30 30
explanation: b refers to a, c refers to b.
6th segment: answer: 30 30 30
explanation: 'a' now refers to a new object of ReferenceSkillChecker. b and c refers to the old object
7th segment: answer: 30 30 30
explanation: c now refers to a new object. b still refers to the old one
8th segment: answer: 30 30 30
explanation: a,b,c refers to three different object of ReferenceSkillChecker now
final segment: answer: 30 30 30
explanation: a,b,c creates a block of reference I guess Very Happy They all refer to the same object of ReferenceSkillChecker. c.setValue() = 30 sets the value to 30


Last edited by BIT0220-Iftekhar on Tue Apr 05, 2011 12:44 am; edited 1 time in total (Reason for editing : forum hidden tag not working :|)

_________________________________________________________________
with great power comes great responsibility
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Check your skill: Java reference

Post by BIT0102-Mohaimin on Tue Apr 05, 2011 1:27 am

BIT0210-MJSunny wrote:ভাই run করার পরও //৮ নাম্বার টা বুঝতাছিনা। confused confused Crying or Very sad Sad
বিস্তারিত বলবেন প্লিজ। Very Happy Very Happy
Of course, but not now. wait few more days.

BIT0220-Iftekhar wrote:I'm giving my answers for the code segments from 2nd to the last,with explanation.
I am confused about your indexing. Assumed that with "2nd" you wanted to mean the first part of the main. I used an indexing to avoid confusion which has created some kind of confusion I suppose.
Again, all your explanation seems right. But your answers does not match with the answer I expect. I think there is some kind of typing mistake.


avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Check your skill: Java reference

Post by BIT0220-Iftekhar on Tue Apr 05, 2011 1:59 am

BIT0102-Mohaimin wrote:
I am confused about your indexing. Assumed that with "2nd" you wanted to mean the first part of the main. I used an indexing to avoid confusion which has created some kind of confusion I suppose.
ya vai, you are dead right Very Happy Very Happy Very Happy
I started indexing from the ReferenceSkillChecker class.
Again, all your explanation seems right. But your answers does not match with the answer I expect. I think there is some kind of typing mistake.
Those were not typing mistakes. They are results of my misunderstanding. Head Bang
For example,in segment //5, I thought,as value was previously set by b.setValue(30),a.getValue() will print 30. But 'a' refers to a new object now so a.getValue() should print 10.


Last edited by BIT0220-Iftekhar on Tue Apr 05, 2011 2:02 am; edited 1 time in total (Reason for editing : typing mistake this time :D)
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Check your skill: Java reference

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum