Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the pool change?

Tags:

java

string

pool

I have two questions:

public static void main(String[] args) {
  String s1 = "bla";
  String s2 = "b" +"l" + "a";
  String s3 = "b".concat("l").concat("a");

  if(s1 == s2) 
        System.out.println("Equal");
  else
        System.out.println("Not equal");
  if(s1 == s3) 
        System.out.println("Equal");
  else
        System.out.println("Not equal");
}
  • Why does s1 and s2 point to the same object, whereas s1 and s3 doesn't? (There is no usage of new keyword).

  • If I get a string from the user and add to the above code these lines:

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    String name=in.readLine();
    if(name.equals("test"))
        s1 = s1 + "xyz";
    

    If the user enters xyz the program will print Not equal, when the user enters another thing the program outputs Equal. Does this mean that the pool changes through the execution of the whole program? Does the optimizer works at the compile time and continues to work in the runtime?

like image 919
Maroun Avatar asked Jan 21 '13 21:01

Maroun


People also ask

How often does pool water get changed?

You should replace pool water every five to seven years. As much as possible, you should drain and refill your pool during mild weather. It's to avoid pool damage caused by direct sunlight and heat. Moreover, a pool maintenance company can recommend the ideal time to drain your pool.

Does pool water get changed?

Generally, pool water needs to be replaced once every five to seven years. This should be done during mild weather so that your pool surface is not at risk from strong sunlight and heat. Your pool maintenance company can recommend when it is time to drain your pool.

How many pool changes a day?

This essentially means that for your pool water to be adequately disinfected and filtered, the entire volume of water needs to pass through the filter and sanitation system at least three to four times per day. In order to do this, you will need to set your pool pump at the correct flow rate for the size of your pool.

How long does pool season last?

While the opening and closing of your pool will rely partially on the weather, most people agree that May through the end of September is the main swimming pool season. The sooner you schedule your pool opening, the sooner you will be able to relax and enjoy!


1 Answers

Why does s1 and s2 point to the same object, whereas s1 and s3 doesn't? (There is no usage of new keyword).

Because the concatenation happens at compile time, and the completed string therefore goes in the constant pool the same as in the first example. It's a special case "known" to the compiler. It's really meant so that long strings, concatenated this way over several lines, still get the benefit of the same performance improvements as simple string constants.

In the second example, you're performing the calculation at runtime, so it won't be part of the constant pool.

Note however that in the JLS the details of what can and can't go in the string constant pool is deliberately left vague, so different implementations may optimise in different ways. It specifies certain rules as to what has to go in there, but don't rely on this behaviour being consistent across implementations.

like image 96
Michael Berry Avatar answered Oct 28 '22 09:10

Michael Berry