This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Assigning in Java?
(5 answers)
Closed 3 years ago.
PS: This question is different than others because I need to know exactly what code to write in order to prevent the variable from changing.
I have this example of code to demonstrate my problem:
float[][] one = {{0}};
float[][] two = one;
System.out.println(two[0][0]);
one[0][0] ++;
System.out.println(two[0][0]);
int three = 0;
int four = three;
System.out.println(four);
three ++;
System.out.println(four);
The output is:
0.0
1.0
0
0
For some reason, when I change float[][] 'one', 'two' automatically changes as well.
However, when I performed the same task but for a different data type int, then the code works as intended.
What is causing this variable to drag the other one with it?
Why doesn't it happen for the int type?
How do I fix it?
Related
This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001
This question already has answers here:
The left-hand side of an assignment must be a variable
(6 answers)
"The left-hand side of an assignment must be a variable" problem with charAt
(4 answers)
How do I edit my program in order to assign a variable to an array list
(2 answers)
Closed 2 years ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
In the following loop how may I create a new List<>(), but I have a problem in the moment I try to set a value:
ListlistOfString = new ArrayList()<>;
listOfString.set(index, value);
Can anyone explain me how does it work?
Unlike arrays, you cannot do array[i] = val. You should use ArrayList.set:
// this must be set
announcementDTO.set(j, new ArrayList<>());
// this can still be get
announcementDTO.get(j).setIdShop(shopBean.getId());
announcementDTO.get(j).setNombreComercio(comercioBean.getNombre());
announcementDTO.get(j).setRazonSocial(comercioBean.getRazonSocial());
This question already has answers here:
How to alter a float by its smallest increment (or close to it)?
(7 answers)
How to get the smallest next or previous possible double value supported by the architecture?
(2 answers)
Closed 5 years ago.
If I have a java float and I want to increment/decrement it by the minumim possible value how can I determine that value? I mean it is a difference if the value of the float is already a very large number like 3.4028235E38 or a very small number like 1.4E-45. In the first case to note any difference I need to subtract a far lager number as I would need to add to the second case right?
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I have a problem in this method(it is not complete but the problem is on those lines). The thing is that I have a matrix "x", but after going through the absMatriz function, it changes its values. I would like to know how to keep my x matrix, I have tried saving it in another matrix "www" but it does not work.
public Matriz[] problema(Matriz x){
Matriz www=new Matriz(x.m,x.n); //the parameters are the dimensions of the matrix
www= x;
double mm= max(absMatriz(x));
return www;
}
It seems that you're looking to make a deep copy of x before absMatriz mutates it, and return the unmutated deep copy.
See How do I copy an object in Java?
This question already has answers here:
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 7 years ago.
I don't consider myself to be bad at programming, but there's been something troubling me since the past few days.
int counter = 3;
++counter;
Is the following code above the same as counter++;.
It is similar, but not the same.
In your expression it doesn't matter, but if you had something more complicated, like System.out.println(counter++), it would make a big difference.
For example:
int counter = 3;
System.out.println(counter++)
This will print 3, then increment counter to 4.
However, if you do
int counter = 3;
System.out.println(++counter)
it will print 4 because it increments prior to giving the value as a parameter to the print function.
It's a question of when the increment is performed, the prefix performs it before other operations, postfix performs it after. They have different precedences.