Java toString() methodIf you want to represent any object as a string, toString() method comes into existence. The toString() method returns the string representation of the object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation. Advantage of Java toString() methodBy overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code. Understanding problem without toString() methodLet's see the simple code that prints reference. Output:Student@1fee6fc Student@1eed786
Example of Java toString() methodNow let's see the real example of toString() method. Output:101 Raj lucknow 102 Vijay ghaziabad Next TopicStringTokenizer in java
|