包装类代码:
package meilinluo;
public class PackagingGroup {//包装类
public static void main(String[] args) {
int a1 = 1;
Integer a2 = new Integer(1);//不推荐使用
//建议使用
Integer a3 = Integer.valueOf(1);
Integer a4 = 1;//自动装箱
Integer a5 = Integer.valueOf(a1);//手工装箱
int a6 = a5.intValue();//手工拆箱
int a7 = a5;//自动拆箱
boolean b1 = true;
Boolean b2 = new Boolean(false);
Boolean b3 = Boolean.valueOf(true);
Boolean b4 = Boolean.getBoolean("false");
Boolean b5 = Boolean.valueOf(b1);//手工装箱
Boolean b6 = b1;//自动装箱
boolean b7 = b5;//自动拆箱
}
}
String与其他类型的转换
package meilinluo;
public class Transition {
public static void main(String[] args) {
//Java中 任何数据类型与String 进行+运算都会变为String类型
int a = 1;
float b = 1.0f;
Boolean e = true;
String c = a + "";
String d = b + "";
String f = e + "";
System.out.println(c);
System.out.println(d);
System.out.println(f);
String g = String.valueOf(a);
System.out.println(g+1);
System.out.println(b+f);
System.out.println(f+a+b+e+c);
}
}
类型转换的小界面:
package meilinluo;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Transition2 {
public static void main(String[] args) {
JFrame frame = new JFrame("字符串转基本型");
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
JTextField jTextField1 = new JTextField("小马宝莉-勇敢柔柔");
panel.add(jTextField1);
jTextField1.setBounds(140,150,120,30);
JTextField jTextField2 = new JTextField("666");
panel.add(jTextField2);
jTextField2.setBounds(140,190,120,30);
JButton button = new JButton("取值");
panel.add(button);
button.setBounds(180,230,60,25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str1 = jTextField1.getText();
String str2 = jTextField2.getText();
System.out.println(str1);
System.out.println(str2);
//基本型 = 包装类.parseXXX(String)
int int1 = Integer.parseInt(str2);//字符串转基本型
System.out.println(int1);
}
});
frame.setVisible(true);
}
}