博客
关于我
用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/

你可能感兴趣的文章
NodeJS @kubernetes/client-node连接到kubernetes集群的方法
查看>>
Nodejs express 获取url参数,post参数的三种方式
查看>>
nodejs http小爬虫
查看>>
nodejs libararies
查看>>
nodejs npm常用命令
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 的 Buffer 详解
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
Nodejs中的fs模块的使用
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>