一、包装类与基本数据类型
* 包装类与基本数据类型
* 包装类是将基本数据类型封装成一个类,包含属性和方法
* 使用:
* 在使用过程中,会涉及到自动装箱和自动拆箱
* 装箱:将基本数据类型转换成包装类
* 拆箱:将包装类转换成基本数据类型
*
*/
public class TestInteger {
public static void main(String[] args) {
// int a = 10;
//自动装箱
// Integer a1 = Integer.valueOf(a);
// Integer i = new Integer(10);
//自动拆箱
// i = i.intValue();
// System.out.println(a == i);
// Integer i1 = 100;
// Integer i2 = 100;
// Integer i3 = 200;
// Integer i4 = 200;
// System.out.println(i1 == i2);
// System.out.println(i3 == i4);
// Double d1 = 10.0;
//自动装箱
// Double d1 = Double.valueOf(10.0);
// Double d2 = 10.0;
// Double d3 = 20.0;
// Double d4 = 20.0;
// System.out.println(d1 == d2);
// System.out.println(d3 == d4);
//字符串类型转化成Integer类型
// String s = "123";
// Integer i1 = Integer.valueOf(s);
// int i2 = Integer.parseInt(s);
// Integer i3 = new Integer(s);
// System.out.println(i3 + 1);
// String s = "abc";
// int i = Integer.parseInt(s);
//Integer类型转化成字符串类型
Integer i = 10;
String s1 = i.toString();
String s2 = String.valueOf(i);
String s3 = i + "";
System.out.println(s3 + 1);
//String-->double ?
}
}
二、字符串的本质与使用
* 注意:常量池在1.7之后放置在了堆空间之中
* 字符串的使用:
* 1、创建
* String str = "abc";
* String str2 = new String("abc");
* 两种方式都可以用,只不过第一种使用比较多
* 2、字符串的本质
* 字符串的本质是字符数组或者叫做字符序列
* String类使用final修饰,不可以被继承
* 使用equals方法比较的是字符数组的每一个位置的值
* String是一个不可变对象
*
*/
public class TestString {
public static void main(String[] args) {
// String s1 = "abc";
// String s2 = new String("abc");
// s2 = s2.intern();
// System.out.println(s1 == s2);
// System.out.println(s1.equals(s2));
// String s = "abcdefghijkalmnopqrst";
//取出指定下标的字符
// System.out.println(s.charAt(2));
//数组拷贝的操作
// System.out.println(s.concat("123"));
//获取指定字符在字符串中第一次出现的索引值
// System.out.println(s.indexOf("a"));
// System.out.println(s.length());
//获取一个从指定下标开始一直到结尾的子字符串
// System.out.println(s.substring(2));
//截头不结尾
// System.out.println(s.substring(2,6));
// String s1 = "abc";
// String s2 = new String("abc");
// s2 = s2.intern();
// System.out.println(s1 == s2);
// String a = "abc";
// String b = "def";
// String c = "abcdef";
// String d = (a + b).intern();
// String e = "abc" + "def";
// System.out.println(c == d);
// System.out.println(c == e);
// String s = "a" + "b" + "c";
// String s1 ="a";
// String s2 = "b";
// String s3 = "c";
// String ss = s1 + s2 + s3;
}
}
三、定义的交通工具Vehicle
1、Vehicle.java
1. 请定义一个交通工具(Vehicle)的类其中有:
属性: 速度(speed)、 体积(size)等,
方法:移动(move())、设置速度(setSpeed(int speed))、加速 speedUp()、减速 speedDown()等。
最后在测试类 Vehicle 中的 main()中实例化一个交通工具对象并通过方法给它初始化 speed,size 的值并且通过打印出来。
另外调用加速、减速的方法对速度进行改变。
*/
public class Vehicle {
private int speed;
private int size;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void move(){
System.out.println("开始移动......");
}
public void speedUp(){
System.out.println("正在加速中.......");
speed += 10;
System.out.println("加速完毕.......,目前速度为:" + getSpeed());
}
public void speedDown(){
System.out.println("正在减速......");
speed -= 5;
System.out.println("减速完毕.......,目前速度为:" + getSpeed());
}
}
2、TestVehicle.java
public class TestVehicle {
public static void main(String[] args){
Vehicle vehicle = new Vehicle();
vehicle.setSpeed(150);
vehicle.setSize(50);
System.out.println("速度为:" + vehicle.getSpeed() + ",体积为:" + vehicle.getSize());
vehicle.move();
vehicle.speedUp();
vehicle.speedDown();
}
}