Java Building Blocks

Reading Time: 1 minute

Hi,

My first blog post is about OCA Java 8 Programmer I certification. I am going to post my notes chapter by chapter. Here is quick introduction..

Packages

java.lang package is imported by default.

Java primitive types

There are 8 primitive types in Java: boolean, byte, short, int , long, float, double, and char.

8 bit: byte

16 bit: short, char

32 bit: int, float

64 bit : long, double

Tips for exam:

char c = “char”; // doesn’t compile. ” is not allowed.
byte accepts only the values from -128 to +127.

Primitive types using base

octal (base is 8, value range is from 0 to 7) : 015 is 5 + 8 = 13

hexadecimal (base is 17 , value range is from 0 to 9 and A to F – A: 10, B:11, … ,F:15): 0xAB is 16*10 + 1*11 = 171

binary (base is 2, values are 0 and 1): 0b10 is 2+ 0 = 2

System.out.println(010); // octal
System.out.println(0b10); // binary
System.out.println(0x10); // hexadecimal

prints
8
2
16

Legal Identifiers

They should start with $, _ , or letter.

$3gh // legal
__d4gh6 // legal
score //legal
4score // not legal
*score // not legal

Java reserved keyword should not be used.

new // not legal
abstract // not legal

Order of Initialization

There are some rules regarding the “order” of initialization of fields. We will go into details later but keep in mind the following order for now.

The first 2 rules applies if an object is NOT instantiated.

1. A super class , if exists, is initialized.
2. Static variable declarations & initializers.

If the object is instantiated, the rules are as follows.
3. Instance variable declarations & initializers
4. Constructors