本文整理了Java中java.lang.String.<init>()
方法的一些代码示例,展示了String.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。String.<init>()
方法的具体详情如下:
包路径:java.lang.String
类名称:String
方法名:<init>
[英]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.
[中]初始化新创建的字符串对象,使其表示空字符序列。请注意,由于字符串是不可变的,因此不需要使用此构造函数。
代码示例来源: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);
}
}
},
内容来源于网络,如有侵权,请联系作者删除!