A literal primitive is a representation i the source code of primitive data.
They can be expressed in base 10, 8 (octal) or 16 (hex):
- decimals can be written using the range
- hexadecimals can be written with the range
0X(case insensitive). Max 16 digits excluding the prefix;
- octals can be written using the range
0in front of the numbers. Max 21 digits excluding the prefix;
They can have
l after the number to indicate the type long.
// The number 26, in decimal int decVal = 26; // The number 26, in hexadecimal int hexVal = 0x1a; // The number -8, in octal int octalVal = 037777777770;
Defined by default as double (64bit). If you want to use them as float you need to put
f after the number. Their structure is
0.0f. Declaring a float without the final f you get a compiler error.
Doubles can have at the end
d but it is optional.
true or false. YOU CANNOT USE INTEGERS, again: YOU CANNOT USE INTEGERS, you get a compilation error.
Single characters between
'. It’s possible even to use Unicode characters like
char a = 'n'; is equal to
char a = '\004E';. They are 16bit integers so it’s possible to assign a char variable to an integer (range
0-65535 but remember that if you go out of range you need to cast them like this:
char a = (char)70000; and you can do also
int a = (char)70000;. It’s possible to escape special characters like
char a = '\'';.
Be aware that:
So 70000 in binary is equal to 0b10001000101110000, when you do something like
int a = (char)70000;70000 is out of range so when you cast to char the jvm flips the left-most digit so 0b10001000101110000 becomes 0b00001000101110000 which is in range and equal to 4464.
A variable can contain a primitive type or a reference to an object. In both cases it contains bits, in the first case is a value, in the second case it’s a reference to the object in the heap.
It’s possible to assign to a primitive variable a literal or the result of an expression. For integers if do an operation (+ - * /) between integers and a subtype (like byte) the expression returns always an integer.
If you write
byte b; byte a = 1; byte b = 5; b = a + b; you’ll get a compiler error because the compiler automatically casts those two variables to int so you get a compiler error. That (easy) solution is up to you.
Casts can be implicit or explicit. A cast is implicit if you are doing a widening operation (e.g.: you put a byte into an int). Explicit cast is needed when narrowing (e.g: you put an int into a byte - duh!). Doing an explicit cast is risky because you can assign out of range values. Be aware that you don’t get a runtime exception, you just get the left-most bit flipped.
If you assign to a primitive a value that is too large without an explicit casting you get a compilation error.
With a compound operator (+=, -=, *= and /=) the cast is implicit:
b=(byte)b+7 so you don’t get a compilation error when doing
If you assign a variable to another variable you are just copying the value of that variable. The two variables are not connected at all. In case of reference variable you are copying the reference of the variable and they are also disconnected:
CoffeeSize a = CoffeeSize.HUGE; CoffeeSize b = CoffeeSize.BIG; System.out.println(a); System.out.println(b); b = a; a = CoffeeSize.SMALL; System.out.println(a); System.out.println(b); int x = 10; int y = 5; System.out.println(x); System.out.println(y); y = x; x = 15; System.out.println(x); System.out.println(y);