File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can you explain this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can you explain this?" Watch "Can you explain this?" New topic
Author

Can you explain this?

sonali sawant
Greenhorn

Joined: Oct 24, 2005
Posts: 13
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

Joined: Feb 02, 2006
Posts: 2023
What is variable hiding and shadowing?
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Above code contains compile time error at line v.val; in method named second.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
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 ]

Ping & DNS - my free Android networking tools app
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
can someone please explain wat v.val does...i dont understand.....
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
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

Joined: Oct 24, 2005
Posts: 13
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

Joined: Oct 24, 2005
Posts: 13
i am really confused...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
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

Joined: Dec 20, 2005
Posts: 22
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

Joined: Oct 24, 2005
Posts: 13
Thanks to all of you for explanation..now i got it.thanks.

---Sonali.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you explain this?