Skip to content

Commit 796dd6c

Browse files
committed
Add example demonstrating default Object toString() behavior in Java
Created a `student` class with fields `studentID` and `name`, along with a constructor to initialize them. In the `toStringMethod` class, instantiated two `student` objects and printed them using `toString()`. Logic: Since the `toString()` method is not overridden, the output shows the default implementation from `Object` class — which prints the class name followed by the object's hash code in hexadecimal. Comments in the code explain how `Object.toString()` works internally and what it returns if not overridden. Prepared the code to allow comparison by optionally uncommenting the custom `toString()` method. Signed-off-by: Somesh diwan <[email protected]>
1 parent e186f41 commit 796dd6c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Section6StringClassPrinting/Engineering Digest ED/src/toStringMethod.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
class student
2-
{
1+
class student {
32
int studentID;
43
String name;
54

6-
student(int studentID, String name)
7-
{
5+
student(int studentID, String name) {
86
this.studentID =studentID;
97
this.name =name;
108
}
9+
1110
/*
1211
public String toString()
1312
{
@@ -29,10 +28,9 @@ public String toString()
2928
would be returned if neither the toString nor hashCode methods were overridden by the object's class.
3029
*/
3130
}
32-
public class toStringMethod
33-
{
34-
public static void main(String[] args)
35-
{
31+
32+
public class toStringMethod {
33+
public static void main(String[] args) {
3634
student s = new student(1,"Bruce");
3735
student s2 = new student(2, "Jackie");
3836

0 commit comments

Comments
 (0)