| Author |
Not case sensitive boolean
|
podonga poron
Ranch Hand
Joined: May 12, 2008
Posts: 55
|
|
public class Practice { public static void main(String... args){ Boolean b = new Boolean("tRUe"); System.out.printf("%b", b); } } that prints true ! Java wasn't case sensitive, what do i miss ?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
|
What does the Javadoc have to say about it?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Java is case sensitive -- by this, it is refering to the tokens. The method names, the class names, the variable names, the keywords, etc. Strings are also case sensitive, but so what... strings should always be case sensitive, even if the language is not. It is kinda silly to not be able to write a program that distinguish case of strings. In this case, it is a string, so... so what? ... but to answer your question, how the Boolean class behaves is defined in the JavaDoc. Here is the relevent excerpt.
public Boolean(String s) Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false. Examples: new Boolean("True") produces a Boolean object that represents true. new Boolean("yes") produces a Boolean object that represents false.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
podonga poron
Ranch Hand
Joined: May 12, 2008
Posts: 55
|
|
In this case, it is a string, so... so what? So it will be case sensitive but is NOT
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
|
Did you read the Javadoc that I referenced and Henry copied into his reply?
|
 |
Wirianto Djunaidi
Ranch Hand
Joined: Mar 20, 2001
Posts: 195
|
|
|
Podon, read the excerpt that Henry posted carefully.
|
 |
podonga poron
Ranch Hand
Joined: May 12, 2008
Posts: 55
|
|
class foo { public static void main(String... args){ String s1 = "true"; String s2 = "TrUE"; System.out.println((s1==s2)?"YES":"NO"); System.out.println((s1.equals(s2))?"YES":"NO"); } } Prints, NO and NO i don't understand the Javadoc the only thing it does is confuse me more, i will ask this question to my instructor, dont worry thanks anyway
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
i don't understand the Javadoc the only thing it does is confuse me more, i will ask this question to my instructor, dont worry thanks anyway
That is because you changed the question. Strings are case sensitive, which is why you second example says NO. The Boolean class parses strings to booleans in a case insensitive way, which is why your first example prints true. You can't assume something is always case sensitive or case insensitive. You have to also know how they are implemented -- which is stated in the JavaDoc. Henry
|
 |
podonga poron
Ranch Hand
Joined: May 12, 2008
Posts: 55
|
|
now i understand thanks !
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
And this is how it's implemented: Note that they could have shortened that line to return "true".equalsIgnoreCase(name); added an "e" to turn "not" into "note" for clarity [ June 26, 2008: Message edited by: Frank Carver ]
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
podonga poron
Ranch Hand
Joined: May 12, 2008
Posts: 55
|
|
Is because the equalsIgnoreCase method ! hey thanks now is VERY clear
|
 |
 |
|
|
subject: Not case sensitive boolean
|
|
|