cool hit counter as far as sth is concernedString targets, Can be used"=" assign a value to, You can also use"new" Keyword assignment, What's the difference between the two approaches?_Intefrankly

as far as sth is concernedString targets, Can be used"=" assign a value to, You can also use"new" Keyword assignment, What's the difference between the two approaches?


When you see the title, you might subconsciously wonder what the difference is between the two approaches.

Look at the following demos, which naturally differentiate

 1 /**
 2  * 
 3  */
 4 package com.b510.test;
 5 
 6 /**
 7  * Problem:
 8  *      as far as sth is concernedString targets, Can be used"=" assign a value to, You can also use"new" Keyword assignment, What's the difference between the two approaches?
 9  *      i.e.:
10  *       String testStrA = "abc";
11  *       String testStrB = new String("abc");
12  *     
13  * @author Jone Hongten
14  * @create date:2013-10-31
15  * @version 1.0
16  */
17 public class StringTest {
18 
19     public static void main(String[] args) {
20         String testStrA = "abc";
21         String testStrB = new String("abc");
22         System.out.println("testStrA == testStrB ? " + (testStrA == testStrB ? true : false));
23         
24         String testStrC = "abc";
25         System.out.println("testStrA == testStrC ? " + (testStrA == testStrC ? true : false));
26         
27         String testStrD = "ab";
28         String testStrE = "c";
29         String testStrF = testStrD + testStrE;
30         System.out.println("testStrA == testStrF ? " + (testStrA == tsetStrF ? true : false));
31     }
32 }

Running effect.

testStrA == testStrB ? false
testStrA == testStrC ? true
testStrA == testStrF ? false

Let's analyze why the results are as they are.

The first thing we need to understand is that

1 String testStrA = "abc";

With this code, an object may or may not be created: the term "pool of string instances" appears here

There are strings in the instance pool

That's a very graphic term., In this pool of string instances, Lots of strings are stored, may contain the string:"abc", consequently

In this case., The above statement is not going to create targets of, Instead, it refers directly to the string in the pool:"abc";

String does not exist in the instance pool

If the string"abc" does not exist in the instance pool, Then at this point, will then initialize a string:"abc", i.e. creating

A string. targets:"abc", and will put the created string into the" String Instance Pool" in。

1 String testStrB = new String("abc");

For the keyword: new , that is, a new object will be created, that is, a new string object will be created each time

Thus, for the first result.

testStrA == testStrB ? false

That's how we knew it was going to be.

And for the second result.

testStrA == testStrC ? true

As long as we're interested in" String Instance Pool" There is an impression, It's not hard to understand。

Finally, a third result.

testStrA == testStrF ? false

This one is going to be slightly harder to understand.

1 String testStrD = "ab";
2 String testStrE = "c";
3 String testStrF = testStrD + testStrE;

The strings testStrD and testStrE are both string constants, so they are already determined at the time the code is compiled.

But for testStrF, his value is a reference to testStrD and testStrE, so it will not be determined at compile time, in fact

testStrF is similar to creating a new object out of it, and then assigning the reference of the created object to testStrF.

So it makes sense that a third result would occur...

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、DeepThe Long March to Technology 10000word article explaining the birth of Alis NASA program
2、Top 10 Hot Posts on Machine Learning in July with Machine Automation Programming Leading the Way
3、2018 National MultiSchool Algorithm Winter Break Camp Practice Competition Session 1 Sixth Son Punch
4、How to make a WordPress sidebar blog stats widget and integrate it in the theme
5、http protocol notes

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号