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())
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
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())
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
Here is my code:
String[] magic = {"stick", "hat", "witch"};
String magic1 = magic.toString();
String magic2 = Arrays.toString(magic);
System.out.println(magic1); // this is printing a memory location
System.out.println(magic2); // this one prints: [stick, hat, witch]
What is the difference between magic1 and magic2?
Arrays are objects, but they don't change (override) its toString() method, which means they use default one, inherited from Object. If you read documentation of this method you will find:
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())
In your case
getClass().getName() returns [Ljava.lang.String which means
one dimensional array (because there is only one [)
of type which full name is java.lang.String
and Integer.toHexString(hashCode()) returns something like 1db9742 which is hexadecimal form of integer returned by hashCode() method.
Now if you take a look at code of Arrays.toString(Object[] array) (String[] is considered as Object[])
4531 public static String toString(Object[] a) {4532 if (a == null)4533 return "null";4535 int iMax = a.length - 1;4536 if (iMax == -1)4537 return "[]";4539 StringBuilder b = new StringBuilder();4540 b.append('[');4541 for (int i = 0; ; i++) {4542 b.append(String.valueOf(a[i]));4543 if (i == iMax)4544 return b.append(']').toString();4545 b.append(", ");4546 }4547 }
you will see that its purpose is to create string build from content of this array. It does this by iterating over all elements and adding their string representation to StringBuilder which is then used to create String which will be returned.
It is basically a tailored toString which makes the output pretty.
What you see in the first toString is the memory address . Reason being that the variable-name is just that - a memory address( aka reference ).
Every class inherits toString, and can implement its own.
See Arrays class API
String[] names = {"Bob", "Dad", "Mom"};
String names1 = names.toString();
String names2 = Arrays.toString(names);
System.out.println(names1 );
System.out.println(names2 );
prints out:
[Ljava.lang.String;#1034bb5
[Bob, Dad, Mom]
Read the doumentation. I have copied the necessary information here.
public static String toString(Object[] a)
Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.
The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(), unless a is null, in which case "null" is returned.
public String toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
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())
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.
What does the following System.out print in the following code??
class ExampleTest {}
public class Test {
public static void main(String ... strings){
ExampleTest et=new ExampleTest();
System.out.println(et);
System.out.println(new ExampleTest());
}
}
When you give any object to a print method, such as in your code, it will call the toString() method.
In your example, your ExampleTest class does not override this toString() method, so it will call the Object.toString():
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
So the output will be the full name of the class, and the result of the hashCode of this class.
Here is what the Javadoc of java.lang.Object.toString() says about that:
Returns a string representation of the
object. In general, the toString
method returns a string that
"textually represents" this object.
The result should be a concise but
informative representation that is
easy for a person to read. It is
recommended that all subclasses
override this method.
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())
It prints the class name followed by the # symbol, followed by the unsigned hexadecimal representation of the object's hashcode.