wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ARRAY doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ARRAY doubt " Watch "ARRAY doubt " New topic
Author

ARRAY doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
class GFC306 {
static int[] i1 = {1}, i2 = {3};
static void m1(int[] i1) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}}

The answer is a. Prints: 1,1

why is the answer is Prints: 1,1
but not Prints: 1,3
please explain
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
The explanation for the following question is:

Code:
-----------------------------------------------------------------------
public class Casting21 {
static int[] i1 = {1}, i2 = {3}; //1

static void m1(int[] i1) {
int[] i3 = i1;
i1 = i2;
i2 = i3; //2
}

public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}
}
-----------------------------------------------------------------------

Inside m1() int i1 has a method scope, while i2 is not local variable i.e it is not defined anywhere inside the method, so when we initialize i2 with the reference of i1, i2 starts pointing to i1. That means now both i1 and i2 are refering to the same object. So the Output comes out to be:

1,1
Debbie Tom
Greenhorn

Joined: Sep 12, 2005
Posts: 11
A Slightly different explanation

Code:
-----------------------------------------------------------------------
public class Casting21 {
static int[] i1 = {1}, i2 = {3}; //1

static void m1(int[] i1) {//creates a local variable i1.contains value {1)
int[] i3 = i1; //class variable i3 now contains value {1}
i1 = i2;// Variable hiding occurs and local variable i1 is assigned to value of i2-{3}.
i2 = i3; //class variable i2 is assigned i3's value which is {1}
}

public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}
}
-----------------------------------------------------------------------
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
class GFC306 {
static int[] i1 = {1}, i2 = {3};
static void m1(int[] i1) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}}

Hello,The Answer Is Correct.
I'll Tel U One Trick 2 Solve This Type Of Problems.
Let, Object Reference Being Passed obj Is Same As Local Reference obj Then Olways Consider Original Argument As 'obj .Now Do Manipulation.Now Outside Method If U Wanna Access obj Retrieve It As 'obj And Not As obj.
This 'll Help U

Agrah Upadhyay
3rd Year B.tech
Preparing For SCJP1.4
SASTRA,Tamilnadu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ARRAY doubt
 
Similar Threads
Method Arguments
Arguments
Array assignment -- need help
Method parameters doubt?
Dan's Exam Array Doubt