包装类 匿名对象 Math类 Random类
包装类 匿名对象 Math类 Random类
1.包装类
java.lang 包,java.util,java.io,java.sql
把基本类型的数据转换为对象,每一个基本数据类型在Java.lang包中都有对应的包装类;在集合中不予许存储基本数据类型,就要使用包装类。例:存数字,Integer /char–>Character
包装类的构造方法
所有包装类将它对应的基本数据类型作为参数,来构造实例
除Character 外,其他包装类可将一个字符串作为参数构造实例
例:
packagecn.com.abner.pack;
public class Demo {
public static void main(String[] args) {
//包装类构造方法
//1.所有包装类将它对应的基本数据类型作为参数,来构造实例
int i=9;

      Integer i1=new Integer(i);
      Double i2=new Double(9.8);
      Boolean i3=new Boolean(false);
      Character i4=new Character('A');
      
      System.out.println(i1+" "+i2+" "+i3+" "+i4);
      
      //2.除Character 外,其他包装类可将一个字符串作为参数构造实例
      Integer j1=new Integer("9");      //字符串必须能转换成数字的字符串
      Double j2=new Double("9.8");
      Boolean j3=new Boolean("true");       //只要不是true这四个字母,都是false;
      //Character j4=new Character("'A'");
      
      System.out.println(j1+" "+j2+" "+j3);
 }

}
常用方法
包装类转换为基本数据类型;对象.基本数据类型Value();
基本类型转换为字符串;1.toString 2.+""
字符串转换为基本类型 parse****(字符串);Character除外
基本类型转换为包装类 valueOf()
基本类型转包装类
字符串转包装类,除Character
例:
packagecn.com.abner.pack;
public class Demo1 {
public static void main(String[] args) {
Integer i=new Integer(9);
//包装类转换为基本数据类型
int j=i.intValue();
System.out.println(j); //9

      //基本类型转换为字符串
      int mun=9;
      String srmun=Integer.toString(mun);
      String srmun1=9+" ";
      
      System.out.println(srmun+" "+srmun1); //9 9
      
      //字符串转换为基本类型  parse****(字符串);Character除外
      String A="90";
      int a=Integer.parseInt(A);        
      boolean b=Boolean.parseBoolean("true");
      
      System.out.println(a+" "+b); //90 true
      
      //基本类型转换为包装类 valueOf()   
          //基本类型转包装类
      Integer iNUM=Integer.valueOf(89);
      Double jNum=Double.valueOf(9.8);
      Character d=Character.valueOf('C');
      System.out.println(iNUM+" "+jNum+" "+d);   //89 9.8 C
          //字符串转包装类,除Character
      Integer w=Integer.valueOf("89");
      Double r=Double.valueOf("9.8");
      //Character y=Character.valueOf("'C'");         //编译错误
      
      System.out.println(w+" "+r); //89 9.8
 }

}
装箱和拆箱
装箱:基本类型转包装类的对象;Integer intObject=5;
拆箱:包装类的对象转基本类型;int intValue=intObject;
2.Random类生成随机数
1.Random it=new Random();
2.it.nextInt(10); //生成0~10之间的整数
*如果想生成(1~n的随机数):(it.nextInt(n)+1)
3.Math类
Math.abs(-3.5); //返回3.5 求绝对值
Math.max(3.5,9.5); //返回9.5
int random=(int)(Math.random()*10); //生成一个0~9之间的随机数
4.匿名对象
格式:new 类名称();
匿名对象只能使用唯一的一次

package cn.com.abner.pack;
import java.awt.PageAttributes.OriginType;
import java.util.Scanner;
import javax.print.attribute.standard.OrientationRequested;
public class nimingclass {
public static void main(String[] args) {
//普通使用方式
Scanner input=new Scanner(System.in);
int num=input.nextInt();
System.out.println(“输出的是”+num);
//匿名对象的方式
int num1=new Scanner(System.in).nextInt();
System.out.println(“输出的是”+num1);
//使用匿名对象传参
method(new Scanner(System.in));
System.out.println("------------------------------------");
Scanner it=method1();
int a=it.nextInt()/2;
System.out.println(“输出的是”+a);
}
public static void method(Scanner sc) {

}
//匿名对象作为返回值
public static Scanner method1() {

// Scanner sc1=new Scanner(System.in);
// return sc1;
return new Scanner(System.in);
}
}
5.枚举
由一组固定的常量组成的类型【enum 关键字】
格式:访问修饰符 enum 枚举名字{ 常量 }
例:
packagecn.com.abner.meiju;
public enum Genders {
男,女
}
packagecn.com.abner.meiju;
public class Student {
public Genders sex;

 public static void main(String[] args) {
      Student st=new Student();
      st.sex=Genders.女;
      
      
      System.out.println(st.sex);
 }

}

相关文章:

  • 2021-11-23
  • 2022-12-23
  • 2021-09-02
  • 2021-12-11
  • 2021-10-20
  • 2022-12-23
  • 2022-03-11
猜你喜欢
  • 2021-06-20
  • 2021-07-31
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案