java学习(8)

java学习8

这篇来写写java常用的API。

1. 基本数据类型包装类

在程序中,为了方便基本类型的数据做更多的操作,java就针对每种基本类型的数据给出了对应的类类型,即基本类型的包装类。

  基本类型
  byte Byte
  short   Short
  int Integer
  long Long
  float     Float
  double Double
  char Character
  boolean Boolean

除了int类型和char类型的,其他的类名和类型名相同,只是首字母大写了。

这里说说Integer类:
常用字段:
最大值:MAX_VALUE
最小值:MIN_VALUE
代码示例:

System.out.println("输出Integer最大值");
System.out.println(Integer.MAX_VALUE);
System.out.println("输出Integer最小值");
System.out.println(Integer.MIN_VALUE);

调用Integer类的字段MAX_VALUE和MIN_VALUE,可以对int类型的最大值,最小值进行输出



构造方法:
  Integer(int value) 
Integer(String s) //注意:这里的字符串必须是由数字组成的字符串

各种成员方法:
转换为二进制: toBinaryString(int i)
转换为八进制: toOctalString(int i) 
转换为十六进制: toHexString(int i) 

这几个成员方法都是静态的,因此直接使用类名Integer调用。直接使用Integer的方法进行进制转化,比进行运算转化简单了许多,如下:

int i = 123;
//输出原数
System.out.println(i);
//转换为二进制:toBinaryString(int i)
String str1 = Integer.toBinaryString(i);
System.out.println(str1);
//转换为八进制:toOctalString(int i) 
String str2 = Integer.toOctalString(i);
System.out.println(str2);
//转换为十六进制:toHexString(int i)
String str3 = Integer.toHexString(i);
System.out.println(str3);
结果:



 int和String类型的相互转换:
  这里给出三种int类型向String类型转化的方法:
方式1: String s = 100 +"";
方式2: String.valueOf(int)
方式3: nteger.toString(100);
  代码示例:

//方式1:
//String s = 100 +"";
int i = 120;
String str1 = i+"";
System.out.println(str1);
//方式2:
  //String.valueOf(int)
String str2 = String.valueOf(130);
System.out.println(str2);
//方式3:
//.toString(100);
String str3 = Integer.toString(140);
System.out.println(str3);

结果:


  String -- >int
方式1:
  Integer.parseInt(String)

代码示例:

//Integer.parseInt(String)
int i2 = Integer.parseInt("12313");
System.out.println(i2);

结果:


 
JDK5加的新特性:

可以对基本类型和包装类进行自动的装箱和拆箱操作,简单了很多:
  自动装箱:基本类型 -- 包装类类型
  Integer.valueOf(100)
  自动拆箱:包装类类型 -- 基本类型
  i.intValue()

代码示例:

//自动装箱:基本类型 -- 包装类类型
//Integer.valueOf(100)
System.out.println("自动装箱操作:");
Integer integer1 = Integer.valueOf(123);
System.out.println(integer1);
//自动拆箱:包装类类型 -- 基本类型
//i.intValue()
System.out.println("自动拆箱操作:");
int i = integer1.intValue();
System.out.println(i);

结果:



2. Character类
  Character:Character 类在对象中包装一个基本类型 char 的值,Character很多方法是静态的,可以直接通过类名调用。
  构造方法:
  public Character(char value)
 成员方法
  public static boolean isUpperCase(char ch) //判断是否是大写字符
  public static boolean isLowerCase(char ch) //判断是否是小写字符
  public static boolean isDigit(char ch) //判断是否是数字
  public static char toUpperCase(char ch) //转成大写
  public static char toLowerCase(char ch) //转成小写

代码示例:

//构造方法:
  //public Character(char value)
Character cha = new Character('s');
System.out.println(cha);
System.out.println("==================");
//成员方法
//public static boolean isUpperCase(char ch) 判断是否是大写字符
System.out.println(Character.isUpperCase('B'));
System.out.println(Character.isUpperCase('b'));
System.out.println(Character.isUpperCase('2'));
System.out.println("==================");
// public static boolean isLowerCase(char ch) 判断是否是小写字符
System.out.println(Character.isLowerCase('B'));
System.out.println(Character.isLowerCase('b'));
System.out.println(Character.isLowerCase('2'));
System.out.println("==================");
// public static boolean isDigit(char ch) 判断是否是数字
System.out.println(Character.isDigit('B'));
System.out.println(Character.isDigit('b'));
System.out.println(Character.isDigit('2'));
System.out.println("==================");
// public static char toUpperCase(char ch) 转成大写
System.out.println(Character.toUpperCase('B'));
System.out.println(Character.toUpperCase('b'));
System.out.println("==================");
// public static char toLowerCase(char ch) 转成小写
System.out.println(Character.toLowerCase('B'));
System.out.println(Character.toLowerCase('b'));

结果:

这里的方法比较简单,代码里有详细的体现和具体的注释,不进行过多叙述。

3. Math:用于执行数学运算的类。
  成员方法:
  public static int abs(int a) //取绝对值
  public static double ceil(double a)//向上取整
  public static double floor(double a)//向下取整
  public static int max(int a,int b) //取较大值

public static int min(int a,int b)//取较小值

  public static double pow(double a,double b) // a的b次方
  public static double random()///取随机数,0.0~1.0,不包括1.0,包括0.0
  public static int round(float a) //四舍五入

这里的成员方法就是数学里常用的计算。

代码示例:

//public static int abs(int a),取绝对值
System.out.println(Math.abs(-11));
System.out.println(Math.abs(11));
System.out.println("===============");
 //public static double ceil(double a),向上取整
System.out.println(Math.ceil(11.2));
System.out.println(Math.ceil(11.7));
System.out.println("===============");
 //public static double floor(double a)向下取整
System.out.println(Math.floor(11.2));
System.out.println(Math.floor(11.7));
System.out.println("===============");
 //public static int max(int a,int b),取较大值  
System.out.println(Math.max(5,3));
System.out.println("===============");
//public static int min(int a,int b), 取较小值
System.out.println(Math.min(5,3));
System.out.println("===============");
 //public static double pow(double a,double b)  a的b次方
System.out.println(Math.pow(5,3));
System.out.println("===============");
// public static double random()
System.out.println(Math.random());
System.out.println("===============");
// public static int round(float a) 四舍五入
System.out.println(Math.round(12.4));

结果:


4. Object类

是所有类的根类。所有的类都直接或者间接的继承自该类
4.1 toString()方法
   默认情况下,打印的是对象的字符串表示: 包名...类名+@+该对象的哈希值的十六进制
一般在子类中进行重写,默认打印的字符串没有太大的意义。
4.2 equals():
默认情况下,比较的是地址值,也没有意义,在子类中进行重写用来比较对象的成员变量值是否相同。
 
********************************

==和equals()的区别:
1)==的作用
比较基本类型,比较的是数据值
比较引用类型,比较的是 地址值
2)equals()
只能比较引用类型。默认比较地址值。如果重写后,是按照重写后的规则进行的。

**********************************

5. Scanner:帮助我们从键盘获取数据。
5.1  构造方法:
  Scanner(InputStream source)
  例如:Scanner sc = new Scanner(System.in);
5.2  成员方法:
  int nextInt():获取int类型的数据
  String nextLine():获取String类型的数据
  注意:可能出现的异常:InputMismatchException:输入的数据和最终的结果类型不匹配。需要
要什么类型,就输入什么类型

从键盘录入对象,经常调用Scanner的两个方法,之前代码中已经有很多体现,这里不进行示例了。

5.3  当为同一个Scanner对象,数据的输入顺序问题:
  出现的问题:
  如果先输入int类型,再输入String会出现了问题。

如下:

Scanner sc3 = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int str7 = sc3.nextInt();
System.out.println("请输入第一个字符串:");
String str8 = sc3.nextLine();
System.out.println(str7);
System.out.println(str8);

结果:


  
 解决方法:
  1)所有的数据都按照String来接受,然后需要什么,进行转化。
  2)重新创建一个新的对象,用多个对象去接受数据

6. Random:用于产生随机数的类。类似于Math类的random()方法。

和种子相关(种子其实可以把它看成一个产生随机数的定义的规则)。
  构造方法:
  Random():没有指定种子,采用的是默认种子。
  Random(long seed):可以指定种子。种子相同,产生的随机数就相同。
  成员方法:
  public int nextInt():返回int范围内的数据
  public int nextInt(int n):返回[0,n)之间的数据

代码示例:

//public int nextInt():返回int范围内的数据
Random random = new Random();
System.out.println(random.nextInt());
  //public int nextInt(int n):返回[0,n)之间的数据
Random random1 = new Random();
System.out.println(random1.nextInt(50)+1);

这里第一个产生的随机数是int类型范围内的,第二个是1到50的int类型数据。

结果:


7. System类:包含一些有用的类字段和方法。它不能被实例化。 
  成员方法:
  1)public static void exit(int status):终止当前正在运行的 Java 虚拟机。根据惯例,非 0 的状态码表示异常终止。 

代码示例:

//终止当前正在运行的 Java 虚拟机。根据惯例,非 0 的状态码表示异常终止。
System.out.println(48);
System.exit(0);
System.out.println(14);

exit参数0为正常退出,其他值为异常退出。

  2)public static long currentTimeMillis():获取当前时间的毫秒值。

代码示例:

//public static long currentTimeMillis():获取当前时间的毫秒值。
long currentTimeMillis = System.currentTimeMillis();
System.out.println(currentTimeMillis);

获取当前的时间距1970年零时的时间毫秒差。
  3)public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length):复制数组
  Object src,:要复制的源数组
  nt srcPos:从源数组那个索引开始复制
  Object dest:需要复制到哪个数组中
  int destPos:从哪个索引开始进行覆盖
  int destPos:需要覆盖几个元素

代码示例:

int[] arr1 = {1,3,5,7,9};
int[] arr2 = {2,4,6,8};
System.arraycopy(arr1,3,arr2,1,2);
System.out.println(Arrays.toString(arr2));

结果:



8. Date类 表示特定的瞬间,精确到毫秒。  
  构造方法:
  Date():默认获取的是当前的日期时间
  Date(long date):把当前日期按照指定的毫秒值进行设定
  成员方法:
  public long getTime():获取日期对象的毫秒值
  public void setTime(long time):设置日期对象的毫秒值,和有参构造类似

代码示例:

//Date():默认获取的是当前的日期时间
Date d1 = new Date();
System.out.println(d1);
//public long getTime():获取日期对象的毫秒值
System.out.println(new Date().getTime());
  //Date(long date):把当前日期按照指定的毫秒值进行设定
Date d2 = new Date(1492864876574L);
System.out.println(d2);
//public void setTime(long time):设置日期对象的毫秒值
d1.setTime(1492864876574L);

  System.out.println(d1);

运行结果:



9. DateFormat(格式化和解析日期对象),这是一个抽象类,因此直接使用已知的子类SimpleDateFormat。该类一般用来进行格式化和解析日期对象
  格式化:以一定的模去格式化日期,得到一个规定格式的字符串。调用方法:String format(Date d)
  解析:把字符串按照一个格式,解析成指定的日期格式。 调用方法:Date  parse(String s)
 

9.1 构造:
  SimpleDateFormat()用默认的模式格式化日期对象
  SimpleDateFormat(String pattern):用给定的模式格式化日期对象
  例如:1)yyyy年MM月dd日 2)HH:mm:ss 3)yyyy-MM-dd 4)HH:mm:ss
 
9.2 成员方法:
  public final String format(Date date)将一个 Date 格式化为日期/时间字符串
  public Date parse(String source)throws ParseException从给定字符串的开始解析文本,以生成一个日期对象,会抛出一个异常。

代码示例:

//创建一个Date对象,格式化这个Date对象
//创建日期对象
Date d = new Date();
//创建日期格式化对象,使用无参构造创建的日期格式化对象,格式化Date对象的时候,使用的是默认的格式
SimpleDateFormat sdf = new SimpleDateFormat();
//调用他的format(Date d)格式化这个Date对象
String dateStr = sdf.format(d);
System.out.println(dateStr);

//自定义日期的格式化格式
//重新创建一个日期的格式化对象,并指定相应的模式 :yyyy-MM-dd HH:mm:ss
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr2 = sdf2.format(d);
System.out.println(dateStr2);

结果:



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值