Cant arrays be changed by a method? [duplicate] - java

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 months ago.
public class lalak
{
public static void m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
m1(array);
System.out.println(array[2]);
}
}
why answer is 1, not 3?
i expected the program to print 3 but i got 1 as output.
i thought method would change my original array but it turned out to be false. does anyone know the reason for this?

You're creating a new array which makes the reference to your array in main redundant.
You could either, replace the first line like Jon Skeet said
Or another option would be, to have m1 return an array and assign it to your array in main
public class lalak
{
public static int[] m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
array = m1(array);
System.out.println(array[2]);
}
}

Related

Passing an array in Java [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 5 years ago.
Why is the output for the following code 11,21,31 11,21,31 and not 10,20,30 10,20,30?
public class Tp {
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
public static void main(String args[])
{
int arr[]= {10,20,30};
doChange(arr);
for(int x:arr)
{
System.out.print(x+",");
}
System.out.println(arr[0]+" "+arr[1]+" "+arr[2]);
}
}
When you do
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
This line a[pos]+=1;, adds +1 to each value on the array, so the expect output is 11,21,31 11,21,31, if you want 10,20,30 10,20,30, delete or comment this line doChange(arr);
Because in java arrays are objects, when you pass the arr array to the doChange() method as argument, it actually copies the reference id of that array (not the elements).
In a simple way, you are updating the same array that you passed in the doChange() method, that's why it reflects to the original array.
Obviously it prints "11, 21, 31, 11,21,31", with the method doChange you add 1 to every element of the array.
Writing:
a[pos]+=1;
is the same as writing:
a[pos]=a[pos]+1;
int arr[]= {10,20,30}; Initializes array [0]=10, [1]=20, [2]=30
doChange called with a[] which is a reference of arr[]
for-loop adds +1 to every value in arr[] ( += is equal to x = x + ? )
Now you have [0]=11, [1]=21, [2]=31

calling a method and it doesn't do it's work [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I made 2 classes one is :
public class JavaApplication5 {
public static void main(String[] args) {
int y=0;
cats catobject = new cats();
catobject.ma(y);
System.out.println(y);
}
}
the other is :
class cats {
public void ma(int x){
x=x+9;
}
}
it's supposed to print out 9 since the method ma should have added 9 to the integer y , but instead it prints out 0. why?
When you pass an integer to a method in java, the method receives a copy of that integer. It does not modify the original. For more info on this see this link: http://javapapers.com/core-java/java-pass-by-value-and-pass-by-reference/
To make your code work, modify it to return an int. Like this:
public class JavaApplication5 {
public static void main(String[] args) {
int y=0;
cats catobject = new cats();
y = catobject.ma(y);
System.out.println(y);
}
}
class cats {
public int ma(int x){
return x+9;
}
}

how to print the elements of an array [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I am trying to print the elements of this array, but it is printing junk characters. What is the problem in this program?
class Demo{
public static void main(String[] args){
int[] x= new int []{5,6,7,8,9,10,11};
{
System.out.println(x);
}
}
}
You need to use the Arrays.toString() method:
System.out.println(Arrays.toString(x));
This gives you a meaningful representation of the data inside your array. The default toString of Java array objects is not a meaningful representation of the data.
public static void main(String args[]) {
int[] x= new int[]{5,6,7,8,9,10,11};
{
System.out.println(Arrays.toString(x));
}
}

Java string input and output string [duplicate]

This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
and thanks for reading this question from a beginner.
I tried to run this code on the command:
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(s);
}
}
and the following input: "java a this is string"
gives me:
"[Ljava.lang.String;#65d4ab0e" as an output.
What should I do to get "this is string" as output?
You are printing the array object itself, not the content of the array.
The hashcode value [Ljava.lang.String;#65d4ab0e indicates that, s is an array and it's has String values. The leading [ says, the object you have printed is an array.
public class a{
public static void main(String[] args){
String[] s = args;
System.out.println(Arrays.toString(s));
}
}
Use Arrays.toString(s)
Take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])

Why can't I reassign the size of an array by replacing the variable with an array of different size? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 3 years ago.
This is my first post here, so please be friendly! :)
Let's say I have this code:
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
public static void main(String[] args) {
int[] nums = {0,2};
reassign(nums);
System.out.println(nums[1]);
}
Why is my answer 2, and not 11? Does it have something to do with the relative sizes of the arrays?
When you do this,
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
you make A as a refference of nums, and the nums you are refering to is the one from parameter, not the one from main method. its two different variable
This is how you suppose to do it:
static int[] nums = {0,2}; //initial value of nums
public static void reassign (int[] arr) {
nums=arr;
}
public static void main(String[] args) {
int[] A = {10,11,22};
System.out.println("before reassign:"+nums[1]);
reassign(A);
System.out.println("after reassign:"+nums[1]);
}
Output:
before reassign:2
after reassign:11
Why is my answer 2, and not 11?
For one, you wanted to write
public static void reassign (int[] nums) {
int[] A = {10,11,22};
nums = A;
}
But than won't help you either: you cannot assign to one method's local variable from another method. The value of the variable (a reference to the array) is passed to reassign, not the address of the variable itself.
In reassign you merely create a new array and then assign its reference to the local variable nums (in my corrected code).
Does it have something to do with the relative sizes of the arrays?
No, the reason behind it is fully general and applies to all of Java.

Categories