Forums Register Login

Strings and Char Arrays

+Pie Number of slices to send: Send
Hi All,

String a = "hello";
char[] d = { 'h', 'e', 'l', 'l', 'o' };
System.out.println(a.equals(d));

output : false

Please can any body explain why out put is "false" ?
+Pie Number of slices to send: Send
Because they're not the same. They're not even the same kind of object.
+Pie Number of slices to send: Send
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
and you have passed an array of char so it will return false
+Pie Number of slices to send: Send
Thanks David & Naman, My doubt is resolved.
+Pie Number of slices to send: Send
There is even a Java™ Language Specification section about that question.

And welcome to the Ranch
+Pie Number of slices to send: Send
Thanks Campbell
+Pie Number of slices to send: Send
You're welcome
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 830 times.
Similar Threads
New challenge equals == hashcode(m confused)
Clarify... Wrapper Classes...
Khalid's Question
Using a char array in string concatenation
'if' with float &double
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:35:27.