Optimisation du compilateur ?
Bonsoir,
lorsque je fait :
Code:
1 2 3 4
|
String a = "hello";
String b = "hello";
System.out.println(a == b); |
il m'affiche "true". Donc a priori, le compilateur décide de créer un seul objet. Est-ce que ce n'est pas dangereux ? Même si les String sont immuable, je me pose la question. D'autant que si je fais :
Code:
1 2 3 4
|
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); |
alors il m'affiche "false". pourquoi on optimise pas ici aussi ?