forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCheat.java
More file actions
237 lines (166 loc) · 6.94 KB
/
StringCheat.java
File metadata and controls
237 lines (166 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
# String
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Slightly magic class because of:
- literals
- interning and efficient bytecode representation: http://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings/29978564#29978564
- `+` operator
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Immutable. Therefore not a Collection, which would require `add`.
It implements `CharSequence` instead.
*/
public class StringCheat {
public static void main(String[] args) {
/*
# String literals
String literals are dynamically allocated just like using new String.
*/
{
assert "ab".getClass() == String.class;
/*
# Heredoc
# Multiline string
# String with quotes without escaping
Impossible:
- http://stackoverflow.com/questions/878573/java-multiline-string
- http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote
*/
}
/*
# length for String
Is a method, and not the magic `.length` field, since String is not an Array.
*/
{
assert "abc".length() == 3;
}
/*
# Compare strings
# == operator for strings
# intern
# String pool
# Interning
- http://stackoverflow.com/a/29978564/895245
- http://stackoverflow.com/a/513839/895245
Unlike `+`, `==` is *not* magic for strings,
and like for Object compares instances instead of content.
What *is* magic, is that:
- compile time constants resolve to `String.intern()` (on the bytecode level)
so `==` works for them.
- `+` for compile time constants is done at compile time.
So `==` works in that case.
TODO is `+` the only operator on strings?
This behavior is specified by:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5
This language design pattern is called "String interning",
and it can also exist for other immutable object literals like all literals in Ruby.
For you sanity, just don't rely on interning and always use `equals`.
*/
{
assert "abc" == "abc";
assert "abc" != new String("abc");
assert "abc" == new String("abc").intern();
}
/*
# Concatenate strings
# plus operator for strings
# + operator for strings
The `+` operator for strings is magic:
- it is overloaded for an object, but Java does not have operator overload.
- it generates compile time constants for strings like it does for primitive types
`StringBuilder` and StringBuffer` are like a mutable version.
Use them for multiple concatenations, otherwise a new buffer may be allocated every
time with `+` and `concat.`
The compiler optimizes `+` into `StringBuilder` calls, but only does so naively:
it does not seem to work for concatenations inside a loop:
http://stackoverflow.com/questions/14927630/java-string-concat-vs-stringbuilder-optimised-so-what-should-i-do
So always use:
- `StringBuilder` for computationally built computations
- `+` for simple compile time constants and interpolation
*/
{
// +
assert ("ab" + "cd").equals("abcd");
assert ("ab" + 1).equals("ab1");
assert (1 + "ab").equals("1ab");
// +=
{
String s = "ab";
s += "cd";
assert s.equals("abcd");
}
// Guaranteed to be done at compile time,
// and be the same object.
assert "ab" + "cd" == "abcd";
/*
+ vs concat:
http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator
- `+` can do String convertions, concat not.
- `+` is compiled at compile time, concat not.
*/
// ERROR. Unlike C, does not work.
// But `+` is guaranteed to be dealt with at compile time,
// see the section on String equality.
//assert "ab" "cd" == "abcd";
// Multiline string literals. Nope:
// http://stackoverflow.com/questions/878573/java-multiline-string
}
/*
# charAt
Get character at given position.
*/
{
assert "ab".charAt(0) == 'a';
// ERROR: array required, but string found.
// Brace magic is only for arrays.
// Furthermore, Strings are immutable,
// and brace notation invites mutability.
//assert "ab"[0] == "a";
/*
4-byte UTF-16 characters are returned as two char...
To deal with those properly, you need to use the codePointXXX methods.
Just imagine how much Java code has been broken by this...
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#codePointAt%28int%29
This is of course like this because when Java was developed, UTF-16 was the hot encoding,
and the 4-byte extension did not exist yet, and UTF-8 was not popular at the time.
*/
{
// TODO example
}
}
/*
# toCharArray
Makes a copy.
*/
/*
# Iterate string characters
http://stackoverflow.com/questions/196830/what-is-the-easiest-best-most-correct-way-to-iterate-through-the-characters-of-a
For loop with explicit variable.
*/
/*
# repr like in Python
Not by default: http://stackoverflow.com/questions/1350397/java-equivalent-of-python-repr
*/
/*
# split
Split string into an array of strings.
*/
/*
# join
Join an array of strings with a given separator.
Does not exist in the stdlib of Java 7:
http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp
But added to Java 8:
http://stackoverflow.com/a/26195047/895245
To implement, use StringBuilder, then:
- use `sb.setLength(sb.length() - 1)` at the end. Likely the best possibility.
- iterator hasNext check
- use array and stop at i - 1
- modified separator pattern: http://stackoverflow.com/a/3395345/895245
*/
/*
# startsWith
# endsWith
Boolean checks.
*/
}
}