Whenever i run the method, i get an error which comes with as numbers
The following is what i have as my code.
public String getAccount()
{
String s = "Listing the accounts";
for(List l:lists)
s+=" "+list.toString;
Return s;
}
I get the following when i run this method:
List#some numbers
For the List class, i just have a constructor which appoints parsed variables into local variables.
DOes someone know what this means?
This means that you haven't overridden the toString method in your (apparently) custom List class. The default implementation (Object.toString) displays output like you show above:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
You should override toString in your custom classes, in order to provide the desired output.
Related
I have written the following code:-
Test ob = new Test();
System.out.println(ob.toString());
System.out.println(ob.hashCode());
and the output is
Test#15db9742
366712642
i understand that the second value is the hashcode of the object and it is an integer value but i am not able to understand what is the first value. If it is the hashcode of the object then how can it be string and not integer
If you read the docs for toString very carefully:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
366712642 in hex is exactly 15DB9742!
If it is the hashcode of the object then how can it be string and not integer?
As you can see from the docs, it is the class name, plus #, plus the dashcode, not just the hash code. Also, the method's name is toString. It would be weird if it returned an int, wouldn't it?
It represents classname#HashCode_in_Hexadeciaml_form. So, the string which you are seeing is actually the hexadecimal form of the integer hashcode
You can look the source code of Object.java. toString method is meant to provide information about class at runtime, so can be overriden. What you're doing is calling the default toString method from Object.java. It simply returns following:
getClass().getName() + "#" + Integer.toHexString(hashCode()
Hence the output.
See code here
Suppose I have a custom class, say Test.
Test test = new Test(); // test is the reference.
Now when I print the value of test, it returns the hashcode .
Now consider,
Integer i = new Integer(10);
When I print the value of i, it returns 10.
Can someone help me to understand what exactly is the difference here? I believe both are object references, but for wrapper class reference, it returns the value of the object it is pointing to.
When you create a new class, it inherits the method toString() from Object. Integer class overrides that method to return the inner value.
When printing, there is an implicit call to toString() method.
By default (in for your Test class) it uses the one inside Object class. For Integer, it convert the Integer to a String in 10-base.
Your Test class is using Object class's toString() method which prints hashCode. But for Integer class, toString method is overrided. You can see Integer.java here
user defined reference is an object,if you print that object means you may get some hash code because every class extends Object class,so your also have the property (method) tostring().
Wrapper class wraps its respective primitive data type
Integer i = new Integer(10);
and
i=10;
both same in value.
When you call System.out.println(Object) (or, more generally, PrintStream.println(Object)):
This method calls at first String.valueOf(x)
String.valueOf(Object) returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
Neither of your objects are null, so the toString() method of the instances is called.
In the case of Integer:
The value is converted to signed decimal representation and returned as a string
In the case of Test, unless you've explicitly overridden it (or a superclass has overridden it), you will call Object.toString():
[T]his method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
If this isn't the desired behaviour, override toString() in Test:
class Test {
#Override public String toString() {
// ... Your implementation.
}
}
Whenever you print the object Java will invoke the toString() method. The default implementation of toString() available in Object Class. Object is the base class for the all the Object in java.
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
It will print the class Name with full package path # and HashCode of the object.
The test class doesn't override the toString() method. But all the wrapper class in java override the toString().so when You invoke the Integer method it invoke the toString() implemented in Integer class.
What is the connection between System.out.println() and toString() in Java? e.g:
public class A {
String x = "abc";
public String toString() {
return x;
}
}
public class ADemo {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj);
}
}
If main class runs, it gives an output as "abc". When I remove the code which overrides toString(), it gives an output as "A#659e0bfd". So, can anyone explain what is the working principle of System.out.println() when I pass the obj object reference as an argument to it? Is it fully connected with toString() method?
System.out is a PrintStream. Printstream defines several versions of the println() function to handle numbers, strings, and so on. When you call PrintStream.println() with an arbitrary object as a parameter, you get the version of the function that acts on an Object. This version of the function
...calls at first String.valueOf(x) to get the printed object's string value...
Looking at String.valueOf(Object), we see that it returns
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
So, long story short, System.out.println(someObject) calls that object's toString() function to convert the object to a string representation.
If your object defines its own toString() function, then that is what will be called. If you don't provide such a function, then your object will inherit toString() from one of its parent classes. In the worst case, it will inherit Object.toString(). That version of toString() is defined to return
a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object.
Or, in other words:
getClass().getName() + '#' + Integer.toHexString(hashCode())
So, when you call System.out.println() on an object that doesn't define its own version of toString(), you might get the Object version which looks like "classname#someHexNumber".
toString() is a method that exist in the Object class (Root of the inheritence tree) for all classes.
System.out.print() (SOP) will call the toString method when fed an object.
If you don't overwrite the method toString(), SOP will call the parent toString() which, if parent is the Object class, it will print the hashCode of the object
If you overwrite the method, SOP will call your toString() method
System.out.println(obj) will print the returned string from obj.toString() if you dont override it it will call the base object.toString() method which by default the toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
import java.util.HashSet;
import java.util.Set;
class Employee {
#Override
public int hashCode() {
System.out.println("Hash");
return super.hashCode();
}
}
public class Test2 {
public static void main(String[] args) {
Set<Employee>set= new HashSet<>();
Employee employee = new Employee();
set.add(employee);
System.out.println(set);// if we comment this "Hash" will be printed once
}
}
Above code calls hashCode method 2 times if we print set. Why hashcode method is called on System.out.println()?
Find the following reason for printing Hash two times
For finding the hash value when you insert the Employee into the HashSet
When you print the set, it's calls the hashCode() method inside the default toString() method from Object class.
The default toString() method from Object class API docs says
The toString() method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `#', and the unsigned hexadecimal representation of the hash
code of the object. In other words, this method returns a string equal
to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
See this. In short, the default toString() function calls hashCode() and uses a hexadecimal representation of the hash as part of the String.
The first call to hashCode() is executed when adding an Employee to your set variable, as it's needed in order to calculate which bucket to put it in.
The second call is a bit sneaker. Any Collection's default toString() is a coma-delimited concatination of all its elements toString()s enclosed by square brackets (e.g., [object1, object2]). Any object's default toString(), if you don't override it is getClass().getName() + "#" + Integer.toHexString(hashCode()). Here, since you don't override Employee's toString(), it's called again when you print set.
I want to get the content in the content model of alfresco to be displayed in my Eclipse. Below is my method from the Dictionary service:
#Override
public Collection<QName> getSubTypes(QName arg0, boolean arg1)
{
//qName = (ArrayList<QName>) model.put("Array is", arg0);
qName.add(arg0);
return qName;
}
And this is how I am calling the method on my test class:
SampleTest sampleTest = new SampleTest();
// WebScriptRequest webScriptRequest =null;
// webScriptRequest.getExtensionPath();;
// String string = webScriptRequest.getExtensionPath();
System.out.println("" + sampleTest.getSubTypes(ContentModel.TYPE_CONTENT, true).toArray().toString());
array.toString prints the address of the array, not the elements in it.
you can use java.util.Arrays.toString method to print the string
so your code should be like
System.out.println(Arrays.toString(sampleTest.getSubTypes(ContentModel.TYPE_CONTENT, true).toArray()));
Maybe the Method Arrays.toString() is what you are looking for.
.toString() on an Object only gives you the Object represantation not the actual value as a String(if it is not overriden).
Do you want to print the Collection ? You can convert it into an array and use either Arrays.toString(arr) or Arrays.deepToString(arr) :
Arrays.toString(
sampleTest.getSubTypes(ContentModel.TYPE_CONTENT, true).toArray());
What you are trying is :
.toArray().toString()
So it converts the Collection to an array and invokes the toString() on that array object , hence by default Object#toString() is called :
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())