how to print an object on kotlin ChatGPT said: ChatGPT In Kotlin, you can print an object using println() or other logging methods. By default, calling println(object) will invoke the object’s toString() method. If you haven’t overridden toString() in your class, it will print the class name followed by the object’s hash code (e.g., MyClass@1b6d3586).
Here’s how you can print an object effectively:
- Use toString() (Default or Custom): Override the toString() method in your class to provide a meaningful representation.
kotlin Copy Edit class Person(val name: String, val age: Int) { override fun toString(): String { return “Person(name=age)” } }
fun main() { val person = Person(“Reza”, 30) println(person) // Prints: Person(name=Reza, age=30) } 2. Print Properties Directly: Access individual properties of the object.
kotlin Copy Edit fun main() { val person = Person(“Reza”, 30) println(“Name: {person.age}”) } 3. Use data class: If you’re using a data class, Kotlin automatically generates a meaningful toString() method for you.
kotlin Copy Edit data class Person(val name: String, val age: Int)
fun main() { val person = Person(“Reza”, 30) println(person) // Prints: Person(name=Reza, age=30) } 4. Convert to JSON: If you need the object in JSON format, use libraries like Kotlinx Serialization or Gson.
Example with Kotlinx Serialization: kotlin Copy Edit import kotlinx.serialization.* import kotlinx.serialization.json.*
@Serializable data class Person(val name: String, val age: Int)
fun main() { val person = Person(“Reza”, 30) val json = Json.encodeToString(person) println(json) // Prints: {“name”:“Reza”,“age”:30} } 5. Debugging with print(): If you just want to debug, you can use Kotlin’s println() or even IDE debugging tools.
kotlin Copy Edit fun main() { val person = Person(“Reza”, 30) println(person) // Easy debugging } Which approach you use depends on how you want the object to be represented in the output! Let me know if you need help with a specific case.