Boolean Flashcards

1
Q

General info

A

You need to remember the following points about Boolean: 1. Boolean class has two constructors - Boolean(String) and Boolean(boolean) The String constructor 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. The boolean constructor is self explanatory. 2. Boolean class has two static helper methods for creating booleans - parseBoolean and valueOf. Boolean.parseBoolean(String ) method returns a primitive boolean and not a Boolean object (Note - Same is with the case with other parseXXX methods such as Integer.parseInt - they return primitives and not objects). The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”. Boolean.valueOf(String ) and its overloaded Boolean.valueOf(boolean ) version, on the other hand, work similarly but return a reference to either Boolean.TRUE or Boolean.FALSE wrapper objects. Observe that they dont create a new Boolean object but just return the static constants TRUE or FALSE defined in Boolean class. 3. When you use the equality operator ( == ) with booleans, if exactly one of the operands is a Boolean wrapper, it is first unboxed into a boolean primitive and then the two are compared (JLS 15.21.2). If both are Boolean wrappers, then their references are compared just like in the case of other objects. Thus, new Boolean(“true”) == new Boolean(“true”) is false, but new Boolean(“true”) == Boolean.parseBoolean(“true”) is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the two Boolean constructors

A
  1. Boolean class has two constructors - Boolean(String) and Boolean(boolean) The String constructor 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What is the result?
new Boolean("trUe");
new Boolean(0);
new Boolean("1");
new Boolean("yes");
A
new Boolean("trUe");  //true
new Boolean(0); //does not compile
new Boolean("1"); //false
new Boolean("yes"); //false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the result?
Boolean.parseBoolean(true);
Boolean.parseBoolean(“car”);
Boolean.parseBoolean(Integer(1));

A

Boolean.parseBoolean(true); //does not compile. Requires String param.
Boolean.parseBoolean(“car”); // false
Boolean.parseBoolean(Integer(1)); // does not compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does this method take for parameter and what does it return?
Boolean.parseBoolean(?);

A

It only takes STRING and returns a primitive.
A string of “true” - ignoring case returns true.
ANY other string returns false.
Boolean.parseBoolean(“car”); will compile. It just returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What will this print?
System.out.println(newBoolean());
A

It will not compile. Boolean doesn’t have a no args constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What will this print?
System.out.println(newBoolean(" true "));
A

it will print false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly