`
standalone
  • 浏览: 613005 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

How many string objects are created?

    博客分类:
  • java
阅读更多
This is a very common java interview question.

Given the following:

String x = new String("xyz");

y="abc";

x=x+y;

How Many Strings have been created:

A - 2

B - 3

C - 4

D - 5

To answer this question, I suggest you to read

http://www.javaranch.com/journal/200409/Journal200409.jsp
分享到:
评论
1 楼 standalone 2013-06-01  
One answer I agree with:

引用
Whenever we create a String object..that is created in heap.
Now if the object is created as a String Literal(Not using a String Constructor),then only a reference of the Object that is created in heap is stored in the StringPool.

Take this Example.

class A

{

...

A a1=new A();

A a2=a1;

A a3=a2;

}

Here Only one object is created in the heap, and a1,a2,a3 only are the references of that Object.

Since the question is for Objects not References, so 3 objects are created in the heap.


相关推荐

    JAVA中String介绍及常见面试题小结

    1. How many Objects created with: `String str=new String(“abc”)`?Answer: 创建了两个对象,一个在堆中,一个在常量池中。 2. `String s1 = "a" + "b" + "c";// 在编译时就变成 abc 常量池中创建abc`,`String...

    Professional.MFC.with.VC6

    How are Message Maps Created? The BEGIN_MESSAGE_MAP() Macro Inside the Message Map Filling the Holes The END_MESSAGE_MAP() Macro Unfolding the Map There's No Sense of Obligation CObject Memory...

    JSP Simple Examples

    In java, Strings are objects that belong to class java.lang.String. A string is a sequence of simple characters. We can get the length of the string by using the method length() of java.lang.String. ...

    python3.6.5参考手册 chm

    PEP 446: Newly Created File Descriptors Are Non-Inheritable Improvements to Codec Handling PEP 451: A ModuleSpec Type for the Import System Other Language Changes New Modules asyncio ensurepip ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    link ▶This style guide contains many details that are initially hidden from view. They are marked by the triangle icon, which you see here on your left. Click it now. You should see "Hooray" appear ...

    微软内部资料-SQL性能优化3

     Differentiate locks, latches, and other SQL Server internal “locking” mechanism such as spinlocks and other synchronization objects. Recommended Reading  Chapter 14 “Locking”, Inside SQL ...

    Sakemail

    The default priority for each msg created will be prNormal, so you don‘t need to change your code any bit.1.8.6- Ulf Sturegren has added D4 compatibility, not many changes to the source (one letter)...

    Java邮件开发Fundamentals of the JavaMail API

    things like the last mail received and calculate how many are new for you. So, when using the JavaMail API, if you want this type of information, you have to calculate it yourself. IMAP IMAP is a...

    VB编程资源大全(英文源码 数据库)

    dbCoder131.zip This code shows how to take data from a VB database application and place it into an Excel spreadsheet.<END><br>30 , Family_v2_upload_Apr_05_2000.zip This is a family address book...

    数位板压力测试

    • Many tablets have many configurable features and types of input information. • Tablets often control the system cursor, provide additional digitizing input, and provide template or macro ...

    VB编程资源大全(英文源码 控制)

    status.zip This example demonstrates how to display text about each control the mouse is currently above.<END><br>5 , dm10e.zip This COM object allows you to send SMTP mail from many of the ...

    php.ini-development

    Output buffering is a mechanism for controlling how much output data ; (excluding headers and cookies) PHP should keep internally before pushing that ; data to the client. If your application's ...

Global site tag (gtag.js) - Google Analytics