Results 1 to 8 of 8

Thread: PassByValues?

  1. #1
    Bungkai is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default PassByValues?

    Please redirect me if I'm in the wrong place, but I'm new here and I think this material relatively matches New To Java..maybe.

    I have a few questions.
    This is the client I believe..
    Java Code:
    import java.util.*;
    import java.io.PrintStream;
    
    public class PassByValueClient
    {
          public static void main(String[] args)
          {
                     PrintStream output = System.out;
                     int a = 1;
                     PassByValue.add(a);
                     output.println(a);
                     Set<String> b = new HashSet<String>();
                     PassByValue.add(b);
                     output.println(b.size());
                     List<Integer> c = new ArrayList<Integer>();
                     PassByValue.add(c);
                     output.println(c.size());
          }
    }

    This is the utility
    Java Code:
    import java.util.*;
    
    public class PassByValue
    {
       
        private PassByValue() {}
    
       
        public static void add(int a)
        {
    	a = a + 1;
        }
    
    
        public static void add(Set<String> b)
        {
    	b = new HashSet<String>();
    	b.add("ABC");
        }
    	
    
        public static void add(List<Integer> c)
        {
    	c.add(new Integer(1));
        }
    }
    If this needed to be stated, I'm using Java Eclipse.

    I was asked to predict the outcome of this on runtime. I predicted that it would look something like this:

    1
    A
    B
    C
    1

    The outcome came:

    1
    0
    1

    Does the PassByValue utility default strings to 0 or something?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where do you think the A B C is being output? Note that there should be three lines out output here, one for each time output.println(...) is called, so there can't be 5 lines out output as your guess shows. Also please note that all output lines will be numbers, the first one the results held by the variable a, the next two the size of two collections. Again, there is no way that any letters can be output.

    This statement:
    Does the PassByValue utility default strings to 0 or something?
    doesn't make sense, so the answer is "no".

  3. #3
    Bungkai is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Oh I see (I realized how stupid the 5 line thing was lol), well that brings up another question that I want to ask. As you can see, it should add "ABC" to the HashSet b but when I try to display it with output.println(b); it only shows me '[]' How am I supposed to display the contents of the HashSet?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's displaying the HashSet correctly as it is empty. Because the method creates a new HashSet within it, "ABC" is added to this new HashSet, but is not to the HashSet object that was passed to the method.

  5. #5
    Bungkai is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I'm not sure I follow, The HashSet is created within the method, ABC was added to it, but the HashSet object wasn't passed to the method?

    I still don't really understand why it's empty :x.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The main method has a HashSet object held in a HashSet variable which you call b, and it passes a reference to the HashSet object into the add(Set...) method. Within this method, the b variable is given a new HashSet object, and "ABC" is added to the new HashSet object. But despite this, the main method's HashSet variable still points to the old HashSet object, one that never received "ABC".

  7. #7
    Bungkai is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Ohhhhh, even your latest post took a little time to sink in, but I get it now lol. Thanks a lot!..and expect a lot of questions from me lol.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •