answersLogoWhite

0

Yes - in Java, every class has this method, which is inherited from the Object class. Often, the inherited method does nothing particularly useful, but you can override it with your own implementation.

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is toString and when you use it?

The toString method is used to create a string representation of the given object. This is often used to give diagnostic output to the programmer.


What method that returns string representation of the contents of the variable?

Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example: "The answer is: " + myObject In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.


Why do you need to override the toString method?

By default every class you write inherently extends the base Object class. This has a basic toString() method which merely returns the name of the class followed by a hex representation of the hash value. By overriding this method, you can return a more meaningful value specific to your class, such as member attributes and their values.


Why do you need to create an object reference variable to call methods cant you directly use the objects that is created?

Creating an object and invoking a method of that object are 2 distinct steps. From good programming practice, it is better off to have 2 separate statements at least to do this. For example: (new object()).ToString(); // creates an instance and invoke ToString() vs. object objRefence = new object(); objRefence.ToString(); The variable objReference is needed to invoke the method ToString(), because the object refereced of the one being created from the new operator needed to be cached somewhere. There is no major differences between the 2 above. However, if there is another method to be invoked, the second way (use variable reference) is the only way, the first one cannot. (You can replace ToString() with GetHashCode(), and prints it on the console twice, the first one may produce 2 different values, while the second one will be the same value. Think of GetHashCode() returns the object id in a way)


Why do all java objects have a common ancestor?

Java is an object oriented language, which means that one of its core principles is inheritance. All classes (and hence the objects created from them) are derived from a parent. In this way, classes inherit variables, methods, and classes from their parent so that they do not have to redefine them. In Java's case, the common parent of all classes is Object. This gives every class some basic functionality, such as the equals, toString, and wait methods. Besides uniformity, another advantage of inheritance is clear organization. Imagine if we could not guarantee the comparison of two objects with the equals method. Finally, a global parent class means that every class, no matter how specific, can be generalized into the parent's type (Object o = new String()).

Related Questions

What does the ToString-method accomplish?

The ToString method accomplishes a very satisfying end result. Using the ToString method, one will be able to textually represent an object by only using strings.


What is toString and when you use it?

The toString method is used to create a string representation of the given object. This is often used to give diagnostic output to the programmer.


What method that returns string representation of the contents of the variable?

Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example: "The answer is: " + myObject In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.


Why do you need to override the toString method?

By default every class you write inherently extends the base Object class. This has a basic toString() method which merely returns the name of the class followed by a hex representation of the hash value. By overriding this method, you can return a more meaningful value specific to your class, such as member attributes and their values.


Why do you need to create an object reference variable to call methods cant you directly use the objects that is created?

Creating an object and invoking a method of that object are 2 distinct steps. From good programming practice, it is better off to have 2 separate statements at least to do this. For example: (new object()).ToString(); // creates an instance and invoke ToString() vs. object objRefence = new object(); objRefence.ToString(); The variable objReference is needed to invoke the method ToString(), because the object refereced of the one being created from the new operator needed to be cached somewhere. There is no major differences between the 2 above. However, if there is another method to be invoked, the second way (use variable reference) is the only way, the first one cannot. (You can replace ToString() with GetHashCode(), and prints it on the console twice, the first one may produce 2 different values, while the second one will be the same value. Think of GetHashCode() returns the object id in a way)


How do you convert numbers into words using java?

Using toString() method


Why comparator has equals method?

The Java superclass Object says that all Java objects have an equals method. Thus Comparator has an equals method.


What is overloading in c sharp?

My answer may not be the intent of the designer of overloading. But here are my understanding and the best use of it: If you have a group of methods that are: 1. functioning the same (return the same object type), and 2. they takes different parameters or arugments, you may overload that that group of methods with the same name. Please note that the word may. (You have the option, but do not have to) One example is the overloaded method ToString() Some of other possible overloading ways are: ToString(string fmt), ToString(int start, int count); In other computer language, one may have to code as ToFormattedString(), ToSubString(), instead of using the same method name.


Why do all java objects have a common ancestor?

Java is an object oriented language, which means that one of its core principles is inheritance. All classes (and hence the objects created from them) are derived from a parent. In this way, classes inherit variables, methods, and classes from their parent so that they do not have to redefine them. In Java's case, the common parent of all classes is Object. This gives every class some basic functionality, such as the equals, toString, and wait methods. Besides uniformity, another advantage of inheritance is clear organization. Imagine if we could not guarantee the comparison of two objects with the equals method. Finally, a global parent class means that every class, no matter how specific, can be generalized into the parent's type (Object o = new String()).


What is the diff between equality of objects and equality of references that refer to them?

References are equal if they both point to the same object. o2); // true because they are meaningfully equal System.out.println(o1.equals(o2)); } } Meaningfully equal is defined by overriding the equals method of class Object. boolean equals (Object obj) Decides whether two objects are meaningfully equivalent.


Static method can be call object?

yes we can call a static method with object


What is the meaning of System.out.print in java?

System.out.print is a standard output function used in java. where System specifies the package name , out specifies the class name and print is a function in that class. This function is different from the System.out.println() since it does not place a new line after printing the variable. System.out.println("hello"); // prints hello System.out.print("hello"); //also prints hello but if you thenSystem.out.println("how are you"); //it continues from the last print resulting in// hellohow are you This also calls the default toString() method of the variable you put as the parameter.Note: not all Classes have a helpful toString() method, i.e. arrays do not have toString(), thus a nasty memory address is printed.