总结
总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。
如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。
public void testJoin() {
System.out.println(“>>> testJoin() <<<”);
long start = System.currentTimeMillis();
List list = new ArrayList();
for (int i = 0; i < max; i++) {
list.add(“a”);
}
long end1 = System.currentTimeMillis();
long cost1 = end1 - start;
StringUtils.join(list, “”);
long end = System.currentTimeMillis();
long cost = end - end1;
System.out.println(" {list.add(“a”)} cost1=" + cost1 + " ms");
System.out.println(" {StringUtils.join(list, “”)} cost=" + cost
+ " ms");
}
public void testStringBuffer() {
System.out.println(“>>> testStringBuffer() <<<”);
long start = System.currentTimeMillis();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < max; i++) {
strBuffer.append(“a”);
}
strBuffer.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {strBuffer.append(“a”)} cost=" + cost + " ms");
}
public void testStringBuilder() {
System.out.println(“>>> testStringBuilder() <<<”);
long start = System.currentTimeMillis();
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < max; i++) {
strBuilder.append(“a”);
}
strBuilder.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out
.println(" {strBuilder.append(“a”)} cost=" + cost + " ms");
}
}
> 测试结果:
- 执行100次, private static final int max = 100;
testPlus() <<<
{str + “a”} cost=0 ms
testConcat() <<<
{str.concat(“a”)} cost=0 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=20 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行1000次, private static final int max = 1000;
testPlus() <<<
{str + “a”} cost=10 ms
testConcat() <<<
{str.concat(“a”)} cost=0 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=20 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行1万次, private static final int max = 10000;
testPlus() <<<
{str + “a”} cost=150 ms
testConcat() <<<
{str.concat(“a”)} cost=70 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=30 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行10万次, private static final int max = 100000;
testPlus() <<<
{str + “a”} cost=4198 ms
testConcat() <<<
{str.concat(“a”)} cost=1862 ms
testJoin() <<<
{list.add(“a”)} cost1=21 ms
{StringUtils.join(list, “”)} cost=49 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=10 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=10 ms
- 执行20万次, private static final int max = 200000;
testPlus() <<<
{str + “a”} cost=17196 ms
testConcat() <<<
{str.concat(“a”)} cost=7653 ms
testJoin() <<<
{list.add(“a”)} cost1=20 ms
{StringUtils.join(list, “”)} cost=51 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=20 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=16 ms
- 执行50万次, private static final int max = 500000;
testPlus() <<<
{str + “a”} cost=124693 ms
testConcat() <<<
{str.concat(“a”)} cost=49439 ms
testJoin() <<<
{list.add(“a”)} cost1=21 ms
{StringUtils.join(list, “”)} cost=50 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=20 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=10 ms
- 执行90万次, private static final int max = 900000;
testPlus() <<<
{str + “a”} cost=456739 ms
testConcat() <<<
{str.concat(“a”)} cost=186252 ms
testJoin() <<<
{list.add(“a”)} cost1=20 ms
{StringUtils.join(list, “”)} cost=68 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=30 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=24 ms
> 查看源代码,以及简单分析
String contact 和 StringBuffer,StringBuilder 的源代码都可以在Java库里找到,有空可以研究研究。
- 其实每次调用contact()方法就是一次数组的拷贝,虽然在内存中是处理都是原子性操作,速度非常快,但是,最后的return语句会创建一个新String对象,限制了concat方法的速度。
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
- StringBuffer 和 StringBuilder 的append方法都继承自AbstractStringBuilder,整个逻辑都只做字符数组的加长,拷贝,到最后也不会创建新的String对象,所以速度很快,完成拼接处理后在程序中用strBuffer.toString()来得到最终的字符串。
/**
* Appends the specified string to this character sequence.
*
* The characters of the {@code String} argument are appended, in
* order, increasing the length of this sequence by the length of the
* argument. If {@code str} is {@code null}, then the four
* characters {@code “null”} are appended.
*
* Let n be the length of this character sequence just prior to
* execution of the {@code append} method. Then the character at
* index k in the new character sequence is equal to the character
* at index k in the old character sequence, if k is less
* than n; otherwise, it is equal to the character at index
* k-n in the argument {@code str}.
* @param str a string.
* @return a reference to this object.
*/
public AbstractStringBuilder append(String str) {
if (str == null) str = “null”;
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
总结
虽然我个人也经常自嘲,十年之后要去成为外卖专员,但实际上依靠自身的努力,是能够减少三十五岁之后的焦虑的,毕竟好的架构师并不多。
架构师,是我们大部分技术人的职业目标,一名好的架构师来源于机遇(公司)、个人努力(吃得苦、肯钻研)、天分(真的热爱)的三者协作的结果,实践+机遇+努力才能助你成为优秀的架构师。
如果你也想成为一名好的架构师,那或许这份Java成长笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。
总结
虽然我个人也经常自嘲,十年之后要去成为外卖专员,但实际上依靠自身的努力,是能够减少三十五岁之后的焦虑的,毕竟好的架构师并不多。
架构师,是我们大部分技术人的职业目标,一名好的架构师来源于机遇(公司)、个人努力(吃得苦、肯钻研)、天分(真的热爱)的三者协作的结果,实践+机遇+努力才能助你成为优秀的架构师。
如果你也想成为一名好的架构师,那或许这份Java成长笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。
[外链图片转存中…(img-B1RM9h5B-1715568121657)]