Introduction: Magic That Is Not Random
\n\nIf you've ever been stuck on Stack Overflow searching for an answer to a strange bug, you've probably seen the question that gathered thousands of views and heated discussions: "Why does this code using random strings print 'hello world'?". Sounds like a riddle, doesn't it? How can randomly generated strings form a meaningful greeting?
\n\nIn this article, we will not only unravel this SEO phenomenon (yes, it's a great case for search engine promotion of educational content), but turn it into a full-fledged lesson on Java. You will learn how string pools work, why hashCode() sometimes throws surprises, and what best practices you should adopt in 2025 to make your code predictable and efficient.
Ready? Let's go!
\n\n1. That Famous Code: What's Happening?
\n\nLet's recall the classic example that blew up Stack Overflow. Imagine you have a method that generates random strings and checks them for equality:
\n\npublic class RandomStringTest {\n public static void main(String[] args) {\n String[] randomStrings = new String[1000];\n for (int i = 0; i < 1000; i++) {\n randomStrings[i] = generateRandomString();\n }\n \n for (String s : randomStrings) {\n if (s.equals("hello world")) {\n System.out.println("Found: " + s);\n }\n }\n }\n \n public static String generateRandomString() {\n // Some strange logic\n return new StringBuilder()\n .append((char) (Math.random() * 256))\n .append((char) (Math.random() * 256))\n // ... many characters\n .toString();\n }\n}\n\n\nAt first glance, the chance of getting "hello world" randomly is (1/256)^11 — practically zero. But the code prints "hello world"! How? Randomness has nothing to do with it. The secret lies in the String pool and the specifics of hashing in Java.
\n\n2. The Solution: String Pool and hashCode
\n\nIn Java, strings are immutable objects. When you create a string via a literal ("hello world"), it goes into the String pool — a special memory area. If another string references the same value, the JVM does not create a new object but returns a reference to the existing one.
But in our example, strings are generated randomly. How do they still turn out to be equal? Answer: overridden hashCode(). In some versions of Java (especially before 8), the string hashing algorithm was predictable and could lead to collisions if strings had the same length and character composition. However, the real magic happens when the String pool uses interning.
\n\nWhen you call s.equals("hello world"), the JVM can optimize the comparison if both strings are in the pool. But in our case, the strings are different... Or are they? In fact, if you look at the implementation of generateRandomString() in the original question, there was a trick: the method returned a string that was always "hello world" due to an error in the generation logic.
Here is an example of such "randomness":
\n\npublic static String generateRandomString() {\n Random random = new Random(42); // Fixed seed\n byte[] bytes = new byte[11];\n random.nextBytes(bytes);\n return new String(bytes);\n}\n\n\nIf the seed is fixed, the "random" sequence is always the same. And if the seed is chosen to generate "hello world"? That's the answer to the riddle! The code is not random — it's deterministic.
\n\n