public void changeColor(int[] value)
{
color = value;
}
In the above code Color is a is a int Array in a class. and value is a array in the scope of method ChangeColor. By Color = value; I am assigning the memory space allocated to value to Color. As per my understanding once a method ended the variable specific to the method will be deleted, in that case will Color be able to access its value after the method ends??
"will Color be able to access its value after the method ends??"
Short answer: Yes, otherwise why would you define a variable as global.(No matter it's a reference or primitive)
Please run this code to see what happens:
public class Test {
int[] Color = new int[2];
int i = 20;
public static void main(String[] args) {
Test t = new Test();
t.ChangeColor(new int[] {1,2,3});
System.out.println(Arrays.toString(t.Color));
System.out.print(t.i);
}
public void ChangeColor(int[] value)
{
Color = value;
i = 10;
}
}
the int[] value parameter of the method is a reference to an array, but not a copy of the full array. Color is also a reference, so when you assign them, you make them point to the same array.
when the method exists, value is deleted from memory, but that was just a reference, not the array. So color which is still pointing to the array, will still be able to access even if the method has finished.
Related
I had to make a program which was able to print symbols based off an array with numbers, with the numbers corresponding to the amount of symbols to print.
I got this to work; here is the code:
class printChart {
int[] values;
void barChart(int[] values){
for (int i=0;i<values.length;i++){
int number = values[i];
for (int j=0;j<number;j++){
System.out.print("*");
}
System.out.println();
}
}
void demo(){
barChart(new int[] {2,5,0,4,3});
}
public static void main(String[] args){
new printChart().demo();
}
}
My question is as follows: How does Java know that the {2,5,0,4,3} array should be assigned to the variable values? I'm assuming it's because I set void barChart to pass along int[] values, but I'd like to know more about the inner workings and what's going on exactly.
In Java, everything is pass-by-value and it is also important to know what the value is.
This method
void demo(){
barChart(new int[] {2,5,0,4,3});
}
Do the same as this one
void demo(){
int[] arr = new int[] {2,5,0,4,3};
barChart(arr);
}
In the first one, there is created new array with 2,5,0,4,3 values and its reference is copied to parameter values in barChart method.
In second one, there is created new array with 2,5,0,4,3 values and its reference is copied to variable arr. Then the value of arr (which is reference to array) is copied to parameter values in barChart method.
And this is how it works and why barChart method knows the values.
Also good point by Ćukasz, the second line does not do anything in your program, therefore you can change this :
class printChart {
int[] values;
void barChart(int[] values){
to this
class printChart {
void barChart(int[] values){
I'm not so sure what your question is, but let me tell you bit what you've done.
You've implemented a method(function) named void barChart(int[] value)
To run this method you must need to pass a one dimensional Array of Integer values to it.
Now comes the interesting part.
You've created a class Variable int[] values; in code line 2.
Also you've have created the lokal variable "value" in the method void barChart(int[] value).
What you've done is called overshadowing. The method "barChart()" only uses the lokal value which is passed to it when it is called.
You never used the class variable once, hence you could delete it.
Now if you want to use the class variable you could either:
a) Change the name of the variable (class or local)
b) In the method "barChart" write a this.value instead of just value. This will ensure that you are using the class variable and not the local one.
I am aware that the idea of the keyword private is to enable encapsulation. However, I got confused when I realized that I can modify an Array after retrieving it with a getter, which surprised me. The same didn't work for the plain integer, although I thought java treats all variables as objects.
The example:
public class PrivContainer {
private int[] myIntArray;
private int myInt;
PrivContainer(){
myIntArray=new int[1];
myIntArray[0]=3;
myInt=22;
}
public int[] getIntArray(){
return myIntArray;
}
public int getInt(){
return myInt;
}
public void printInt(){
System.out.println(myIntArray[0]);
System.out.println(myInt);
}
}
public class Main {
public static void main(String[] args){
PrivContainer pc=new PrivContainer();
int[] r=pc.getIntArray();
int q=pc.getInt();
r[0]=5;
q=33;
pc.printInt();
}
}
The Output of printInt() is 5,22
This means that main method could change the entries of the private array but not of the private int.
Could someone explain this phenomena to me?
An array is a mutable Object. Therefore, if you have a reference to that array, you can modify its contents. You can't do the same with primitive members of a class (such as int) and with references to immutable class instances (such as String and Integer).
Your assignment :
q=33;
Would be similar to :
r = new int [5];
Both of those assignments cause the variables to contain new values, but they don't affect the state of the PrivContainer instance from which the original values of those variables were assigned.
Nothing seems strange here. What happen basically as follow.
public class Main {
public static void main(String[] args){
PrivContainer pc=new PrivContainer(); <-- create new `PrivContiner` object which also initialised the private variables
int[] r=pc.getIntArray(); <-- you get the "object" integer array here and assign r to refer to that object
int q=pc.getInt(); <-- you get the "primitive" integer here and assign q to refer the myInt variable here.
r[0]=5; <-- you assign the first value of the array 5. Note that the object reference is still the same here
q=33; <-- you assign the variable q to 33. Note that, this mean, the variable q refer to another primitive here (which is 33)
pc.printInt(); <-- print the content of the object here.
}
}
When you invoke the printInt function. the output will be 5 and 22 as the new integer (33) is assigned to q and its scope is only within the main function.
While you return an array from a getter you return the reference of that object. Since you have the reference you can change its elements. If you want to avoid this behavior you will have to return the clone of your array in that case you wont be able to change the elements of your array
public class Main {
public static void main(String... args) {
Arr arr = new Arr();
int[] y = arr.getX();
y[1] = 5;
System.out.println(arr.getX()[1]);
}
}
class Arr {
private int[] x = {1, 2, 3};
public int[] getX() {
return x.clone();
}
}
Try this code and remove the clone method, like this
class Arr {
private int[] x = {1, 2, 3};
public int[] getX() {
return x;
}
}
Now execute the main method, you will observe that changing value of y will change the value of array x as well.
public class foo {
private int a[];
private int b;
public foo(){
a = new int[] {1,2};
b= 3;
}
public int[] getA(){
return this.a;
}
public int getB(){
return this.b;
}
I noticed that it's possible to change a value of A by accessing the object like this:
foo f = new foo();
f.getA()[0] = 5; // f.a[0] changes to 5
but it isn't possible to do something like:
f.getB = 5; // gives error
f.getA() = new int[]{2,3}; //gives error
can someone explain me how this works, and how to prevent the user from changing the value of an array cell?
Thanks in advance.
In Java, array is a reference type, which means that the value of an array expression is a reference to the actual array.
The return value of getA() is, therefore, a reference to the private array inside your object. This breaks encapsulation: you give access to your object's internals.
You can avoid this by either returning a reference to a copy of your internal array, or by providing a different API which only returns individual elements, say a method getA(int index).
f.get(A) returns a reference to an array. You can access that array the way you access any array, and assign values to its elements with f.get(A)[i]=... (though it makes more sense to store the returned array in a variable, which would let you access that array multiple times, without having to call f.get(A) each time).
You can't, however, assign anything f.get(A) via f.get(A)=.., since a method call is not a valid left side of an assignment operator. For all you know, a call to f.get(A) may generate a new array that is not referred to by a member of the foo class, so assigning f.get(A)= new int[5]; would make no sense, since there would be no variable in which to store the new array.
The same explanation applies to f.getB() = 5;.
Instead of giving away the array, to allow the caller to do what they like with it you can use an indexed getter
public int getA(int n){
return this.a[n];
}
public void setA(int n, int x) {
this.a[n] = x;
}
Now, the caller has no access to the array and cannot change it without you knowing.
I have to implement an array that takes its elements from a random generator. Whenever the array reaches it's last element a method resize() will be called to increase the size of the array. In my code every thing goes alight until invoking resize() method, it does not do any effect on the size of the array. It supposed to increase the size of the array to allow more elements to be entered.
for (int j=0; j<arr.length-1; j++) {
random= generator.nextInt(arr.length*4) ;
arr[j]=random;
}
if(arr_size==arr.length){
resize(arr);
for (int h=0; h<(arr.length-1)*2; h++) {
random= generator.nextInt(arr.length*4) ;
arr[h]=random;}
}
Here is resize():
private static void resize(int[] arr) {
int[] tmp = new int[2*arr.length];
System.arraycopy(arr,0,tmp,0,arr.length);
arr = tmp;
}
Setting arr = tmp does not do what you think it does. You're simply pointing the local variable arr in the resize() method to the local variable tmp. What you want to do is return tmp and assign it to arr outside of the method.
if(arr_size==arr.length){
arr = resize(arr);
And change the resize() method signature to
private static int[] resize(int[] arr) {
//Resize
return tmp;
}
The key thing to take away is that: When passing an object reference to a method in Java, you're passing a copy of that object's reference, which can be thought of as a copy of the location in memory of that object. All such manipulations to this REFERENCE in the called method won't take effect outside of the method. However, you CAN manipulate the actual object itself because, as I said, the reference is pointing to the same place in memory with the exact same object.
But changing a copy of a location in memory to point to a new location in memory does not cause the original calling object reference to point to that same location
try this :
private static int[] resize(int[] arr) {
int[] tmp = new int[2*arr.length];
System.arraycopy(tmp,0,arr,0,arr.length);
System.out.println(tmp.length);
return tmp;
}
Either set and return the tmp variable as described by Kon, or you pass in arr by reference to make changes to it persist outside of the resize function:
private static void resize(ref int[] arr) { .... }
and call it like
resize(ref arr);
When you say New int, reference is changed. Java uses Copy by Reference not exactly Pass by reference, if you change reference by new the caller method properties wont have any change, in your case have a return type and assign it back to arr.
I'm doing a task for a course in Java programming and I'm not sure how the following thing is working? The method below takes the value from an array and a integer. The integer should be added to the array and then be used outside the method in other methods and so on, but how could this work when the method has no return for the new content of the array? There is a void in the method? Have I missed something? Preciate some help? Is there something about pointers?
public static void makeTransaction(int[] trans, int amount);
Arrays in Java are objects. If you modify the trans array inside the method, the changes will be reflected outside of it1. Eg:
public static void modify(int[] arr)
{
arr[0] = 10;
}
public static void main(...)
{
int x = {1, 2, 3};
System.out.println(x[0]); // prints 1
modify(x);
System.out.println(x[0]); // now it prints 10
}
Note that native arrays can't be dynamically resized in Java. You will have to use something like ArrayList if you need to do that. Alternatively you can change the return type to int[] and return a new array with the new element "appended" to the old array:
public static int[] makeTransaction(int[] trans, int amount)
{
int[] new_trans = Arrays.copyOf(trans, trans.length + 1);
new_trans[trans.length] = amount;
return new_trans;
}
1 It is also worth noting that as objects, array references are passed by value, so the following code has no effect whatsoever outside of the method:
public void no_change(int[] arr)
{
arr = new int[arr.length];
}
You can't add anything to an array. Java arrays have a fixed length. So indeed, what you want to do is impossible. You might make the method return an int[] array, but it would be a whole new array, containing all the elements of the initial one + the amount passed as argument.
If you want to add something to an array-like structure, use an ArrayList<Integer>.
Do you have to keep the method signature as is?
Also, can you be a bit more specific. When you say "the integer should be added to the array", are you referring to the amount argument? If so, then how is that amount added? Do we place it somewhere in the array or is it placed at the end, thus extending the array's length?
As far as pointers go, Java's pointers are implicit, so if you don't have a strong enough knowledge of the language, then it might not be so clear to you. Anyways, I believe that Java methods usually will pass objects by reference, and primitives by value. But, even that isn't entirely true. If you were to assign your object argument to new object, when the method terminates, the variable that you passed to the method is the same after the method executed as it was before. But, if you were to change the argument's member attributes, then when the method terminated those attributes values will be the same as they were inside of the method.
Anyways, back to your question, I believe that will work because an array is an object. So, if you were to do the following:
public static void makeTransaction(int[] trans, int amount)
{
trans[0] = amount;
}
// static int i;
/**
* #param args
*/
public static void main(String[] args)
{
int[] trans = {0,1,3};
makeTransaction(trans, 10);
for(int i = 0; i<trans.length; i++)
{
System.out.println(trans[i]);
}
}
The output of the array will be:
10
1
3
But, watch this. What if I decided to implement makeTransaction like so:
public static void makeTransaction(int[] trans, int amount)
{
trans[0] = amount;
trans = new int[3];
}
What do you think that the output will be? Will it be set to all zero's or will be the same as it was before? The answer is that the output will be the same as it was before. This ties in to what I was saying earlier.
I might've assigned that pointer to a new object in memory, but your copy of the pointer inside of the main method remains the same. It still points to the same place in memory as it did before. When the makeTransaction method terminates, the new int[3] object that I created inside of it is available for garbage collection. The original array remains intact. So, when people say that Java passes objects by reference, it's really more like passing objects' references by value.