• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reference variable doubt

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi in one of the DAN question


class MWC106 {
static void m1(String s) {
s = s.trim(); s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}}

What is the result of attempting to compile and run the program?

a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compile-time error
h. Run-time error
i. None of the above


then answer is "b"


with explaination... as follow

The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged


but i don't understand why this answer is correct ...because when ever the reference variable to method is passed ...another reference variable ( in method called) now contains the address of the object in memeory...
so change through reference variable even in method will change the actual object ...

this is what i have read ..but its contradictory to the above answer...

pls expain the rt answer and why it right ??

thanx
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but i don't understand why this answer is correct ...because when ever the reference variable to method is passed ...another reference variable ( in method called) now contains the address of the object in memeory...
so change through reference variable even in method will change the actual object ...



Remember that strings are constant -- they can't be changed. All changes are done by changing the reference. So all the method did was change the reference to point to something else -- not change the original object.

Of course, the answer would have be different if it was a stringbufffer object.

Henry
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i got it...

when ever the referense is past of string constant to other reference variaible both refer to same string constant
and if 2nd reference variable changes the string ..then another string is created and refer to that object...

for eg.

String a= "first object";
String b=a; // now both referencing to same object...
b=b.concat(" hi"); // now another object created having value "first
//object hi"


is i m rt sir ??

thanx for your reply...

one more thing....

can you tell me how to master the topic "threads"

i wanted to make webserver.....how much advanced thread programming is needed...

to be honest ... i have not made any much thread program so bit afraid of it...

just give me some inspiration to make me thread master....
which resourse to read..other then ur book
 
reply
    Bookmark Topic Watch Topic
  • New Topic