Move articles out of java-strings part2
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CharSequenceVsStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUsingString_whenInstantiatingString_thenWrong() {
|
||||
CharSequence firstString = "bealdung";
|
||||
String secondString = "baeldung";
|
||||
|
||||
assertNotEquals(firstString, secondString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalCharSequences_whenCastToString_thenEqual() {
|
||||
CharSequence charSequence1 = "baeldung_1";
|
||||
CharSequence charSequence2 = "baeldung_2";
|
||||
|
||||
assertTrue(charSequence1.toString().compareTo(charSequence2.toString()) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenAppended_thenUnmodified() {
|
||||
String test = "a";
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test += "b";
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertNotEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringBuilder_whenAppended_thenModified() {
|
||||
StringBuilder test = new StringBuilder();
|
||||
test.append("a");
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test.append("b");
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
public class StringBufferStringBuilder {
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StringBufferStringBuilder.class.getSimpleName())
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class MyState {
|
||||
int iterations = 1000;
|
||||
String initial = "abc";
|
||||
String suffix = "def";
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public StringBuffer benchmarkStringBuffer(MyState state) {
|
||||
StringBuffer stringBuffer = new StringBuffer(state.initial);
|
||||
for (int i = 0; i < state.iterations; i++) {
|
||||
stringBuffer.append(state.suffix);
|
||||
}
|
||||
return stringBuffer;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public StringBuilder benchmarkStringBuilder(MyState state) {
|
||||
StringBuilder stringBuilder = new StringBuilder(state.initial);
|
||||
for (int i = 0; i < state.iterations; i++) {
|
||||
stringBuilder.append(state.suffix);
|
||||
}
|
||||
return stringBuilder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user