Page 4 of 5 FirstFirst ... 2345 LastLast
Results 61 to 80 of 93
Like Tree4Likes

Thread: adding an object to an array in another class

  1. #61
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: adding an object to an array in another class

    Perhaps you meant to delete or edit reply #59? I'm so confused!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  2. #62
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: adding an object to an array in another class

    Ultimately the static method String valueOf(Object obj) does the all the work; it calls the Object.toString() method on the object; for ""+foo, it's the append method in the StringBuilder class that sets the machinery rolling (all started by the compiler and the overloaded + operator translation); for the System.out.println(foo) scenario it's the println( ... ) method that calls the String.valueOf( ... ) method; they don't call the toString() method directly because they want null represented by the "null" String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #63
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: adding an object to an array in another class

    Thanks Jos. You just saved me the trouble of copying and pasting several lines of JDK source :)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #64
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: adding an object to an array in another class

    The bill is on its way ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #65
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: adding an object to an array in another class

    From Effective Java, 2nd Edition, by Joshua Bloch, Item 10, Page 51,

    "The toString() method is automatically invoked when an object is passed to println, printf, the string concatenation operator, or assert, or printed by a debugger."

    You may not like the word automatically, but considering the number of folks involved with reviewing his book and who also helped author most of the JDK, at least I am in good company.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #66
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: adding an object to an array in another class

    I actually tend to explicitly invoke toString(). Reason being: I work with many other developers and we all hack in the same code bases, we peer review each other's code, etc. When you do that, you want things to be as explicit as you can possibly make it, especially in huge application bases where a peer review will likely cover hundreds of lines of code. I'll give a simple example why.

    Java Code:
    public class Person {
      private String name;
      private String socialSecurityNumber;
    
      public Person(String, String ssn){
        // ...
      } 
    
      public String getName(){
         return name;
      }
    
      public String getSocialSecurityNumber(){
        return socialSecurityNumber;
      }
    
      public String toString(){
         return socialSecurityNumber + ": " + name;
      }
    }
    Now when in the code you run into this:

    Java Code:
    "bla bla bla bla " + person + " bla bla bla"
    that is hardly self-documenting, this could easily be a programming mistake and you actually wanted to append the name only. But when you make it explicit:

    Java Code:
    "bla bla bla bla " + person.toString() + " bla bla bla"
    There is no confusion possibly - this was done on purpose.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #67
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: adding an object to an array in another class

    This is straight from the JDK source (version 1.6)

    Java Code:
        public static String valueOf(Object obj) {
    	return (obj == null) ? "null" : obj.toString();
        }
    This is the method called by print( ... ), append( ... ) and possibly other methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #68
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: adding an object to an array in another class

    To me "automatically" implies it's part of the language.
    Which it isn't.

    Bloch's sentence should read:
    "The toString() method is invoked when an object is passed to ..."

    The only "automatic" thing that happens is with String concatenation, and that's the compiler doing a translation, which generally doesn't involve toString directly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #69
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: adding an object to an array in another class

    I don't dispute that whatsover (how could I because it is fact). This whole discussion started because I used the term "automatic" in relation to how toStirng was invoked without the user having to explicitly call it. And as cited above, I am not alone in my use of that term. This is all about semantics, not about implementation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #70
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: adding an object to an array in another class

    Well send him an email; perhaps if he ever feels the need to write a third edition he'll correct his years old tiny slip of the tongue ;) Meanwhile... I still believe that you are defending yourself against ghosts. All I see is a discussion about how toString() works, not about people trying to correct what you said.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #71
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: adding an object to an array in another class

    Quote Originally Posted by JosAH View Post
    The bill is on its way ;-)
    And the rest of the duck too, I hope ...
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #72
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Quote Originally Posted by gimbal2 View Post
    Well send him an email; perhaps if he ever feels the need to write a third edition he'll correct his years old tiny slip of the tongue ;) Meanwhile... I still believe that you are defending yourself against ghosts. All I see is a discussion about how toString() works, not about people trying to correct what you said.
    Perhaps you are correct. I have made some real boners on this forum and when I do have a chance to defend myself against what I have said I feel compelled to do it. I looked for an errata on his book to see if this statement was addressed but I couldn't find it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #73
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: adding an object to an array in another class

    Quote Originally Posted by jim829 View Post
    when I do have a chance to defend myself against what I have said I feel compelled to do it.
    Almost like its... automatic!

    Lighten up dude, I've not been around for very long but my overall assessment of your replies, me being the master of the universe, is that you know your stuff and I need to take care what I say around you to not get a boner myself.

    (*ba-dum pssh*)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #74
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: adding an object to an array in another class

    I 'automatic' was meant to mean 'toString() calls are generated (by the compiler?) behind your back', it isn't true; those toString() calls are already there in the source code of the core classes. aamof, it is only in the String.valueOf( ... ) method that also needs to generate the "null" String representation for null. The compiler does generate code for the + operator when at least one of its operands happens to be a String, but that's all. btw the original wordings were "all these thingies are automagically transmogrified to Strings" but that caused a hell of an internal discussion ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #75
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Please understand that when I said defend myself, I didn't mean from a personal perspective (anymore that anyone else who replies are defending the statements they made). In fact, it is my observation that those that participated in this discussion are simply stating their views on the topic with no malice what so ever. It is in part because of these types of discussions and hearing everyone's opinions that I enjoy participating in this forum.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #76
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: adding an object to an array in another class

    You want an argument? That's down the hall, first door on your left; this is abuse (you silly twit!)

    kindest regards,

    Jos ;-)
    jim829 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #77
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: adding an object to an array in another class

    I see jim likes abuse. Good to know.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #78
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: adding an object to an array in another class

    Wow.....you guys had fun while I slept!! lol!!

    OK, another newbie question, and simply because I do not like the way the tester results look...how do I tell a double (the easiest way) to only show 2 places after the decimal...such as you would see with money?

    Java Code:
    	public double getTotalRentalSales()
    			{
    				double sum = 0;
    				for(int i = 0; i < rentCnt; i++)
    				{					
    					sum  += theCars[i].getCarRate();
    				}
    				return sum;
    			}

  19. #79
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: adding an object to an array in another class

    Check out String.format().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #80
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default Re: adding an object to an array in another class

    How would that work when I am returning a double not a string?

Page 4 of 5 FirstFirst ... 2345 LastLast

Similar Threads

  1. null pointer when adding object into an array
    By NoobieCode in forum New To Java
    Replies: 6
    Last Post: 07-24-2013, 11:58 PM
  2. Replies: 15
    Last Post: 08-19-2011, 03:08 AM
  3. help adding to array from another class
    By Grid_iso in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 10:00 AM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 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
  •