Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings information required

 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
What and why will be the result of the following piece of code:

String s = "Test1";

public void testFunction(String s)
{
s="Test2";
}

testFunction("test3");

System.out.println(s);
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bilal Ali wrote:
What and why will be the result of the following piece of code:


please use code tag to post your code. and also post the program with good format . Coming to your question , you will get the answare "Test1" due to shadowing of the variable
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output will be "Test1"

Because in sysout the variable referred is a global variable and not local 's' declared inside the function.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/CallByReferenceVsCallByValue
 
Clyde DeSouza
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need for call by reference and call by value

put your code inside proper format anyways i will give you the answer i guess you wanted to refer to class variable but instead your referring to method variable you could have refered to class variable if you used the different name over here but you used the same name as method name to refer to class variable in such situation just use
this.<variable name>=<local method variable name>
in your case here
this.s=s;

this refers to current object and you are saved from ambiguity! got it any more queries do post it here
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Joywish Man" welcome to JavaRanch
 
Clyde DeSouza
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot !
 
James Mathews
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how this is refering to current object?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic