Tabnine Logo
String.<init>
Code IndexAdd Tabnine to your IDE (free)

How to use
java.lang.String
constructor

Best Java code snippets using java.lang.String.<init> (Showing top 20 results out of 149,535)

origin: stackoverflow.com

 "abc" == new String("abc")    // true
"abc" === new String("abc")   // false
origin: stackoverflow.com

 // 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
origin: stackoverflow.com

 static String readFile(String path, Charset encoding) 
 throws IOException 
{
 byte[] encoded = Files.readAllBytes(Paths.get(path));
 return new String(encoded, encoding);
}
origin: square/okhttp

private static String repeat(char c, int count) {
 char[] array = new char[count];
 Arrays.fill(array, c);
 return new String(array);
}
origin: square/okhttp

public static String repeat(char c, int count) {
 char[] array = new char[count];
 Arrays.fill(array, c);
 return new String(array);
}
origin: google/guava

@Override
public String replaceFrom(CharSequence sequence, char replacement) {
 char[] array = new char[sequence.length()];
 Arrays.fill(array, replacement);
 return new String(array);
}
origin: square/okhttp

@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);
 }
}
origin: iluwatar/java-design-patterns

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);
 }
}
origin: google/guava

@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);
}
origin: google/guava

@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));
}
origin: google/guava

 @Override
 public String apply(String input) {
  return new String(BaseEncoding.base64().decode(input), Charsets.UTF_8);
 }
};
origin: google/guava

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));
}
origin: google/guava

@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));
}
origin: google/guava

 @Override
 void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
  String s = new String(new char[] {randomLowSurrogate(random)});
  for (PrimitiveSink sink : sinks) {
   sink.putUnencodedChars(s);
  }
 }
},
origin: google/guava

 @Override
 void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
  String s = new String(new char[] {randomHighSurrogate(random)});
  for (PrimitiveSink sink : sinks) {
   sink.putUnencodedChars(s);
  }
 }
},
origin: google/guava

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));
}
origin: google/guava

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));
}
origin: google/guava

@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();
}
origin: google/guava

 @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);
  }
 }
};
origin: google/guava

 @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);
  }
 }
},
java.langString<init>

Javadoc

Initializes a newly created String object so that it represents an empty character sequence. Note that use of this constructor is unnecessary since Strings are immutable.

Popular methods of String

  • equals
  • length
    Returns the number of characters in this string.
  • substring
    Returns a string containing a subsequence of characters from this string. The returned string shares
  • startsWith
    Compares the specified string to this string, starting at the specified offset, to determine if the
  • format
    Returns a formatted string, using the supplied format and arguments, localized to the given locale.
  • split
    Splits this string using the supplied regularExpression. See Pattern#split(CharSequence,int) for an
  • trim
  • valueOf
    Creates a new string containing the specified characters in the character array. Modifying the chara
  • indexOf
  • endsWith
    Compares the specified string to this string to determine if the specified string is a suffix.
  • toLowerCase
    Converts this string to lower case, using the rules of locale.Most case mappings are unaffected by t
  • contains
    Determines if this String contains the sequence of characters in the CharSequence passed.
  • toLowerCase,
  • contains,
  • getBytes,
  • equalsIgnoreCase,
  • replace,
  • isEmpty,
  • charAt,
  • hashCode,
  • lastIndexOf

Popular in Java

  • Making http requests using okhttp
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • getResourceAsStream (ClassLoader)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • Socket (java.net)
    Provides a client-side TCP socket.
  • TreeSet (java.util)
    TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are support
  • Modifier (javassist)
    The Modifier class provides static methods and constants to decode class and member access modifiers
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • Servlet (javax.servlet)
    Defines methods that all servlets must implement. A servlet is a small Java program that runs within
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now