0
toString() in Java
public class light{ public static void main(String[]args){ System.out.println(alphabet()) } public static String[] alphabet (){ return new String[]{"a","b"}; } } So my question is how to print result of this code, because now program prints position in memory. I tried write this(but it does not works): @Override public String toString(){ return String.valueof(alphabet()); } Can somebody tell me more about toString method in Java, because i am beginner in Java and I do not understand this.
7 ответов
+ 4
toString is the method that prints the String representation of an instantiated object. It is inherited from the Object class which is a base class to all classes. If it is not overridden then the Object classes implementation is what will be shown. This is what you're seeing when you try to print the String array as a whole, as there is no overriden toString in the String array object. Your implementation of valueOf is not valid as that method doesn't have an overloaded version that takes a String[] (String array) as a parameter.
The toString method will work to print out the alphabet for a Light class Object with a few minor changes to your code, if that is the preferred way you wish to do so. Both ways shown in the main method will work with the alteration that is made to the alphabet method.
public class Light{
public static void main(String[]args) {
Light light = new Light();
System.out.println(light);
System.out.println(new Light());
}
public static char[] alphabet() {
return new char[]{'a', 'b'};
}
@Override
public String toString(){
return String.valueOf(alphabet());
}
}
+ 2
The toString() Method is used to convert normal strings parsed by other methods, into Literal Strings: meaning they have no association with other methods. They Literately print out that String for the User to see. Simple!
For your code, wouldn't it be conventional for it to list all the letters in the alphabet one by one instead of relying on packages? Not to undermine the ability of said package and code, but it would seem easier for a simpler program compared to programs such as database managers (which use toString() a lot).
+ 2
The return type is a char array simply because the String.valueOf() method doesn't accept String as a parameter type and it does accept char array as one of its overloaded parameter types. See the documentation for String and scroll down to valueOf under its methods: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
You can also see here: https://www.tutorialspoint.com/java/java_string_valueof.htm
I'm not sure what you mean by add second array. Do you mean to add it as another parameter or add two arrays together, or what? Please, be more specific about this.
Your inner class can have its own overridden toString() method as can any class. I'm not sure what you're doing incorrectly, but here is an example:
public class Light {
private static Bulb bulb = new Bulb();
public static void main(String[]args) {
Light light = new Light();
System.out.println(light);
System.out.println(new Light());
System.out.println(light.getBulb());
}
public static Bulb getBulb() {
return bulb;
}
public static char[] alphabet() {
return new char[]{'a', 'b'};
}
@Override
public String toString() {
return String.valueOf(alphabet());
}
static class Bulb {
@Override
public String toString() {
return "Class Bulb";
}
}
}
Note that the class, the bulb variable, and the getBulb() method are static just so that the main method can access them, but it does not necessarily need to be this way depending on class structrure.
It is also posible to overload the toString() method in any class, just as any other method may be overloaded. Just declare it with a different number(s) or type(s) of parameter(s).
public String toString(String s) {}
0
Thank you very much. I think I understood this, but I have some questions why the data type must be char insted of String.
Other is how I can add second array for example
public class Light{
public static void main(String[]args) {
Light light = new Light();
System.out.println(light);
System.out.println(new Light());
}
public static char[] alphabet() {
return new char[]{'a', 'b'};
}
public static int[] numbers(){
return new int[]{1,2};
}
@Override
public String toString(){
return String.valueof(numbers());
}
@Override
public String toString(){
return String.valueOf(alphabet());
}
}
I tried to write an inner class but bug was the same that (method toString is already defined in class Light) so how to right two or more methods toString in one class, in my view this should be possible in inner class is there any other way to write it?
0
I want to make and print new array with type data int. When I did this earlier it prints me only location in memory so I have override it like with alphabet method but, it throws me a bug that method toString is already defined in class Light. Are you understand now? If now I will try to write all code and you only correct some mistakes.
0
class A {
private int x;
public boolean equals(Object o) {
((A)o).x == this.x;
}
public static void main(String[ ] args) {
A a = new A();
a.x = 9;
A b = new ();
b.x = 5;
System.out.println(a. (b));
}
}
0
toString is the method that prints the String representation of an instantiated object. It is inherited from the Object class which is a base class to all classes. If it is not overridden then the Object classes implementation is what will be shown. This is what you're seeing when you try to print the String array as a whole, as there is no overriden toString in the String array object. Your implementation of valueOf is not valid as that method doesn't have an overloaded version that takes a String[] (String array) as a parameter.
The toString method will work to print out the alphabet for a Light class Object with a few minor changes to your code, if that is the preferred way you wish to do so. Both ways shown in the main method will work with the alteration that is made to the alphabet method.
public class Light{
public static void main(String[]args) {
Light light = new Light();
System.out.println(light);
System.out.println(new Light());
}
public static char[] alphabet() {
return new char[]{'a', 'b'};
}
@Override
public String toString(){
return String.valueOf(alphabet());
}
}