4 ms·
In Java int and char are primitive types. They don't provide methods so: int i = 4; i.toString(); //Exception - int isn't a class, so i isn't an object, so
by ElliotH 16y ago
In Java int and char are primitive types. They don't provide methods so:
int i = 4;
i.toString(); //Exception - int isn't a class, so i isn't an object, so you can't call its methods.
Integer j = 4;
j.toString(); //Returns "4" as a string, because Integer is a class.
Java would 'encourage' the latter practice at times because its useful to be able to call methods off of characters and integers and such, but there is a difference.
- msg 16y agoBut you wouldn't do this in Java (need a random string for an integer). Instead you might see: logger.fatal( String.format("Program crashed on index %d: %s", i, message) ); I think C has a similar construct, not quite as verbose, that would save 7 characters out of the line...
- ElliotH 16y agoThere are other methods on Integer and such - it was a contrived example because I was too lazy to read the docs.