OCA Java SE8: Boolean True Byte 123 Short 123 Int 123 Long 123 Float 123.45f Double 123.456 Char 'A'
OCA Java SE8: Boolean True Byte 123 Short 123 Int 123 Long 123 Float 123.45f Double 123.456 Char 'A'
Chapter 1
If in a java file you have two classes defined, only one is allowed to be
PUBLIC.
In imports, you cannot import methods,ONLY class names.
Instance initializers= Braces that are outside a method.
Primitive types and their values:
Keyword Type Example
boolean true or false true
byte 8-bit integral value 123
short 6-bit integral value 123
int 32-bit integral value 123
long 64-bit integral value 123
float 32-bit floating-point value 123.45f
double 64-bit floating-point value 123.456
char 16-bit Unicode value 'a'
You can declare variables ONLY OF THE SAME TYPE in a single line.
All of the arithmetic operators may be applied to any Java primitives, except boolean
and String.
Only the addition operators + and += may be applied to String
values, which results in String concatenation.
Smaller data types, namely byte, short, and char, are FIRST promoted to int any time
they’re used with a Java binary arithmetic operator, even if neither of the operands is
int.
Example:
!!!!!!What is the data type of x + y?
double x = 39.21;
float y = 2.1;
This is actually a trick question, as this code will not compile! As you may remember
from Chapter 1, fl oating-point literals are assumed to be double, unless postfi xed with
an f, as in 2.1f. If the value was set properly to 2.1f, then the promotion would be
similar to the last example, with both operands being promoted to a double, and the
result would be a double value.
Data types supported by switch statements include the following:
■ int and Integer
■ String
■ enum values
Chapter 3
Strings
The equals() method checks whether two String objects contain exactly the same characters
in the same order. The equalsIgnoreCase() method checks whether two String
objects contain the same characters with the exception that it will convert the characters’
case if needed.
We can call equals() because an array is an object. It returns true because of reference
equality. The equals() method on arrays does not look at the elements of the array.
Remember, this would work even on an int[] too. int is a primitive; int[] is an object.
Chapter 4
If we have varargs in a parameter list, it must be put the LAST one and a method can only contain
one parameter of type varargs.