Thread: Off topic I know (Java Question)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    31

    Off topic I know (Java Question)

    I came here because this place seems to have the most knowledgeable programmers on the internet

    My question is: In java if everything is essentially an object (aside from primitive data types), including Strings, why do you have to return a String back to main if you want to make some changes to it. example

    Code:
    //your main class
    main
    {
    String s = "C programming";
    StringChangerObject o = new StringChangerObject();
    o.changeString(s);
    print(s);
    }
    
    //another class in a different file
    StringChangerObject{
    public void changeString(String blah){ blah = "Java programming"; }
    }
    I know this is a terrible example and I left out details in the code, im just trying to paint a picture here and get the underlying reason as to why that wont work if a String is an object.

    Perhaps maybe that method goes on the stack and since the parameter is on the stack as well, when the method ends, the stack pointer erases all that has to do with that method????

    IDK im confuseddddd

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, you should bother to post an example that has all the details and isn't terrible.

    Second, you should research this yourself first, and then ask us for help if you're still stuck. If you did research this, you certainly didn't indicate it. Try searching "java string tutorial". The first result is this: Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings). Read the "Note" section.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by new2C- View Post
    why do you have to return a String back to main if you want to make some changes to it.
    I don't know Java, but my guess would be because your assignment of "blah" is creating a new object. Java is pass-by-value, so your callee never gets to see the assignment of the argument.

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by new2C- View Post
    confuseddddd
    A very long pause on the 'd'...
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java question
    By linuxdude in forum Tech Board
    Replies: 5
    Last Post: 07-19-2009, 05:25 PM
  2. A Java Question
    By stevesmithx in forum Tech Board
    Replies: 3
    Last Post: 04-02-2008, 12:51 PM
  3. Possible off topic question about arrays
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 02-19-2006, 10:34 PM
  4. A question for .. java!
    By McAuriel in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-01-2002, 11:16 AM
  5. Java Question
    By sweets in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 08-18-2002, 09:59 AM