Java Reference
In-Depth Information
// Using constructors
Boolean b11True = new Boolean(true);
Boolean b21True = new Boolean("true");
Boolean b31True = new Boolean("tRuE");
Boolean b41False = new Boolean("false");
Boolean b51False = new Boolean("how is this"); // false
// Using the factory methods
Boolean b12True = Boolean.valueOf(true);
Boolean b22True = Boolean.valueOf("true");
Boolean b32True = Boolean.valueOf("tRuE");
Boolean b42False = Boolean.valueOf("false");
Boolean b52False = Boolean.valueOf("how is this"); // false
// Getting a boolean value from a Boolean object
boolean bbTrue = b12True.booleanValue();
// Parsing strings to boolean values
boolean bTrue = Boolean.parseBoolean("true");
boolean bFalse = Boolean.parseBoolean("This string evaluates to false");
// Using constants
Boolean bcTrue = Boolean.TRUE;
Boolean bcFalse = Boolean.FALSE;
// Printing some Boolean objects
System.out.println("bcTrue = " + bcTrue);
System.out.println("bcFalse = " + bcFalse);
bcTrue = true
bcFalse = false
Unsigned Numeric Operations
Java does not support unsigned primitive integer data types. The byte , short , int , and long are signed data types. For
a signed data type, half of the range of values is used for storing positive number and half for negative numbers, as one
bit is used to store the sign of the value. For example, a byte takes 8 bits; its range is -128 to 127. If you were to store
only positive numbers in a byte, its range would have been 0 to 255.
Java 8 has not added any new unsigned integer data types. Rather, it has added some static methods in wrapper
classes that support operations treating the bits in the signed values as if they are unsigned integers.
The Byte class contains two static methods:
int toUnsignedInt(byte x)
long toUnsignedLong(byte x)
The methods convert the specified byte argument into an int and a long as if the byte stores an unsigned value.
If the specified byte argument is zero or a positive number, the converted int and long values will be the same as the
argument value. If the argument is a negative number, the converted number will be 2 8 + x. For example, for an input
of 10, the returned value will be 10, and for an input of -10, the returned value will be 2 8 + (-10), which is 246. Negative
numbers are stored in 2's complement form. The value -10 will be stored as 11110110. The most significant bit 1
 
Search WWH ::




Custom Search