Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you explain this?

 
sonali sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code,what will be the output?

class Value
{
public int i=15;
}

public class Test
{
public static void main(String argv[])
{
Test t=new Test();
t.first();
}

public void first()
{
int i=5;
Value v=new Value();
v.i=25;
second(v,i);
System.out.println(v.i);
}

public void second(Value v,int i)
{
i=0;
v.i=20;
Value val=new Value();
v.val;
System.out.println(v.i + "" + i);
}
}

Answer is :
15 0
20

How??
 
wise owen
Ranch Hand
Posts: 2023
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Above code contains compile time error at line v.val; in method named second.
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is that we cannot change the reference of an object but the object can be changed.In the method, a copy of the reference variable is passed so its changing the reference of that copy and not the actual reference.

Bye & Regards,
Praveen
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be surprised by the output. Why don't you describe here what you were expecting? That would make it easier to spot were you possibly went wrong in your reasoning. It would also be a good learning experience.
[ August 22, 2006: Message edited by: Ulf Dittmer ]
 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please explain wat v.val does...i dont understand.....
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code has a compile error (somebody already mentioned this) and does not produce the output that's claimed in the original message.

valueObject1.valueObject2; //is not legal syntax as far as I know.

Where did this code come from?
 
sonali sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This code is from whizlabs exam 1. Sorry i typed v.val in public void second(Value v,int i) instead of v=val. correct code is ---

public void second(Value v,int i)
{
i=0;
v.i=20;
Value val=new Value();
v=val;
System.out.println(v.i + "" + i);
}


According to me answer is---
25 0
20

We are passing second(25,5) from method first().

In method first(),
System.out.println(v.i + "" + i);
will print the value v i.e 25 and local value of i i.e 0.
and we are assigining v=val i.e v=20.

when we again come back to method first,
System.out.println(v.i);
will print 20 means answer will be
25 0
20

[ August 23, 2006: Message edited by: sonali sawant ]
[ August 23, 2006: Message edited by: sonali sawant ]
 
sonali sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am really confused...
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will print the value v i.e 25

v has been assigned the newly created val object, the i field of which is i=15.

Even if it hadn't been assigned val, it would not print 25, but 20, because of the "v.i = 20" assignment.
[ August 23, 2006: Message edited by: Ulf Dittmer ]
 
Sruthi Sen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

Answer is 15 0 20

First i is local variable with value 5
i.e i=5

then it creates a object v that contains i=15
ie v.i=15 (initially)

then assigning v.i=25;

so it contains 25 now..

now calling with the values like second(25,5)

here in second method
we were assigning i=0 and v.i=20

again new object val is created and val.i=15

now V=Val ie now v points to Val object so
v->val.i=15
if we say v.i,it gives 15 and as we know i=0

now control goes to first method and here v is the original object
which got modified its i value to 20 in Second method

so answer is 15 0 20

Hope you got it

Sruthi
 
sonali sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for explanation..now i got it.thanks.

---Sonali.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic