This question already has answers here:
int array initialization
(7 answers)
Closed 5 years ago.
Why is it that when an array of 'ints' is made as a local variable they default to zero?
public static void main(String [] args) {
int []arrayInts = new int[5];
for(int i: arrayInts)
System.out.println(i);
//Prints out zeros
}
While if an 'int' variable is declared as a local variable it does not get initialized.
public static void main(String [] args) {
int a;
System.out.println(a);
//Compilation error: The local variable a may not have been initialized
}
These two examples aren't comparable.
In the first one, you are actively initializing your array. The default value for an array of ints is 0.
In the second example, you've not initialized the variable a, either explicitly or implicitly, and you're attempting to make use of it. Java will complain about this action.
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 years ago.
The value of array elements are getting changed while calling a static method with the parameter passing the same array as argument but primitive value does not get changed
Is it work differently in the case of while we pass data structure as method argument ?
Want to know why array element get changed after the method call with the array ..
I was expecting value 0 in the last syso statement instead of 999
public class TestStatic {
public static int square (int x)
{
x = x + 1;
return x * x;
}
public static int[] changeArr(int a[])
{
a[0] = 999;
return a;
}
public static void main(String[] args)
{
int a = 10;
System.out.println((square(a)));
System.out.println(a);
int arr[] = {0,0};
changeArr(arr);
System.out.println(arr[0]);
}
}
Actual Output:
121
10
999
I was expecting
121
10
0
In Java, method parameters are by reference, not by value, which means you pass the reference to the object, not a copy of the object. Note that primitives are passed by value.
This question already has answers here:
Java variable may not have been initialized
(7 answers)
Why does the Java compiler not understand this variable is always initialized?
(3 answers)
Closed 2 years ago.
I wrote this code in an online java test recently. It was inside a method that was set to return an integer. I got an error message something like "variable a has no value assigned to it". I find this odd because the forloop must have access to the methods variable and the assignments inside of the loop must be registered right?
int a;
for(int i=1;i<5;i++){
a = i;}
return a;
I did assume that the method would return the integer 5.
This is only a question regarding scope of the variable a. I know that the code makes no sense.
You can try below code , that will help you. Initialisation is mandatory for any variable to return or hold any value.
class a{
public static void main(String[] args) {
System.out.println(test());
}
public static int test(){
int a = 0;
for(int i=1;i<5;i++){
a = i;}
return a;
}
}
This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.
This question already has answers here:
Passing directly an array initializer to a method parameter doesn't work
(3 answers)
Closed 8 years ago.
I am writing test code to explore properties of an Array.
Why does this work
public static void main(String[] args){
int[] testing={1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
and why doesn't this work?
public static void main(String[] args){
int[] testing= new int[3];
testing = {1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
What is it about Array that prevents this from being valid?
Because the Java Language Specification says so
An array initializer may be specified in a declaration (§8.3, §9.3,
§14.4), or as part of an array creation expression (§15.10), to create
an array and provide some initial values.
So you can either use it as you are already doing
int[] testing = {1,2,3};
or as part of an array creation expression
testing = new int[]{1,2,3};
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.