How many String objects will be created when this method is invoked?

转载 2012年04月06日 12:03:06
String s="hello";
String s=new String("hello");
how many object and reference is created in heap and pool.

WHAT IS OUTPUT FOR TH FOLLOWING CODE?

public String makinStrings()
{
String s = "Fred";
s = s + "47";
s = s.substring(2, 5);
s = s.toUpperCase();
return s.toString();
}
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5

F. 6


Thanks a lot Ian and Abhijeet for your exlanation.
When I went through the link given by Ian,
I came across a thread , the reply given by Jim Yingst (wanderer Member # 290) was valuable.Thanks to Jim


"foo", "99" (two objects)created when class is loaded.
"foo99", "o99", "O99" (three objects) created when the method is run.

Since the question has been asked that how many objects will be created when the method is invoked?
The answer is 3.


Thank you guys!!




相关文章推荐

警告: Category is implementing a method which will also be implemented by its primary class

Category is implementing a method which will also be implemented by its primary class

how many objects can be locked per transaction

how many objects can be locked per transaction

linux系统使用sudo命令报xxx is not in the sudoers file.This incident will be reported.的解决方法

在使用sudo命令前时,用户确保在sudoers文件中已添加xxx ALL=(ALL) ALL (这里的xxx是你的用户名),否则 会报xxx is not in the sudoers file....

xxx is not in the sudoers file. This incident will be reported.

xxx is not in the sudoers file. This incident will be reported.ubuntu下使用sudo命令遇到如下问题: xxx is not ...
  • yyinhai
  • yyinhai
  • 2016年10月27日 11:00
  • 392

Linux---XXX is not in the sudoers file. This incident will be reported

源文: http://www.xuehuile.com/blog/54ec6e70e7ac4367926403e00cdaceda.html 一、 1 $wher...

Some ashmem based objects can not be created due to file descriptor leak

记录了Android系统上基于ashmem或Fd的一些对象由于进程内文件描述符耗尽不能被创建的Issues。 Example One. 06-22 22:38:05.949 27928 31688...

The context cannot be used while the model is being created

今天在使用 EF 的Code First 过程中,更新数据到数据库时, 执行到  context.UserPermission.Add(permission); 时提示错误信息:提供程序未返回 P...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How many String objects will be created when this method is invoked?
举报原因:
原因补充:

(最多只允许输入30个字)