JAVA SP T


Lets dive into snapshot of my programs!💻

14. How to convert Object to String.

💡Code:

 public class ObjectToStringConversion7078 {
              public static void main(String[] args) {
                  // Example object
                  SampleObject sampleObject = new SampleObject("Hello, I'm an object!");
          
                  // Convert object to String using toString() method
                  String stringValue = sampleObject.toString();
          
                  // Print the converted String
                  System.out.println("Converted String: " + stringValue);
              }
          }
          
          class SampleObject {
              private String message;
          
              public SampleObject(String message) {
                  this.message = message;
              }
          
              // Override the toString() method to provide a custom string representation
              @Override
              public String toString() {
                  return "SampleObject{" +
                         "message='" + message + '\'' +
                         '}';
              }
          }
           

📸Output :

output