Convert boolean to string
In this chapter you will learn:
Convert boolean to string
String toString()
returns a String object representing this Boolean's value.static String toString(boolean b)
returns a String object representing the specified boolean.
public class Main {
/*from j a v a 2s . co m*/
public static void main(String[] args) {
Boolean boolean1 = new Boolean("true");
System.out.println(boolean1.toString());
System.out.println(Boolean.toString(true));
}
}
The output:
boolean value to yes or no
The following code converts a boolean to a String returning 'yes' or 'no'.
public class Main {
public static void main(String[] argv){
System.out.println(toStringYesNo(true));
}/*j a v a2 s. co m*/
public static String toStringYesNo(boolean bool) {
return toString(bool, "yes", "no");
}
private static String toString(boolean bool,
String trueString,
String falseString) {
return bool ? trueString : falseString;
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- How to declare Java char type
- How to create char Literals
- How to use escape sequences and create special characters
- Store unicode into a char
- How to create a Character object from char value
Home » Java Tutorial » Primitive Data Types