aspose file tools*
The moose likes Beginning Java and the fly likes Strings information required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings information required" Watch "Strings information required" New topic
Author

Strings information required

Bilal Ali
Ranch Hand

Joined: Jun 14, 2007
Posts: 66
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 15, 2008
Posts: 131
The output will be "Test1"

Because in sysout the variable referred is a global variable and not local 's' declared inside the function.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

http://faq.javaranch.com/java/CallByReferenceVsCallByValue


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Clyde DeSouza
Greenhorn

Joined: Mar 16, 2009
Posts: 26
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

Joined: Oct 13, 2005
Posts: 39409
    
  28
"Joywish Man" welcome to JavaRanch
Clyde DeSouza
Greenhorn

Joined: Mar 16, 2009
Posts: 26
Thanks a lot !
James Mathews
Greenhorn

Joined: Mar 16, 2009
Posts: 7
how this is refering to current object?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings information required