File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ARRAY doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic