-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeConversionReference.java
More file actions
33 lines (23 loc) · 1004 Bytes
/
TypeConversionReference.java
File metadata and controls
33 lines (23 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Vector;
public class TypeConversionReference{
public static void main(String[] args) {
Object obj = new Object();
String str = new String("A string-object");
System.out.println("\nobj = " + obj);
obj = str;
System.out.println("\nobj = " + obj);
System.out.println("str = " + str);
obj = new Object();
//str = obj; does not compile, due to demoting type conversion
//str = (String) obj; does not run (but compiles!) due to same reason as above, despite of explicit type conversion
Object objStr = new String("String-object with object-type reference");
System.out.println("\nobjStr = " + objStr);
//str = objStr; demoting type conversion, i.e. IMPLICIT
str = (String) objStr; // now EXPLICIT, which works
System.out.println("\nstr = " + str);
Vector vec = new Vector();
str = new String("A new string-object");
//vec = str; compiling error due to incompatible types
//vec = (Vector) str; explicit conversion fails as types are inconvertible
}
}