• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

ARRAY doubt

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}
}
-----------------------------------------------------------------------
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic