Results 1 to 4 of 4
  1. #1
    mohitchouhan is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Angry String object Help

    class Something{
    void change(String ln,String fn){
    ln="Khan";
    f="Aamir";
    }

    public static void main(String[] arg){
    String firstname="Akshay";
    String lastname="kumar";
    Something s=new Something();
    s.change(lastname,firstname);
    System.out.println(firstname+" "+lastname);
    }
    }


    Output:
    Akshay kumar

    Que:why dint i get "Aamir Khan".....
    Kind of stuck on that....

    Thanks in advance

  2. #2
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    class Something{
    void change(String ln,String fn){
    ln="Khan";
    f="Aamir";
    }.

    This method is doesn't do anything. You are just changing the parameters 'in' and 'fn'.
    Your println() method prints the firstname and lastname not the parameters.
    It should be:
    class Something{
    void change(){
    Firstname="Khan";
    Lastname="Aamir";
    }
    You don't use the parameters.
    Sorry, I only speak machine language. Yes or a No?:confused:

  3. #3
    Bellerofonte is offline Member
    Join Date
    Feb 2011
    Location
    Rome, IT
    Posts
    4
    Rep Power
    0

    Default

    Strings are immutable. You cannot modify parameter through references. You have to create, and then return, new Objects type string.

  4. #4
    mohitchouhan is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. String to an object?
    By bobbyboyy in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 08:43 PM
  2. Object to String.
    By Gelembjuk in forum New To Java
    Replies: 6
    Last Post: 10-29-2008, 11:19 PM
  3. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 04:29 AM
  4. Object from String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:20 PM
  5. String vs Object
    By Gilgamesh in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 11:26 PM

Posting Permissions

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