Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Please describe the output of the following program!

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Please describe the output of the following program!

    I have seen this interview question on a site and executed it

    code:
    public class Main
    {
    public static void main(String l[])
    {
    String a,b;
    a="First";
    b="Second";
    swap(a,b);
    System.out.println(a);
    System.out.println(b);
    }
    private static void swap(String a, String b)
    {
    String temp=a;
    a=b;
    b=temp;
    }
    }
    The output is:
    First
    Second

    but i was expecting
    Second
    First

    can any one explain how??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please describe the output of the following program!

    Java passes args by value not reference. The swap method changed the values of the args it received, not the values of the variables whose values were passed to it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Please describe the output of the following program!

    Hi,

    As norm said you changed only args values. So better you do one thing put print statement with in swap method.

    private static void swap(String a, String b)
    {
    String temp=a;
    a=b;
    b=temp;
    System.out.println(a);
    System.out.println(b);
    }
    }

    Thanks,

Similar Threads

  1. My program always gives me an output of 0.0, what's wrong with it?
    By thepiranha in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2013, 12:29 AM
  2. Replies: 7
    Last Post: February 26th, 2013, 02:21 AM
  3. I/O Program output error
    By gatorsgirl in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: April 11th, 2012, 04:18 PM
  4. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM
  5. No Output in Program:
    By bengregg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2011, 11:01 PM