| Numeric Literals |
|---|
| Numeric integer literals WITHOUT a type suffix are integers, and have a max size of 2147483647. If you wish to create a literal larger than that, you must suffix it with L for long, F for float, or D for double. |
| Numeric literals WITH a type suffix are treated as the type specified by the suffix - L for long, F for float, and D for double. |
| For primitive assignments, if the value fits within the size of the desired primitive, an integer literal can be assigned directly to the variable. For example, if the value doesn't exceed 127, the integer literal can be assigned to a byte variable. |
|
A float can take any integer, long, or float literal assignment, but not double.
A double can take any integer, long, float, or double literal assignment. |
| Wrapper objects Byte, Character, Short, and Integer can be assigned integer literals, as above. |
| Long, Float, and Double can only be assigned literal values with the correct suffix - L for long, F for float, and D for double. This is regardless if whether or not the literal would fit into an integer - even assigning the value 1 to a Long, requires Long var = 1L. In addition, they're not swappable - you can't assign a 1F float literal to a Long, for instance - Longs can take only literals with an L suffix, Floats can take only literals with an F suffix, and Doubles can take only literals with a D suffix. |
|
Think of it this way: Up to size integer, literals are everyone's best friend, and can be used by both primitives and wrapper classes alike.
If an integer can possibly whittle down to fit your size, it will;
only if the value is too large to fit in your variable type, will you get a compile error.
For the larger primitives long, float, and double, they're still friendly, and work the way you think they should according to type coercion rules. However, the wrapper classes Long, Float, and Double are draconian - they accept ONLY the suffixes they require, and no others. All must bow to their rules. Take a look at the following, for clarification:
package test;
public class Main {
public static void main(String[] args) {
// These primitive assignments all work
byte a = 127;
char c = 65535;
short s = 32767;
int i = 2147483647;
// No suffix required if literal fits in an int.
long l = 2147483647;
float f = 2147483647;
double d = 2147483647;
// Suffix required if literal doesn't fit in an int.
l = 2147483648L; // Long.MAX_VALUE + 1
f = 2147483648L; // float can take long suffix
f = 2147483648F; // float can take float suffix
d = 2147483648L; // double can take long suffix
d = 2147483648F; // double can take float suffix
d = 2147483648D; // double can take double suffix
// These wrapper object assignments all work
Byte aa = 1;
Character cc = 128;
Short ss = 32767;
Integer ii = 2147483647;
// The following work, but require the specified suffix,
// even if the number fits in an int. Also, they're not
// swappable, like up above - Doubles can't take float
// suffixes, Floats can't take long suffixes, etc.
Long ll = 1L; // L suffix required
Float ff = 1F; // F suffix required
Double dd = 1D; // D suffix required
/*
// These don't work - put a type suffix on the end to make them work.
Long ll = 1; // required: long found: int error
Float ff = 1; // required: long found: int error
Double dd = 1; // required: long found: int error
// These don't work - they overflow the max size for the type.
a = 128; // required: byte found: int error
c = 65536; // required: char found: int error
s = 32768; // required: short found: int error
i = 2147483648; // integer number too large error
long l = 9223372036854775807; // integer number too large error
*/
}
}
|
| instanceof |
|---|
| instanceof is an operator, not a function - you don't use parentheses with it. |
|
instanceof can be used to check whether an object inherits from a certain class or implements a certain interface.
For example: cat instanceof Animal would return true in the example below. In addition, cat instanceof Object would return true, since all classes have Object at the base of their inheritance tree. The reverse of a true instanceof, is never true! Since an Animal is not a type of Cat, the animal instanceof Cat check fails. |
|
null is never an instanceof anything! Even null instanceof Object returns false.
So - if you have a variable cat, and assign it the value null, cat instanceof Animal will return false, even though the variable can hold a reference to Animal. |
| Attempting to check an instanceof relationship between unrelated class hierarchies is a compile time error, not a run time error. |
Examine the example below for clarification:
package test;
class Animal {}
class Cat extends Animal {}
class Vegetable {}
public class testInstanceOf {
public static void main(String[] args) {
Animal animal = new Animal();
Cat cat = new Cat();
System.out.println(cat instanceof Animal); // true
System.out.println(cat instanceof Cat); // true
System.out.println(animal instanceof Cat); // false
System.out.println(animal instanceof Vegetable); // compile error
cat = null;
System.out.println(cat instanceof Cat); // false
System.out.println(cat instanceof Object); // false
}
}
|
| Boolean and Bitwise Bits |
|---|
| All 'if' statements require a boolean test. You can't pass it an expression that evaluates to something other than boolean. |
| Short circuit boolean operators require a boolean test. This includes && and || You can't pass in expressions that evaluates to something other than boolean. |
|
Bitwise operators can take either boolean, int, or long values.
This includes &, |, and ^.
If given an integer or long, it will perform a bitwise operation. If given a boolean expression, it will perform a non-short-circuit logical operation. |
Following is an example that illustrates these points:
package test;
public class BooleanBits {
public static void main(String[] args) {
boolean a = true;
boolean b = true;
if (a & b); // normal and operator
if (a && b); // short circuit and operator
if (a | b); // normal or operator
if (a || b); // short circuit or operator
if (a ^ b); // xor - one or the other must be true, not both
/*
* All if statements require a boolean test.
* You can't pass it an expression that evaluates to
* something other than boolean.
*/
if (1); // compiler error - not a boolean expression
if (1 = 1); // compiler error - not a boolean expression
if (1 == 1); // true
if (b); // true
if (b = false); // false because result of assignment is false
if (b == false); // true because b was set to false up above
/*
* Short circuit boolean operators require a boolean test.
* This includes && and ||
* You can't pass in expressions that evaluates to
* something other than boolean.
*/
if (1 && 1); // compiler error - operator cannot be applied to int
if (1 || 1); // compiler error - operator cannot be applied to int
/*
* Bitwise operators can take either boolean, int, or long values.
* This includes &, |, and ^
* If given an integer or long, it will perform a bitwise operation.
* If given a boolean expression, it will perform a
* non-short-circuit logical operation.
*/
// All these compile.
int x = 63 & 252; // bitwise and
long l = x | 2; // bitwise or
int y = x ^ 252; // bitwise xor
boolean retval = false;
retval = a & b; // non-short-circuit and
retval = a | b; // non-short-circuit or
retval = a ^ b; // xor
}
}
|