answersLogoWhite

0

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.

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Engineering

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 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.


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.


Float fNumber1 12.3f String sOutput fNumber1.ToString(n3) What is the value of sOutput?

sOutput has the value "12.3" (the string representation of 12.3f).


How do you check the type of a variable in c?

The example below shows how to do it in C#. using System; namespace Type_check_for_vars { class Program { static void Main(string[] args) { object myVar = null; myVar = (int) 10; if (myVar is int) { Console.WriteLine("myVar is type of {0} and has value - {1}", myVar.GetType().ToString(), myVar); } myVar = (char)'a'; if (myVar is char) { Console.WriteLine("myVar is type of {0} and has value - {1}", myVar.GetType().ToString(), myVar); } Console.ReadLine(); } } }

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.


How do you convert numbers into words using java?

Using toString() 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 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 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.


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.


Float fNumber1 12.3f String sOutput fNumber1.ToString(n3) What is the value of sOutput?

sOutput has the value "12.3" (the string representation of 12.3f).


How can you convert from int to string in NET?

lets say you have the following: Dim Int1 as Integer=12 Dim String1 as String 'Very Simple String1=Int1.tostring


How do you check the type of a variable in c?

The example below shows how to do it in C#. using System; namespace Type_check_for_vars { class Program { static void Main(string[] args) { object myVar = null; myVar = (int) 10; if (myVar is int) { Console.WriteLine("myVar is type of {0} and has value - {1}", myVar.GetType().ToString(), myVar); } myVar = (char)'a'; if (myVar is char) { Console.WriteLine("myVar is type of {0} and has value - {1}", myVar.GetType().ToString(), myVar); } Console.ReadLine(); } } }


What is a light year expressed in cm?

1 meter = 100 cm1 kilometer = 1,000 meters = 105 cm1 light year = 9.46089 x 1012 kilometers = 9.46089 x 1017 centimetersSee ... once you learn to use scientific notation, you don't have tostring rows of zeros all over the page.


Concatenation of string with number in java?

The concatenation can be done using the "+" operator. The output of this concatenation would be a String. Ex: public String concatenate(int val, String phrase) { return val + phrase; } If you invoke this method with 10 and years as the arguments the ouput would be 10 years.


Does Every class has a toString method and an equals method inherited from the Object class?

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.