Monday, June 25, 2012

Assignments


Stack and Heap

Stack handles Local variables.
Heap handles Instance variables and objects.



Stack
setBrand() brandName
build() pc
main() c

Heap
Computer
MotherBoard
String

Integer Literals

Decimal, Octal and Hexadecimal are by default int, unless we type L as a suffix

long so = 0xFFFFl;  // Note the lowercase 'l'

Octal Literals => Octal integers use only the digits 0 to 7.
The preceding 0 identifies an Octal integer.

We can have up to 21 digits in an octal number, not including the leading zero.

Hexadecimal Literals => Are constructed using 16 distinct symbols not including the prefix 0x or the optional suffix extension L

0 1 2 3 4 5 6 7 8 9 a b c d e f


Floating-Point Literals

By default floating-point literals are defined as a double(64 bits), that's why the first statement does not compile, the second statement is using a suffix "f"(could be "F") which says to compiler we're talking the risk(loss of precision), Finally we cannot use a semicon instead of a dot.

Boolean Literals

!Important
Java does not allow a number like 0 or 1, only true and false are valid

Character Literals

You can assign a number literal, assuming it will fit into the unsigned 16-bit
range (65535 or less).

Not legal assignments.

char e = -29;   // Possible loss of precision; needs a cast
char f = 70000  // Possible loss of precision; needs a cast

Primitive Assignments

Image of all primitives

As you know literal integer is always int, futhermore the result of a expression involving an int results in an int value.

byte a = 3;     // No problem, 3 fits in a byte
byte b = 8;     // No problem, 8 fits in a byte
byte c = b + c; // Does not compile because the result is an int.

Primitive Casting

Casts can be implicit or explicit, where implicit cast we don't have to type anything, implicit casts happen when we try to assign a smaller thing(like short) to a bigger container(like long).

int num = 100;
long num1 = num; // Implicit cast, an int value always fits in a long

An explicit casts looks like this:
float ft = 100.001f;
int num = (int)ft; // Explicit cast, the float could lose info

Let's review


Floating Assignments


! Important
 +=, -=, *=, and /= will all put in an implicit cast.

 byte b = 127;
 b+= 7;




No comments: