// These two have the same value new String("test").equals("test") // --> true // ... but they are not the same object new String("test") == "test" // --> false // ... neither are these new String("test") == new String("test") // --> false // ... but these are because literals are interned by // the compiler and thus refer to the same object "test" == "test" // --> true // ... but you should really just call Objects.equals() Objects.equals("test", new String("test")) // --> true Objects.equals(null, "test") // --> false
static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); }
private static String repeat(char c, int count) { char[] array = new char[count]; Arrays.fill(array, c); return new String(array); }
public static String repeat(char c, int count) { char[] array = new char[count]; Arrays.fill(array, c); return new String(array); }
@Override public String replaceFrom(CharSequence sequence, char replacement) { char[] array = new char[sequence.length()]; Arrays.fill(array, replacement); return new String(array); }
@Override public @Nullable String getSelectedProtocol(SSLSocket socket) { try { byte[] alpnResult = (byte[]) getAlpnSelectedProtocol.invoke(socket); return alpnResult != null ? new String(alpnResult, UTF_8) : null; } catch (IllegalAccessException | InvocationTargetException e) { throw new AssertionError(e); } }
private void sendLogRequests(PrintWriter writer, InputStream inputStream) throws IOException { for (int i = 0; i < 4; i++) { writer.println(clientName + " - Log request: " + i); writer.flush(); byte[] data = new byte[1024]; int read = inputStream.read(data, 0, data.length); if (read == 0) { LOGGER.info("Read zero bytes"); } else { LOGGER.info(new String(data, 0, read)); } artificialDelayOf(100); } }
@Override public String read() throws IOException { // Reading all the data as a byte array is more efficient than the default read() // implementation because: // 1. the string constructor can avoid an extra copy most of the time by correctly sizing the // internal char array (hard to avoid using StringBuilder) // 2. we avoid extra copies into temporary buffers altogether // The downside is that this will cause us to store the file bytes in memory twice for a short // amount of time. return new String(ByteSource.this.read(), charset); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { checkNotClosed(); // It turns out that creating a new String is usually as fast, or faster // than wrapping cbuf in a light-weight CharSequence. target.append(new String(cbuf, off, len)); }
@Override public String apply(String input) { return new String(BaseEncoding.base64().decode(input), Charsets.UTF_8); } };
public void testHashCode() throws Exception { int h1 = Objects.hashCode(1, "two", 3.0); int h2 = Objects.hashCode(new Integer(1), new String("two"), new Double(3.0)); // repeatable assertEquals(h1, h2); // These don't strictly need to be true, but they're nice properties. assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, 2)); assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, null, 2)); assertTrue(Objects.hashCode(1, null, 2) != Objects.hashCode(1, 2)); assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(3, 2, 1)); assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(2, 3, 1)); }
@SuppressWarnings("deprecation") public void testInvalidUnicodeHashString() { String str = new String( new char[] {'a', Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE, 'z'}); assertEquals( murmur3_32().hashBytes(str.getBytes(Charsets.UTF_8)), murmur3_32().hashString(str, Charsets.UTF_8)); }
@Override void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) { String s = new String(new char[] {randomLowSurrogate(random)}); for (PrimitiveSink sink : sinks) { sink.putUnencodedChars(s); } } },
@Override void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) { String s = new String(new char[] {randomHighSurrogate(random)}); for (PrimitiveSink sink : sinks) { sink.putUnencodedChars(s); } } },
public void testEqual() throws Exception { assertTrue(Objects.equal(1, 1)); assertTrue(Objects.equal(null, null)); // test distinct string objects String s1 = "foobar"; String s2 = new String(s1); assertTrue(Objects.equal(s1, s2)); assertFalse(Objects.equal(s1, null)); assertFalse(Objects.equal(null, s1)); assertFalse(Objects.equal("foo", "bar")); assertFalse(Objects.equal("1", 1)); }
public void testAsFunction_simplistic() { String canonical = "a"; String not = new String("a"); Function<String, String> internerFunction = Interners.asFunction(Interners.<String>newStrongInterner()); assertSame(canonical, internerFunction.apply(canonical)); assertSame(canonical, internerFunction.apply(not)); }
@Test public void testRemoveEqualKeyWithDifferentReference() { String fooReference1 = new String("foo"); String fooReference2 = new String("foo"); assertThat(fooReference1).isNotSameAs(fooReference2); assertThat(mapCache.put(fooReference1, "bar")).isNull(); assertThat(mapCache.get(fooReference1)).isEqualTo("bar"); // ensure first reference is cached assertThat(mapCache.remove(fooReference2)).isEqualTo("bar"); assertThat(mapCache.get(fooReference1)).isNull(); }
@Override void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) { String s = new String(new char[] {randomHighSurrogate(random), randomLowSurrogate(random)}); for (PrimitiveSink sink : sinks) { sink.putUnencodedChars(s); } } };
@Override void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) { String s = new String(new char[] {randomLowSurrogate(random), randomHighSurrogate(random)}); for (PrimitiveSink sink : sinks) { sink.putUnencodedChars(s); } } },