String --》基本数据类型、包装类

1.调用包装类的parseXxx()

String str1 = "123";
//错误情况:
int num1 = (int)str1;
Integer in1 = (Integer)str1;  //对象类型都不一样,没法强转

//正确方法
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);

String str2 = "true";

boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);

2.包装类面试题

Object o1 = true ? new Integer(1) : new Double(2.0);
        System.out.println(o1); //1.0   编译会全部执行,会经过double,经过之后会有变化

3.面试题

 public void test3(){
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j); //fasle  new两个方法 比较的是地址值

        Integer m = 1;
        Integer n = 1;
        System.out.println(n == m); //true  自动装箱成基本数据类型 比较的是值

        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y); //false  ??不会  (超出范围,new了对象)
        //Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],
        //保存了从-128 - 127范围的证书 超出范围就无法自动装箱,会new对象
    }

4.练习题

package atguigu_oop_day10.Tset07;

import java.util.Scanner;
import java.util.Vector;

public class WrapperTest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Vector v= new Vector();
        int max = 0;

        for (;;){
            System.out.println("请输入成绩");
            int score = scanner.nextInt();
            if (score < 0){
                break;
            }
            if (score > 100){
                System.out.println("输入有误,请重新输入");
                continue;
            }
            if (max < score){
                max= score;
            }

            Integer intScore = new Integer(score);
            v.addElement(intScore);
        }
        System.out.println("最高分数:" + max);

        for (int i = 0; i < v.size(); i++) {

            int score = (int) v.elementAt(i);

            if (max - score >= 30){
                System.out.println("第"+ (i + 1) + "个同学是D等");
            }else if (max - score < 30 && max - score >= 20){
                System.out.println("第"+ (i + 1) + "个同学是C等");
            }else if (max - score < 20 && max - score >= 10){
                System.out.println("第"+ (i + 1) + "个同学B等");
            }else {
                System.out.println("第"+ (i + 1) + "个同学是A等");
            }
        }
    }
}

5.static、finnal关键字

//finnal之后断子绝孙 ,不能继承

//静态导入包
import static java.lang.Math.random
//可以不使用Math.random调用,直接random()


//static属于一个类的,非static属于对象
//只执行一次,常用于赋初始值

public void Student(){

    private static int age;  //静态变量

    private double score;  //非静态变量 

}


public static void main(String[] args) {
    //调用age 
    System.out.println(Studnet.age);

    //调用score
    Student s1 = new Student();
    System.out.println(s1.score);
}

6.抽象类

父类有抽象类,只有方法名,子类继承需重写方法,子类如果是抽象类,子子类实现这个方法。

后面有人帮实现。

6.1抽象类不能new,不能实例化,只能子类实现

6.2抽象类中可以写普通方法

6.3抽象方法必须在抽象类中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值