Professional Documents
Culture Documents
Outline
1 2
3 4
Heap Memory
3 4
1
5 6
5 6
7 8
7 8
2
9 10
9 10
String t = s; String t = s;
int i = 201; int i = 201;
int j = i; int j = i;
s = new String(“goodbye”); s = new String(“goodbye”);
t = new String(“bye”); t = new String(“bye”);
“goodbye”
i 201 i 201
“bye”
j 201 j 201
11 12
3
13 14
13 14
15 16
15 16
4
17 18
17 18
@NGUYỄN Thị Thu Trang, trangntt@soict.hust.edu.vn 19 @NGUYỄN Thị Thu Trang, trangntt@soict.hust.edu.vn 20
University
Artifacts Student CourseOffering
19 20
5
21 22
• package oolt.hedspi;
• class IS1{
• void is1_method(){}
•}
21 22
23 24
23 24
6
25 26
25 26
27 28
27 28
7
29 30
• Wrappers
• Number, Boolean, Byte, Character, Double, Float, Integer, Long, Short, Void
1. Memory management in Java
• Character and String Manipulation
• Character.Subset, String, StringBuffer, Character.UnicodeBlock
2. Class organization
• Math Functions
• Math, StrictMath 3. Utility classes in Java
• Runtime Model
• Process, Runtime, Thread, ThreadGroup, ThreadLocal, InheritableThreadLocal,
RuntimePermission
• JVM
• ClassLoader, Compiler, SecurityManager
• Exception Handling
• StackTraceElement, Throwable
• Also contains Interfaces, Exceptions and Errors
29 30
31 32
31 32
8
33 34
33 34
35 36
Constants Example
• Boolean • Float
double d = (new Integer(Integer.MAX_VALUE)).
• Boolean FALSE • float MAX_VALUE doubleValue();
• Boolean TRUE • float MIN_VALUE
System.out.println(d); // 2.147483647E9
• Byte • float NaN
• byte MIN_VALUE • float NEGATIVE_INFINITY
• byte MAX_VALUE • float POSITIVE_INFINITY String input = "test 1-2-3";
• Character • Integer int output = 0;
• int MAX_RADIX • int MIN_VALUE
• char MAX_VALUE • int MAX_VALUE for (int index = 0; index < input.length(); index++)
• int MIN_RADIX • Long {
• char MIN_VALUE • long MIN_VALUE
• Unicode classification constants • long MAX_VALUE char c = input.charAt(index);
• Double • Short if (Character.isDigit(c))
• double MAX_VALUE • short MIN_VALUE
• double MIN_VALUE • short MAX_VALUE
output = output * 10 + Character.digit(c, 10);
• double NaN }
• double NEGATIVE_INFINITY
• double POSITIVE_INFINITY System.out.println(output); // 123
35 36
9
37 38
37 38
39 40
39 40
10
41
s2 Hello “Goodbye”
42
String(“Goodbye”)
41 42
43 44
3.3. StringBuffer/StringBuilder
• String str = “”; • String is an immutable type:
• for (int i=0; i<1.000.000; i++){ • Object does not change the value after being created à
• //read a line from a file Strings are designed for not changing their values.
• str += line;
• Concatenating strings will create a new object to store
•} the result à String concatenation is memory
consuming.
• StringBuffer str = “”; • StringBuffer/StringBuilder is a mutable type:
• for (int i=0; i<1.000.000; i++){ • Object can change the value after being created
• //read a line from a file
• str.append(line);
•}
=> String concatenation can get very expensive,
only use in building a simple String
43 44
11
45 46
45 46
47 48
47 48
12
49 50
49 50
13