-
Notifications
You must be signed in to change notification settings - Fork 0
Why does my program output MyClass@1b6d3586
discorddioxin edited this page Apr 28, 2022
·
5 revisions
Given the class
class MyClass {
}And given an instance of the class
MyClass instance = new MyClass();Attempting to print the instance
System.out.println(instance)Or attempting to append the instance to a UI element as a String
String text = instance.toString();
textField.setText(text);Results in output similar to the following
MyClass@1b6d3586
(Or something similar)
Override the toString() method in MyClass
class MyClass {
@Override
public String toString() {
return "New output";
}
}You can output the data you desire
class MyClass {
private String textData;
private int numberData;
@Override
public String toString() {
return textData + " " + numberData;
}
}You may now print your object as expected
MyClass instance = new MyClass();
System.out.println(instance);Given the array
String[] array = { "one", "two" };Attempting to print the arary
System.out.println(array);Results in the following:
[Ljava.lang.String;@e9e54c2
(Or something similar)
Use Arrays.toString to convert the array to a String
String s = Arrays.toString(array);
System.out.println(s);