Saturday 4 January 2020

java - strange copy values from one array to another

When I watched code examples in Java, I saw a strange code:




public class Application {
public static void main(String[] args) {
String[] x = {"A"};
String[] y = x;
x[0] = "B";
System.out.print(x[0] + " " + y[0]);
}
}



And I don't understand, why the result "B B" is correct? When I created and initialized an arrays x and y and assigned zero element of array x is equal to B, I think that an answer must be "B A".

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...