0

I'm getting Person@4b67cf4d as output.

fun main(args: Array<String>){
    var person = Person("Jimmy")
    println(person)
}


class Person(val name: String){
    var age = 10
}

Output should be Jimmy. Sorry if I'm not clear enough, I just started learning Kotlin and couldnt find solution for this.

  • `println(person.name)` should print "Jimmy". Your call should print the haschcode of the object - and it does correctly. – DVarga May 21 '19 at 08:59
  • Related: [How to print my object without getting "SomeType@2f92e0f4"](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4/29140403)... it also shows why it is this way (`toString`). The same applies to Kotlin... a special case is the `data class`, but you can read that up yourself ;-) – Roland May 21 '19 at 09:27

4 Answers4

6

You should be printing a name variable of an object Person.

So its just be println(person.name)

By using println(person) you are just printing the object instance hash

Btw. you could just inline this class as class Person(val name: String, var age: Int = 10)

P.Juni
  • 2,365
  • 14
  • 26
  • 1
    "Just inline this class" That would make different behavior, enabling you to set `age` when constructing the instance, which isn't necessarily desirable. – Alexey Romanov May 21 '19 at 09:11
6

You must override the method toString() inside the Person class:

class Person(val name: String){
    var age = 10

    override fun toString(): String {
        return name
    }
}

Now your code will print:

Jimmy

and not the hashcode.

forpas
  • 160,666
  • 10
  • 38
  • 76
3

Short answer: override toString().

Java and Kotlin have a standard way to convert any object into a String: the toString() method.

This is defined in the top-level classes java.lang.Object and in kotlin.Any, so every object is guaranteed to have this method.  The implementations there simply return the class name followed by '@' and a hex representation of the object's hashcode.  (They have to work for every possible type of object, so they don't have any other info to use.)

That's what you're seeing in your output.

If you want your class to show something more meaningful, then you should override the toString() method in your Person class.  For example, to show the name, as requested:

override fun toString() = name

However, in practice that's not always the best approach.  toString() will get called whenever your objects get printed to logs, in error messages, and similar, so it's more helpful to have a less-ambiguous representation, such as:

override fun toString() = "Person($name, $age)"

(Alternatively, you could make it a data class, which will automatically provide a suitable toString() implementation, along with several other things.)

When you want to print just then name, you can do that explicitly:

println(person.name)

Or you could provide a separate method to call, e.g.:

fun toPrettyString() = name

and then:

println(person.toPrettyString())

That would make your intent much clearer.

gidds
  • 16,558
  • 2
  • 19
  • 26
2

Just use data class

fun main(args: Array<String>){
    var person = Person("Jimmy")
    println(person)
}


data class Person(val name: String, var age = 10)

Output be

Person(name=Jimmy,age=10)

If you want to output exactly "Jimmy", so, output name field :)

fun main(args: Array<String>){
    var person = Person("Jimmy")
    println(person.name)
}
Scrobot
  • 1,911
  • 3
  • 19
  • 36