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: Learning - could someone explain this code to me?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Learning - could someone explain this code to me?

    class Passer {
     
        void toUpperCase(String[] text) {
            for (int i = 0; i < text.length; i++) {
                text[i] = text[i].toUpperCase();
            }
        }
     
        public static void main(String[] arguments) {
            Passer passer = new Passer();
            passer.toUpperCase(arguments);
            for (int i = 0; i < arguments.length; i++) {
                System.out.print(arguments[i] + " ");
            }
            System.out.println();
        }
    }

    Can someone explain this code to me? I'm not quite sure I understand. This is what I'm thinknig...

    From what I can understand here the toUpperCase method takes a string of text, and converts it into an array. It then loops through the entire array, turning each letter into an uppercase using this. text[i] = text[i].toUpperCase();


    In the second method, called main, a string from the command-line arguments is taken and put into an array. an object called passer is created that references the upperCase method. and then from here I'm not exactly quite sure what is going on.

    I see that the for loop in the main method is looping through the length of the argument string, turning each into an uppercase, but I'm not exactly sure why it works.

    The void in the first method is confusing me. doesn't void mean that nothing gets returned? If nothing is getting returned, how is the main method able to get the capitalized letters from the toUpperCase method?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Learning - could someone explain this code to me?

    A few notes, for clarity:

    You can see that an array of String objects is being used, not a single String. The main method is called first, and given an array of Strings. Then the Strings are modified a String at a time, not a character at a time.

    As far as the method return value of void, java passes the array of strings to the method by reference, a pointer, or the address of where the array is. The method modifies the array, not a copy sent over.
    Search keywords: "java pass by value" "java pass by reference"
    Be sure to note the way primitives are treated vs the way objects are treated.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning - could someone explain this code to me?

    Hi, I think I understand what youre saying. The toUppercase method is void because it is not returning anything back to main. The way it is converting the strings to uppercase is via a direct reference. It is literally going into the strings and converting it to uppercase. Is this the correct way to think about it?

Similar Threads

  1. [SOLVED] Learning OOP; Gettin error messages i cant explain.
    By E.K.Virtanen in forum Object Oriented Programming
    Replies: 2
    Last Post: March 20th, 2013, 11:47 AM
  2. Can someone please explain this code for me?
    By Grot in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 17th, 2013, 11:56 AM
  3. Could you explain this code to me please?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: December 7th, 2011, 02:10 PM
  4. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  5. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM