Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not case sensitive boolean

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the Javadoc have to say about it?
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the Javadoc that I referenced and Henry copied into his reply?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Podon, read the excerpt that Henry posted carefully.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i understand thanks !
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is because the equalsIgnoreCase method !

hey thanks now is VERY clear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic