• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Basic Questions  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got this from Javaprepare. The answer is that the program prints 4.
Two Questions.
1. change_i method has a void return type. Why would i[0] affected by change_i?
2. i[] is not static. Why would the program even compiled?
I am getting nervous since my exam date is close. Thank you.
---------------------------------------
public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. change_i method has a void return type. Why would i[0] affected by change_i?
change_i is passed a reference to the array object and operates on that object.
2. i[] is not static. Why would the program even compiled?
int i[] is a local variable in the main method - whats wrong with that? You need to be able to keep straight the differences between:
static variables
instance variables
local variables (also called automatic variables)
Bill

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Arrays are treated like objects. So the same rule applies when you pass them to methods. (i.e changes made on the object via this ref change the state of the object)
public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i); //you are passing the i declared in main.
System.out.println(i[0] + " Value of i is " + i);
}
public static void change_i(int i[]) {
i[0] = 2;
// here this is not a instance variable this local variable.
i[0] *= 2;
System.out.println("Value of i is " + i);
//you can see that the both i are refering the same objects by the values.
}
}
Hope this helps,
Vanitha.
 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bill.
For #1, what is the difference from the following example? int i is 1 because change_i has void return type.
------------------------------------------------
public class example {
int i = 0;
public static void main(String args[]) {
int i = 1;
change_i(i);
System.out.println(i);
}
public static void change_i(int i) {
i = 2;
i *= 2;
}
}
--------------------------------------------------
#2, I am totally clear about local and static variables. I think you said that i is a local variable in the main method because it's declared on line 4, which is right below the main method. How about line 2 (int i[] = {0} ? I thought i is a instance varibale because it's declared on line 2?
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here i is primitive value, so the changes made in a method will not affect the original value.
Vanitha.
 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ops! i didn't mean to make the smiling face.... let me repeat.
#2, I am totally clear about local and static variables. I think you said that i is a local variable in the main method because it's declared on line 4, which is right below the main method. How about line 2 (int i[] = {0};)? I thought i is a instance varibale because it's declared on line 2?
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susie, I've commented a bit of your code:

Note that at no point in this code is the instance variable i[] changed from its initial declaration. You are really dealing with 3 variables of the same name: the instance variable i[], the variable i[] local to the main method, and the variable i[] local to the change_i method. Both local variables happen to be pointing to the same location in memory since the one was passed in as a parameter, so we consider them to be the same variable in a sense -- but note that if you had changed the reference of i[] in the change_i method, it would no longer be referencing the same array object as the i[] local to the main method.
Things like this can get a little confusing when you use the same variable names in different scopes. I usually make an effort to give my instance, local, and paramenter references different names so that I know exactly which objects are being manipulated at all times.
[This message has been edited by Scott Appleton (edited July 09, 2001).]
[This message has been edited by Thomas Paul (edited July 09, 2001).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass an object (an array is an object) any changes made to that object will be seen by the calling method.
Example:

Notice that changeButton1() does change the label as seen by the calling method but changeButton2() does not! Because changeButton2() creates a brand new object, it looses the reference to the object passed to it.

[This message has been edited by Thomas Paul (edited July 09, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Susie,
You may want to check out Pass-by-value Please and Understanding that parameters are passed by value and not by reference.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I like tacos! And this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!