This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 10 years ago.
When I want to implement a function in C++ , if it matters to receive the int array in the following cases?
void fn1(int []a) {
a[0] = 1;
}
void fn2(int a[]) {
a[0] = 1;
}
In Java, there is no semantic difference.
In C++, the first syntax is invalid.
In Java,the declaration is same...
but in C++,the fn1() declaration need to be different
Well, the question is not clear.. Whether to receive the int array or not, it depends on the logics of your method. In Java it is better to write a[], but you can write either way.
Also, look over here - pass array to method Java
Related
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 1 year ago.
I'm sure this is dumb, I'm just learning to code and trying to follow instructions on how to make this method that multiplies doubles into an array. The code compiles but does not return my array, it just gives me [D#76ed5528 which I'm assuming is the memory address of the array?
public class Ex1 {
public static void main(String[] args) {
Ex1 exone = new Ex1();
double[] bob = exone.square(2, 6, 9, 8);
System.out.println(bob);
}
public double[] square(int a, int b, int c, int d) {
double[] result = {a*a, b*b, c*c, d*d};
return result;
}
}
Yes #Drunkasaurus, the [D#76ed5528 you mentioned in is indeed the hash for the array in memory. In order to print the values in your array you have a few options including:
Using the Arrays.toString() method: https://stackoverflow.com/a/29140403/10152919
Using a simple loop:
https://stackoverflow.com/a/409795/10152919
Also, no question is a dumb question if you can learn from it 🙂
This is because you are directly trying to return the result array.
Try to iterate over the array.
For ex - https://www.geeksforgeeks.org/iterating-arrays-java/
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:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.. If it doesn't return a value than it is void..
However in the below program, I'm able to change the values of an array from a void function. Can anybody explain this to me please?
public class ArraysInMethods {
public static void main(String args[]){
int rd[] = {2,3,4,5,6};
change(rd);
for(int y: rd){
System.out.println(y);
}
}
public static void change(int x[]){
for(int counter = 0; counter<x.length;counter++){
x[counter]+=5;
}
}
}
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.
That is correct only for methods returning values, i.e. methods other than void. These methods define expressions, while void methods define statements.
Calling your change method is a statement, in the sense that it lacks return values. It does not mean, however, that it cannot change the state of your running program - for example, give different values to variables.
However in the below program, I'm able to change the values of an array from a void function.
You are not returning a value from your void function; all you do is modifying the array in place. This is allowed, because arrays are passed by reference.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
Hey there I was wondering why the "return ask" here does not change the value of 'ask' when I print it out on my main method (it prints out 0 in the main but works in the Log method) and how I can fix it. Thanks in advance!
public static int Log(int ask){
int b=0;
int c =0;
c = scannerobj.nextInt();
b = scannerobj.nextInt();
ask = b*c;
System.out.println(ask);
return ask;
}
public static void main(String [] args){
int ask=0;
Log(ask);
System.out.println(ask);
}
Because you never reset the ask variable, but rather you ignore the int that is returned by the Log method:
Log(ask); // the int returned is not assigned to anything
Instead do:
ask = Log(ask); // assign the int returned from the method back into ask.
Also understand that the ask parameter inside of the Log method is totally disconnected from the ask variable in your main method. Changing one will have no effect on the other, especially since ask is a primitive, and Java is pass by value.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to write a basic swap function in Java
Hi. I don't know java at all, and in the near future have no wish to study it. However I have lots of friends that are java programmers and from conversations I learnt that there is no analog of C#'s ref keyword in Java. Which made me wonder how can one write a function that swaps two integers in Java. My friends (though not very good java experts) could not write such a function. Is it officially impossible? Please note, that I understand that one can swap two integers without a function, the question is exactly to write a function that takes two integers and swaps them. Thanks in advance.
Short: You can't.
Long: You need some workaround, like wrapping them in a mutable datatype, e.g. array, e.g.:
public static void swap(int[] a, int[] b) {
int t = a[0]; a[0] = b[0]; b[0] = t;
}
but that doesn't have the same semantic, since what you're swapping is actually the array members, not the a and b themselves.
In Java, all the arguments are passed by value. So there is no such thing as a ref.
However, you might achieve variable swapping by wapping values in objects (or arrays).
public class Holder<T> {
public T value = null;
public Holder(T v) { this.value = v; }
}
public static <T> void swap(Holder<T> a, Holder<T> b) {
T temp = a.value; a.value = b.value; b.value = temp;
}