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

你可能感兴趣的文章
OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
查看>>
OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
查看>>
OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
查看>>
OAuth2.0_授权服务配置_授权码模式_Spring Security OAuth2.0认证授权---springcloud工作笔记144
查看>>
OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
查看>>
OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
查看>>
OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
查看>>
oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
查看>>
OAuth2.0四种模式的详解
查看>>
OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
查看>>
oauth2登录认证之SpringSecurity源码分析
查看>>
OAuth2:项目演示-模拟微信授权登录京东
查看>>
OA系统多少钱?OA办公系统中的价格选型
查看>>
OA系统选型:选择好的工作流引擎
查看>>
OA让企业业务流程管理科学有“据”
查看>>
OA项目之会议通知(查询&是否参会&反馈详情)
查看>>
Vue.js 学习总结(13)—— Vue3 version 计数介绍
查看>>
OA项目之我的会议(会议排座&送审)
查看>>
OA项目之我的会议(查询)
查看>>
OA项目之我的审批(会议查询&会议签字)
查看>>