• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strings

 
Shumani Dolresia Edzani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's easy to test in a small program. Some of these don't even compile.
 
Frank Hinkel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shumani Dolresia Edzani,


Please edit your post to QuoteYourSources.
Mock questions without valid sources need to be removed from Javaranch.
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh, I counter question you.

How many objects are created in

String s1 = new String("hello")


1. 1
2. 2
3. 3
4. 4
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one object.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 object actually. One on the literal pool and the other on the heap.
 
Afzal Hossain
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mariusz Szpiler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic