-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeConversion.java
More file actions
50 lines (42 loc) · 1.57 KB
/
TypeConversion.java
File metadata and controls
50 lines (42 loc) · 1.57 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package problems;
public class TypeConversion {
public static void main(String[] args) {
// Create Object type variables to be used in the solution function
Object ob = "12";
Object ob1 = 12;
Object ob2 = 'c';
// Print out the class types before using the solution function
System.out.println(ob.getClass());
System.out.println(ob1.getClass());
System.out.println(ob2.getClass());
System.out.println("");
// Using the solution function to convert
// the data types of the variables
Object sol = solution(ob);
Object sol1 = solution(ob1);
Object sol2 = solution(ob2);
// Print out the class types after using the solution function
System.out.println(sol.getClass());
System.out.println(sol1.getClass());
System.out.println(sol2.getClass());
}
// This function takes in an Object type and converts it based on its datatype
public static Object solution(Object object){
if(object instanceof Integer){
int instance = (Integer) object;
String convert = String.valueOf(instance);
return convert;
}
if(object instanceof String){
String instance = (String) object;
int convert = Integer.parseInt(instance);
return convert;
}
if(object instanceof Character){
Character instance = (Character) object;
String convert = Character.toString(instance);
return convert;
}
return null;
}
}