aspose file tools*
The moose likes Java in General and the fly likes Strings and Char Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strings and Char Arrays" Watch "Strings and Char Arrays" New topic
Author

Strings and Char Arrays

Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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" ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because they're not the same. They're not even the same kind of object.
Naman Patidar
Greenhorn

Joined: Oct 03, 2008
Posts: 15
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
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

Thanks David & Naman, My doubt is resolved.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
There is even a Java™ Language Specification section about that question.

And welcome to the Ranch
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

Thanks Campbell
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings and Char Arrays