Java提供了各种数据类型来存储各种数据值。它提供7种原始数据类型(存储单个值),即布尔,字节,字符,短,整数,长,浮点,双精度和引用数据类型(数组和对象)。

用Java进行转换

将一种原始数据类型转换为另一种原始数据类型称为类型转换。

示例import java.util.Scanner;
public class TypeCastingExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value: ");
int i = sc.nextInt();
long num = i;
System.out.println("Value of the given integer: "+num);
}
}
输出结果Enter an integer value:

421

强制转换引用的数据类型

是的,您可以将一种(类)类型的引用(对象)转换为另一种。但是,两个类之一应该继承另一个。

例如,假设我们有一个名称为Person的类,它有两个实例变量name和age和一个displayPerson()显示名称和age的实例方法。public class Person{

private String name;
private int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void displayPerson() {
System.out.println("Data of the Person class: ");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
}
}

另一个名为Student的类扩展了人员类,除了继承的名称和年龄外,它还有两个变量branch和student_id。它具有一种displayData()显示所有四个值的方法。

public class Student extends Person {
public String branch;
public int Student_id;
public Student(String name, int age, String branch, int Student_id){
super(name, age);
this.branch = branch;
this.Student_id = Student_id;
}
public void displayStudent() {
System.out.println("Data of the Student class: ");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Branch: "+this.branch);
System.out.println("Student ID: "+this.Student_id);
}
public static void main(String[] args) {
..............
}
}
将子类变量转换为超类类型
现在,在main方法中,您可以分别创建两个类的对象,并将超类对象直接转换为子类。public static void main(String[] args) {
//创建学生类的对象
Student student = new Student("Krishna", 20, "IT", 1256);
//将学生的对象转换为人
Person person = new Person("Krishna", 20);
//将学生的对象转换为人
person = (Student) student;
person.displayPerson();
}
输出结果Data of the Person class:
Name: Krishna
Age: 20
简而言之,超类引用变量可以保存子类对象。但是,使用此引用,您只能访问超类的成员,如果尝试访问子类的成员,则会生成编译时错误。
示例public static void main(String[] args) {
Person person = new Student("Krishna", 20, "IT", 1256);
person.displayStudent();
}
输出结果Student.java:33: error: cannot find symbol
person.dispalyStudent();
^
symbol: method dispalyStudent()  location: variable person of type Person
1 error
将超类变量转换为子类
以相同的方式,您可以尝试将超类变量转换为子类以实现此目的,这与之前的情况不同,您需要使用强制转换运算符。
示例public static void main(String[] args) {
//创建学生类的对象
Student student = new Student("Krishna", 20, "IT", 1256);
//将学生的对象转换为人
Person person = new Person("Krishna", 20);
//将人的对象转换为学生
student = (Student) person;
student.displayPerson();
student.displayStudent();
}

对此参考,这两个类的成员均可用,并且程序已成功编译。

但是,当您尝试执行它时,将引发一个异常,如下所示-

Exception in thread "main" java.lang.ClassCastException: ther.Person cannot be cast to ther.Student
at ther.Student.main(Student.java:41)
要解决此问题,首先需要使用子类对象创建超类引用,然后使用强制转换运算符将此(超级)引用类型转换为子类类型。
示例public static void main(String[] args) {
//将学生的对象转换为人
Person person = new Student("Krishna", 20, "IT", 1256);
//将人的对象转换为学生
Student student = (Student) person;
student.displayPerson();
student.displayStudent();
}
输出结果Data of the Person class:
Name: Krishna
Age: 20
Data of the Student class:
Name: Krishna
Age: 20
Branch: IT
Student ID: 1256