Stack and Heap
Stack handles Local variables.
Heap handles Instance variables and objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.assignments; | |
class MotherBoard { } | |
class Computer { | |
MotherBoard motherboard; | |
String brand; | |
void setBrand(String brandName) { // <===== local variable | |
brand = brandName; | |
} | |
void build(Computer pc) { // <===== local variable | |
motherboard = new MotherBoard(); | |
pc.setBrand("MacBook"); | |
} | |
} | |
public class StackHeap { | |
public static void main(String[] args) { | |
Computer c; // <===== local variable | |
c = new Computer(); | |
c.build(c); | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Octal { | |
public static void main(String [] args) { | |
int six = 06; // Equal to decimal 6 | |
int seven = 07; // Equal to decimal 7 | |
int eight = 010; // Equal to decimal 8 | |
int nine = 011; // Equal to decimal 9 | |
System.out.println("Octal 010 = " + eight); | |
} | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HexTest { | |
public static void main (String [] args) { | |
int x = 0X0001; | |
int y = 0x7fffffff; | |
int z = 0xDeadCafe; | |
System.out.println("x = " + x + " y = " + y + " z = " + z); | |
} | |
} |
Floating-Point Literals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.assignments; | |
public class FloatingPoint { | |
public static void main(String[] args) { | |
float m = 100.88; // does not compile | |
float n = 100.88f; | |
} | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PrimitiveAssignments { | |
public static void main(String[] args) { | |
short sh = 300; // by default integer literals are int | |
short sh1 = 127 * 5; // By default a result of an expression is an int | |
//short sh2 = 127 * 1000; Does not compile because the int returned does not fit in a range of short | |
short sh3 = 5; | |
short sh4 = 127; | |
//short sh5 = sh3 * sh4; //Does not compile because the value of a variable can change at runtime, needs a cast | |
short sh5 = (short)(sh3 * sh4); // Works fine using casting. | |
} | |
} |
Floating Assignments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FloatingAssignments { | |
public static void main(String[] args) { | |
//float f = 32.5; Does not compile because a floating literals are always double | |
float f = (float) 32.3; // Works fine, using casting | |
float g = 32.3f; // Works fine, suffix f | |
float h = 32.3F; // Works fine, suffix F | |
} | |
} |
! Important
+=, -=, *=, and /= will all put in an implicit cast.
byte b = 127;
b+= 7;