• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ARRAY doubt

 
Ranch Hand
Posts: 481
  • 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
 
Ranch Hand
Posts: 102
  • 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
 
Greenhorn
Posts: 11
  • 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]);
}
}
-----------------------------------------------------------------------
 
Ranch Hand
Posts: 579
  • 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 claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic