今日内容介绍
1、基本类型包装类
2、System类
3、Math类
4、Arrays类
5、大数据运算

01基本数据类型对象包装类概述

  1. *A:基本数据类型对象包装类概述
  2. *a.基本类型包装类的产生
  3. 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。
  4. 而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,
  5. 如年龄需要转换成int类型,考试成绩需要转换成double类型等
  6. *b.八种基本类型对应的包装类
  7. char Character
  8. int Integer
  9. byte Byte
  10. short Short
  11. long Long
  12. float Float
  13. double Double
  14. boolean Boolean

02Integer类parseInt方法

  1. *A:IntegerparseInt方法:
  2. *a:parseInt()
  3. int i = Integer.parseInt("12");
  4. System.out.println(i/2);//6
  5. *b:parseInt(String s, int radix)
  6. /*
  7. * Integer类静态方法parseInt(String s, int radix)
  8. * radix基数,进制
  9. * "110",2 含义 前面的数字是二进制的,但是方法parseInt运行结果都是十进制
  10. * 指定进制的字符串转换为十进制的整数
  11. */
  12. public static void function_1(){
  13. int i = Integer.parseInt("110", 2);
  14. System.out.println(i);
  15. }

03Integer类int转成字符串

  1. *A:Integerint转成字符串:
  2. *a:使用+与字符串拼接
  3. int i = 3;
  4. String s = i+"";
  5. System.out.println(s+1);//"31"
  6. *b:toString(int ,int 进制),任意进制整数转成任意进制的字符串 (了解)
  7. String s1 = Integer.toString(5,2);
  8. System.out.println(s1);

04Integer类构造方法

  1. *A:Integer类构造方法
  2. /*
  3. * Integer类构造方法
  4. * Integer (String s)
  5. * 将数字格式的字符串,传递到Integer类的构造方法中
  6. * 创建Integer对象,包装的是一个字符串
  7. * 将构造方法中的字符串,转成基本数据类型,调用方法,非静态的, intValue()
  8. */
  9. public static void function_3(){
  10. Integer in = new Integer("100");
  11. int i = in.intValue();
  12. System.out.println(--i);//99
  13. }

05Integer类其他方法

  1. A:Integer类其他方法
  2. /*
  3. * Integer类的3个静态方法
  4. * 做进制的转换
  5. * 十进制转成二进制 toBinarString(int)
  6. * 十进制转成八进制 toOctalString(int)
  7. * 十进制转成十六进制 toHexString(int)
  8. * 三个方法,返回值都是以String形式出现
  9. */
  10. a:十进制转二,八,十六进制
  11. public static void function_1(){
  12. System.out.println(Integer.toBinaryString(99));
  13. System.out.println(Integer.toOctalString(99));
  14. System.out.println(Integer.toHexString(999));
  15. }
  16. b:获取int的最大值和最小值
  17. /*
  18. * Integer类的静态成员变量
  19. * MAX_VALUE
  20. * MIN_VALUE
  21. */
  22. public static void function(){
  23. System.out.println(Integer.MAX_VALUE);
  24. System.out.println(Integer.MIN_VALUE);
  25. }

06自动装箱和自动拆箱

  1. *A:自动装箱与自动拆箱:
  2. //JDK1.5新特性
  3. //自动装箱,拆箱的 好处: 基本类型和引用类直接运算
  4. //自动装箱:使用Integer.valueOf(整数值)返回一个封装了该整数值的Integer对象
  5. //自动拆箱:使用Integer对象.intValue()返回Integer对象中封装的整数值
  6. public static void function(){
  7. //引用类型 , 引用变量一定指向对象
  8. //自动装箱, 基本数据类型1, 直接变成了对象
  9. Integer in = 1; // Integer in = new Integer(1)
  10. //in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型
  11. //in+1 ==> in.inValue()+1 = 2
  12. //in = 2 自动装箱
  13. in = in + 1;
  14. System.out.println(in);
  15. }

07自动装箱和自动拆箱练习题

  1. *A:自动装箱与自动拆箱:
  2. Integer i = new Integer(1);
  3. Integer j = new Integer(1);
  4. System.out.println(i==j);// false 对象地址
  5. System.out.println(i.equals(j));// true 继承Object重写equals,比较的对象数据
  6. System.out.println("===================");
  7. Integer a = 500;//Integer integer=Integer.valueOf(500)
  8. //integer=new Integer(500);
  9. Integer b = 500;
  10. System.out.println(a==b);//false
  11. System.out.println(a.equals(b));//true
  12. System.out.println("===================");
  13. //数据在byte(-128~127)范围内,JVM不会从新new对象
  14. Integer aa = 127; // Integer aa = new Integer(127)
  15. Integer bb = 127; // Integer bb = aa;
  16. System.out.println(aa==bb); //true
  17. System.out.println(aa.equals(bb));//true

08System类方法currentTimeMillis

  1. *A:System类方法currentTimeMillis():用于计算程序的执行时间
  2. /*
  3. * 获取系统当前毫秒值
  4. * static long currentTimeMillis()
  5. * 对程序执行时间测试
  6. */
  7. public static void function(){
  8. long start = System.currentTimeMillis();//当前时间x-1970年1月1日零时零分零秒
  9. for(int i = 0 ; i < 10000; i++){
  10. System.out.println(i);
  11. }
  12. long end = System.currentTimeMillis();//当前时间y-1970年1月1日零时零分零秒
  13. System.out.println(end - start);//当前时间y-当前时间x
  14. }

09System类方法exit

  1. *A:System类方法exit()方法
  2. /*
  3. * 退出虚拟机,所有程序全停止
  4. * static void exit(0)
  5. */
  6. public static void function_1(){
  7. while(true){
  8. System.out.println("hello");
  9. System.exit(0);//该方法会在以后的finally代码块中使用(讲到再说)
  10. }
  11. }

10System类方法gc

  1. A:System类方法gc
  2. public class Person {
  3. public void finalize(){
  4. System.out.println("垃圾收取了");
  5. }
  6. }
  7. /*
  8. * JVM在内存中,收取对象的垃圾
  9. * 当没有更多引用指向该对象时,会自动调用垃圾回收机制回收堆中的对象
  10. * 同时调用回收对象所属类的finalize方法()
  11. * static void gc()
  12. */
  13. public static void function_2(){
  14. new Person();
  15. new Person();
  16. new Person();
  17. new Person();
  18. new Person();
  19. new Person();
  20. new Person();
  21. new Person();
  22. System.gc();
  23. }

11System类方法getProperties

  1. A:System类方法getProperties(了解)
  2. /*
  3. * 获取当前操作系统的属性:例如操作系统名称,
  4. * static Properties getProperties()
  5. */
  6. public static void function_3(){
  7. System.out.println( System.getProperties() );
  8. }

12System类方法arraycopy

  1. A:System类方法arraycopy
  2. /*
  3. * System类方法,复制数组
  4. * arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  5. * Object src, 要复制的源数组
  6. * int srcPos, 数组源的起始索引
  7. * Object dest,复制后的目标数组
  8. * int destPos,目标数组起始索引
  9. * int length, 复制几个
  10. */
  11. public static void function_4(){
  12. int[] src = {11,22,33,44,55,66};
  13. int[] desc = {77,88,99,0};
  14. System.arraycopy(src, 1, desc, 1, 2);//将src数组的1位置开始(包含1位置)的两个元素,拷贝到desc的1,2位置上
  15. for(int i = 0 ; i < desc.length ; i++){
  16. System.out.println(desc[i]);
  17. }
  18. }

13Math类的方法_1

  1. A:Math类中的方法
  2. /*
  3. * static double sqrt(double d)
  4. * 返回参数的平方根
  5. */
  6. public static void function_4(){
  7. double d = Math.sqrt(-2);
  8. System.out.println(d);
  9. }
  10. /*0
  11. * static double pow(double a, double b)
  12. * a的b次方
  13. */
  14. public static void function_3(){
  15. double d = Math.pow(2, 3);
  16. System.out.println(d);
  17. }
  18. /*
  19. * static double floor(double d)
  20. * 返回小于或者等于参数d的最大整数
  21. */
  22. public static void function_2(){
  23. double d = Math.floor(1.5);
  24. System.out.println(d);
  25. }
  26. /*
  27. * static double ceil(double d)
  28. * 返回大于或者等于参数d的最小整数
  29. */
  30. public static void function_1(){
  31. double d = Math.ceil(5.1);
  32. System.out.println(d);
  33. }
  34. /*
  35. * static int abs(int i)
  36. * 获取参数的绝对值
  37. */
  38. public static void function(){
  39. int i = Math.abs(0);
  40. System.out.println(i);
  41. }

14Math类的方法_2

  1. A:Math类的方法_2
  2. /*
  3. * static double round(doubl d)
  4. * 获取参数的四舍五入,取整数
  5. */
  6. public static void function_6(){
  7. double d = Math.round(5.4195);
  8. System.out.println(d);
  9. }
  10. /*
  11. * static double random() 返回随机数 0.0-1.0之间
  12. * 来源,也是Random类
  13. */
  14. public static void function_5(){
  15. for(int i = 0 ; i < 10 ;i++){
  16. double d = Math.random();
  17. System.out.println(d);
  18. }
  19. }

15Arrays工具类

  1. A:Arrays工具类:
  2. public class ArraysDemo {
  3. public static void main(String[] args) {
  4. function_2();
  5. int[] arr = {56,65,11,98,57,43,16,18,100,200};
  6. int[] newArray = test(arr);
  7. System.out.println(Arrays.toString(newArray));
  8. }
  9. /*
  10. * 定义方法,接收输入,存储的是10个人考试成绩
  11. * 将最后三个人的成绩,存储到新的数组中,返回新的数组
  12. */
  13. public static int[] test(int[] arr){
  14. //对数组排序
  15. Arrays.sort(arr);
  16. //将最后三个成绩存储到新的数组中
  17. int[] result = new int[3];
  18. //成绩数组的最后三个元素,复制到新数组中
  19. // System.arraycopy(arr, 0, result, 0, 3);
  20. for(int i = 0 ; i < 3 ;i++){
  21. result[i] = arr[i];
  22. }
  23. return result;
  24. }
  25. /*
  26. * static String toString(数组)
  27. * 将数组变成字符串
  28. */
  29. public static void function_2(){
  30. int[] arr = {5,1,4,6,8,9,0};
  31. String s = Arrays.toString(arr);
  32. System.out.println(s);
  33. }
  34. /*
  35. * static int binarySearch(数组, 被查找的元素)
  36. * 数组的二分搜索法
  37. * 返回元素在数组中出现的索引
  38. * 元素不存在, 返回的是 (-插入点-1)
  39. */
  40. public static void function_1(){
  41. int[] arr = {1,4,7,9,11,15,18};
  42. int index = Arrays.binarySearch(arr, 10);
  43. System.out.println(index);
  44. }
  45. /*
  46. * static void sort(数组)
  47. * 对数组升序排列
  48. */
  49. public static void function(){
  50. int[] arr = {5,1,4,6,8,9,0};
  51. Arrays.sort(arr);
  52. for (int i = 0; i < arr.length; i++) {
  53. System.out.println(arr[i]);
  54. }
  55. }
  56. }

16数组复制练习

  1. *A:数组复制练习:
  2. public static void main(String[] args) {
  3. int[] arr = {56,65,11,98,57,43,16,18,100,200};
  4. int[] newArray = test(arr);
  5. System.out.println(Arrays.toString(newArray));
  6. }
  7. /*
  8. * 定义方法,接收输入,存储的是10个人考试成绩
  9. * 将最后三个人的成绩,存储到新的数组中,返回新的数组
  10. */
  11. public static int[] test(int[] arr){
  12. //对数组排序
  13. Arrays.sort(arr);
  14. //将最后三个成绩存储到新的数组中
  15. int[] result = new int[3];
  16. //成绩数组的最后三个元素,复制到新数组中
  17. //System.arraycopy(arr, 0, result, 0, 3);
  18. for(int i = 0 ; i < 3 ;i++){
  19. result[i] = arr[i];
  20. }
  21. return result;
  22. }

17BigInteger类概述和构造方法

  1. A:BigInteger类概述和构造方法
  2. public static void main(String[] args) {
  3. function();
  4. }
  5. /*
  6. * BigInteger类的构造方法
  7. * 传递字符串,要求数字格式,没有长度限制
  8. */
  9. public static void function(){
  10. BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
  11. System.out.println(b);
  12. BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
  13. System.out.println(b1);
  14. }

18BigInteger类四则运算

  1. A:BigInteger类四则运算
  2. public static void main(String[] args) {
  3. function_1();
  4. }
  5. /*
  6. * BigInteger对象的四则运算
  7. * 调用方法计算,计算结果也只能是BigInteger对象
  8. */
  9. public static void function_1(){
  10. BigInteger b1 = new BigInteger("5665464516451051581613661405146");
  11. BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
  12. //计算 b1+b2对象的和,调用方法 add
  13. BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
  14. System.out.println(bigAdd);
  15. //计算b1-b2对象的差,调用方法subtract
  16. BigInteger bigSub = b1.subtract(b2);
  17. System.out.println(bigSub);
  18. //计算b1*b2对象的乘积,调用方法multiply
  19. BigInteger bigMul = b1.multiply(b2);
  20. System.out.println(bigMul);
  21. //计算b2/b1对象商,调用方法divied
  22. BigInteger bigDiv = b2.divide(b1);
  23. System.out.println(bigDiv);
  24. }

19员工案例的子类的编写

  1. A:BigDecimal类概述
  2. /*
  3. * 计算结果,未知
  4. * 原因: 计算机二进制中,表示浮点数不精确造成
  5. * 超级大型的浮点数据,提供高精度的浮点运算, BigDecimal
  6. System.out.println(0.09 + 0.01);//0.09999999999999999
  7. System.out.println(1.0 - 0.32);//0.6799999999999999
  8. System.out.println(1.015 * 100);//101.49999999999999
  9. System.out.println(1.301 / 100);//0.013009999999999999
  10. */

20BigDecimal类实现加法减法乘法

  1. A:BigDecimal类实现加法减法乘法
  2. /*
  3. * BigDecimal实现三则运算
  4. * + - *
  5. */
  6. public static void function(){
  7. BigDecimal b1 = new BigDecimal("0.09");
  8. BigDecimal b2 = new BigDecimal("0.01");
  9. //计算b1+b2的和,调用方法add
  10. BigDecimal bigAdd = b1.add(b2);
  11. System.out.println(bigAdd);
  12. BigDecimal b3 = new BigDecimal("1");
  13. BigDecimal b4 = new BigDecimal("0.32");
  14. //计算b3-b2的差,调用方法subtract
  15. BigDecimal bigSub = b3.subtract(b4);
  16. System.out.println(bigSub);
  17. BigDecimal b5 = new BigDecimal("1.015");
  18. BigDecimal b6 = new BigDecimal("100");
  19. //计算b5*b6的成绩,调用方法 multiply
  20. BigDecimal bigMul = b5.multiply(b6);
  21. System.out.println(bigMul);
  22. }

21BigDecimal类实现除法

  1. A:BigDecimal类实现除法
  2. /*
  3. * BigDecimal实现除法运算
  4. * divide(BigDecimal divisor, int scale, int roundingMode)
  5. * int scale : 保留几位小数
  6. * int roundingMode : 保留模式
  7. * 保留模式 阅读API文档
  8. * static int ROUND_UP 向上+1
  9. * static int ROUND_DOWN 直接舍去
  10. * static int ROUND_HALF_UP >= 0.5 向上+1
  11. * static int ROUND_HALF_DOWN > 0.5 向上+1 ,否则直接舍去
  12. */
  13. public static void function_1(){
  14. BigDecimal b1 = new BigDecimal("1.0301");
  15. BigDecimal b2 = new BigDecimal("100");
  16. //计算b1/b2的商,调用方法divied
  17. BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
  18. System.out.println(bigDiv);
  19. }

作业测试

1.用循环实现不死神兔

故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,
再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
问:一对刚出生的兔子,一年内繁殖成多少对兔子?

1 1 2 3 5 8 13 21

2.第100个月繁殖多少对兔子?(利用BigInteger完成)

17_常用API_第17天(包装类、System、Math、Arrays、大数据运算)_讲义的更多相关文章

  1. 常用API(包装类、System、Math、Arrays、大数据运算)

    常用API 今日内容介绍 u 基本类型包装类 u System u Math u Arrays u BigInteger u BigDecimal 第1章 基本类型包装类 大家回想下,在第二天我们学习 ...

  2. java基础(17):包装类、System、Math、Arrays、大数据运算

    1. 基本类型包装类 大家回想下,在第三篇文章中我们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢? 1.1 基本类型 ...

  3. 基本类型包装类、System类、Math类、Arrays类、大数据运算

    1 基本类型包装类 Java中想对8种基本数据类型进行复杂操作很困难. 实际程序界面上用户输入的数据都是以字符串类型进行存储的. 程序开发中,需要把字符串转换成指定的基本数据类型. 1.1基本数据类型 ...

  4. Java—包装类/System类/Math类/Arrays类/大数据运算/Collection接口/Iterator迭代器

    基本类型包装类 8种基本类型对应的包装类如: 将字符串转成基本类型: 将基本数值转成字符串有3种方式: 基本类型直接与””相连接即可:34+" " 调用String的valueOf ...

  5. 7、包装类、System、Math、Arrays、大数据运算

    基本类型封装 基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字 ...

  6. JAVA基础之基本类型包装类、System类、Math类、Arrays类及大数据运算

    个人理解: 为了方便运算及调用一些方法,我们需要将基本类型的数值转换为对象:不过转换的时候需要特别注意好它们的类型到底是什么,需要调用方法的类名是哪个!特别注意是Byte常量池的相关问题(==):gc ...

  7. java 基本类型包装类,system类,Math类,Assrays类,大数据运算

    实现字符串与基本数据之间转换 将字符串转成基本数据类型方法 例如:将字符串转成成int类型 String str ="123"; int a =Integer.parseInt(s ...

  8. 日期类&&包装类&&System类&&Math类&&Arrays数组类&&大数据类

    day 07 日期类 Date 构造函数 Date():返还当前日期. Date(long date):返还指定日期 date:时间戳--->距离1970年1月1日 零时的毫秒数 常用方法 日期 ...

  9. Java基础学习笔记十四 常用API之基本类型包装类

    基本类型包装类 Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢?在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们 ...

随机推荐

  1. 深入虚拟内存(Virtual Memory,VM)

    我们应该知道物理内存(Physical Memory)指的是硬件上的内存,即 RAM.它通常指的是插在主板上的内存条,给进程提供临时数据存储的设备.因为 CPU 可以直接从物理内存中读取数据和指令,所 ...

  2. 【转】JavaScript操作SVG的一些知识

    原文:http://blog.iderzheng.com/something-about-svg-with-javascript/ 前阵子学习了一下SVG(Scalable Vector Graphi ...

  3. Hibernate第三天——表间关系与级联操作

    第三天,我们来使用Hibernate进行表之间一对多 多对多关系的操作: 这里我们先利用两个例子进行表关系的回顾: 一对多(重点): 例如分类和商品的关系,一个分类多个商品,一个商品属于一个分类 CR ...

  4. WPF 自定义ComboBox样式,自定义多选控件

    原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...

  5. python基础学习1-json,pickle的序列化和反序列化

    import requests import json dic={'k1':'v1'} print(dic,type(dic)) result = json.dumps(dic)#调用dumps方法把 ...

  6. 使用SDNN (space displacement neural network)进行多字体手写识别

    手写单字体的识别,在看过卷积神经网络的mnist例子之后,很容易实现,那么如何实现多字体的同时识别呢? 如下图 LeCun大神所用的是SDNN space displacement neural ne ...

  7. 洛咕 P2468 [SDOI2010]粟粟的书架

    强行二合一啊... 前面直接二分最小值,二维前缀和.后面用主席树查最小值.注意要写\(nlogn\). // luogu-judger-enable-o2 #include<bits/stdc+ ...

  8. mysql查询操作之单表查询、多表查询、子查询

    一.单表查询 单表查询的完整语法: .完整语法(语法级别关键字的排列顺序如下) select distinct 字段1,字段2,字段3,... from 库名.表名 where 约束条件 group ...

  9. SQL Server 小数类型(float 和 decimal)

    在SQL Server中,实际上小数数值只有两种数据类型:float 和 decimal,分别是近似数值和精确数值.其他小数类型,都可以使用float和decimal来替代,例如,双精度(double ...

  10. CSS快速入门-鼠标悬浮(hover伪类)

    一.概述 hover伪类:在鼠标移到元素上时向此元素添加特殊的样式.比较普通的就是一个url,当你鼠标放上去后,会变颜色. 在现实的应用场景也非常之多.最常见的是网站的悬浮导航,当鼠标放到导航条上时, ...