博客
关于我
用new创建对象和用字符串常量创建对象的区别
阅读量:355 次
发布时间:2019-03-04

本文共 2220 字,大约阅读时间需要 7 分钟。

String???Java??????????????

1. new String("hello")?"hello"?????

?Java?????????????????????????????String s1 = new String("hello")?String s2 = "hello"???????????

1.1 ??????
  • s1 = new String("hello")?

    • ??main()??????
    • ???????s1????new????????
    • new????????????????
    • ???????s1??????
    • "hello"???????String?????
    • Java???????????String Constant Pool?????"hello"??????
    • ???????????????????????"hello"??????????????String?????
    • s1?????????????????????
  • s2 = "hello"?

    • "hello"??????????String?????
    • Java??????????????"hello"??????
    • ??????????????????????????????s2?
    • s2?????????????????"hello"?????????
1.2 ????
  • s1 == s2?

    • ??????String????????
    • ??s1?s2?????????????????????????????????????
    • ???s1 == s2????false?
  • s1.equals(s2)?

    • String????equals()??????????????????
    • "hello"?????????String????????
    • ???s1.equals(s2)????true?
1.3 ???????
  • s1????????????????"hello"???????
  • s2?????????????"hello"?????????
  • ??????????????==?????false?

2. String???????????

?Java??????????????????????????????????????????

String s3 = "helloworld";String s1 = "hello";String s2 = "world";String temp = s1 + s2;String finalStr = s3 + "test";
2.1 temp = s1 + s2
  • s1 + s2???????????String???
  • ????????????????????????????????
  • ????????s1?s2?????????
  • ?????????????String?????
  • temp??????????????
2.2 finalStr = s3 + "test"
  • "test"??????????String?????
  • Java??????????????"test"????
  • ???????????????????????
  • s3 + "test"???????????String???
  • ??String??????????s3?"test"?????????
  • finalStr??????????????
2.3 ????
  • temp == s1 + s2?

    • temp?????????s1 + s2????????
    • ???temp == s1 + s2????true?
  • **finalStr == "helloworld" + "test"`?

    • "helloworld" + "test"???????????String???
    • ??String??????????"helloworldtest"???????
    • finalStr?????????"helloworldtest"???????????
    • ???finalStr == "helloworld" + "test"????true?
  • temp.equals(s1 + s2)?

    • temp???????s1 + s2?????????
    • ???temp.equals(s1 + s2)????true?
  • finalStr.equals("hello" + "world")?

    • "hello" + "world"?????????????finalStr?????????
    • ???finalStr.equals("hello" + "world")????true?

3. ??

  • **String s = new String("hello")**???2????

    • 1??????????????"hello"???????
    • 0??????????????????"hello"??????
  • **String s = "hello"**????1????

    • ??????????????"hello"??????
  • ????????

    • ??????????????????????
    • ???????????????????????????????
  • ???new??????????????????????????????????

转载地址:http://ywqe.baihongyu.com/

你可能感兴趣的文章
Objective-C实现factorial阶乘算法(附完整源码)
查看>>
Objective-C实现Factors因数算法(附完整源码)
查看>>
Objective-C实现Farey Approximation近似算法(附完整源码)
查看>>
Objective-C实现Fast Powering算法(附完整源码)
查看>>
Objective-C实现Fedwick树算法(附完整源码)
查看>>
Objective-C实现fenwick tree芬威克树算法(附完整源码)
查看>>
Objective-C实现FenwickTree芬威克树算法(附完整源码)
查看>>
Objective-C实现fermat little theorem费马小定理算法(附完整源码)
查看>>
Objective-C实现FermatPrimalityTest费马素数测试算法(附完整源码)
查看>>
Objective-C实现fft2函数功能(附完整源码)
查看>>
Objective-C实现FFT快速傅立叶变换算法(附完整源码)
查看>>
Objective-C实现FFT算法(附完整源码)
查看>>
Objective-C实现fibonacci search斐波那契查找算法(附完整源码)
查看>>
Objective-C实现fibonacci斐波那契算法(附完整源码)
查看>>
Objective-C实现fibonacci斐波那契算法(附完整源码)
查看>>
Objective-C实现FIFO(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>
Objective-C实现finding bridges寻找桥梁算法(附完整源码)
查看>>
Objective-C实现first come first served先到先得算法(附完整源码)
查看>>
Objective-C实现FIR滤波器(附完整源码)
查看>>