aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strings" Watch "strings" New topic
Author

strings

Shumani Dolresia Edzani
Greenhorn

Joined: Mar 28, 2008
Posts: 4
Which of the following statements assigns "Hello Java" to the String variable s?

a) String s = "Hello Java";
b) String s[] = "Hello Java";
c) new String s = "Hello Java";
d) String s = new String("Hello Java");
and explain why?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
That's easy to test in a small program. Some of these don't even compile.
Frank Hinkel
Greenhorn

Joined: Mar 28, 2008
Posts: 24
a) String s = "Hello Java";

works
creates a string reference variable s and a new string object with the value "Hello Java" and assigns it to so

b) String s[] = "Hello Java";
ouch!
creates a reference variable for a string array and a new string object with the value "Hello Java" and tries to assign this string object to a string array reference variable. doesnt match!
a array is a array is array (is a object ;-))

c) new String s = "Hello Java";
"new" is needed if you want to create a new object, you never use new to create a reference variable. wrong syntax

d) String s = new String("Hello Java");
its the same lik a), with the difference that you explicit create a new object. a
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Shumani Dolresia Edzani,


Please edit your post to QuoteYourSources.
Mock questions without valid sources need to be removed from Javaranch.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
It won't surprise me that if the mock question said the answer was only option a) . At least in the real exam (as of SCJP 1.5 I think) you WILL be told how many answers to choose.

With the reason that option d) may be discounted is that it creates a *new* String, rather than using one created from the string pool as in option a).
The question did not mention anything about creating objects (eligable for GC).

I hope that's cleared things up/confused you
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
b) String s[] = "Hello Java";
ouch!
creates a reference variable for a string array and a new string object with the value "Hello Java" and tries to assign this string object to a string array reference variable. doesnt match!
a array is a array is array (is a object ;-))

Can you explain me in details of this .

c) String s "Hi"
String s1 = new String("hello")
what is the difference between above two.

Can anyone please explain in detail


Thanks<br />Dinesh
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Dinesh, I counter question you.

How many objects are created in

String s1 = new String("hello")


1. 1
2. 2
3. 3
4. 4


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Sandeep Bhandari:
Dinesh, I counter question you.

How many objects are created in

String s1 = new String("hello")


1. 1
2. 2
3. 3
4. 4



2
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Avi Sridhar:



2


Sorry about that , the answer is 1

String s1 = new String("Hello Java");

this one explicitly creates ONE new String object

it is same as

String s1 = "Hello Java";
yu yong
Ranch Hand

Joined: Mar 09, 2008
Posts: 44
To Avi:
I think the correct is 2.

String s1 = new String("Hello Java");

new String("Hello Java") is a String object on the heap. Besides a String values "Hello Java" is created on the String Pool of heap. But this is not a object.
s1 is another object which is the reference on the stack.


Yours sincerely,<br />yuyong<br /> <br /> <br /> E-mail:yuyong22@hotmail.com<br /> msn: yuyong22@hotmail.com<br /> Skype:yuyong88
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Only one object.
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
2 object actually. One on the literal pool and the other on the heap.
Afzal Hossain
Greenhorn

Joined: Jan 02, 2006
Posts: 25
hi All,

String s1 = new String("Hello Java");

Only 1 object is created on the heap.
There is no instance in string pool is created because if i write:

String s1 = new String("Hello Java");
String s2 = "Hello Java";
System.out.println(s1==s2);
It will print "false" which means there is no instance in the string pool.


Afzal
Mariusz Szpiler
Greenhorn

Joined: Nov 10, 2007
Posts: 1
See in study guide chapter 6 page 420 .

For below:

String s = new String ("abc");

there is an explanation - quote -

"In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool."

Could You explain this?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strings