Stupid question, but still.
There is such code:

int i = 10; String s = "privet " + i; System.out.println(s); // привет 10 i = 20; System.out.println(s)// опять "привет 10", хотя переменная i изменилась 

Is it possible to somehow update what is in the variable s without assigning a value to it again?

  • four
    Java string values ​​are immutable. The variable s is a reference to a certain string value (it does not change), if it is necessary for s to refer to a new one, then it must be created and assigned to s. - avp

3 answers 3

 class MyClass{ String str; public MyClass(String str){ this.str = str; } public void Show (int i ){ System.out.println(str + " " + i); } 

}

 public class Main { public static void main(String[] args) { MyClass my = new MyClass("Privet"); my.Show(10); //privet 10 my.Show(20); //privet 20 } 

}

So you can still ...

    create a class S , which will have an attribute i . Change this attribute.

    • class lol {int i; String get_my_string () {return "zaika" + this.i; }} lol l = new lol (); li = 10; System.out.println (l.get_my_string ()); li = 20; System.out.println (l.get_my_string ()); - Evgenii

    only not "zanoGo", but "zanoVo." Sorry for the pick) Read about modifiable / unmodifiable types. In general, all objects in Java are passed by reference, except for primitive types, they are transmitted by copying. Those. line:

     String s = "privet " + i; 

    Generates a new line object, where it will copy the value of i. Perhaps you need something like this:

     class SomeClass { int i; public String toString() { return "someString" + i; } } 

    But personally, my opinion, like (with pointers) like to do things in the SI, in “they don't like java” (c) =)

    • one
      In Sy such too. There was something similar in Algol-60 with parameters of functions / procedures (passing parameters "by name - by name"). Perhaps somewhere there is now. - alexlz
    • I meant, passing the reference to the argument that is modified inside the function, but used, then not it, without an explicit return from the function, somehow crookedly explained ... well, lan, not even in the mood, I am today) - JEcho
    • one
      Judging by the question of String, @kanarisisgod is really not really necessary for an object of class C1, whose member refers to another object (class C2), and as a result of calling C1.toString (), the current value of the independently modified member from C2 appears. - @alexlz, the analogy with the parameters by name in the algorithm here is also not entirely correct, because there such a parameter (with the same behavior) existed only inside the called procedure, the TC wants to link to the string, the value of which during each call is a function of a local variable. - avp
    • hmm ... "and the author wanted to say that the curtains are blue" (c) IMHO: it was meant that the question was about links and references to them. Those. the question is why the dependent value has not changed after the element has changed. And the question is quite standard, because everywhere they write that Java passes the value by reference, that is why I pointed to changeable / immutable types, that primitive types are passed by copying, and objects by reference. An example of an object is the answer to the question on the forehead). The analogy with C was, for the fact that in C, functions often modify arguments within themselves. Something like that =) - JEcho