1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java日记:API入门

Java日记:API入门

时间:2020-05-08 04:19:14

相关推荐

Java日记:API入门

Scanner类

通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需 要 使用hasNext() 与hasNextLine()判断是否还有输入的数据。

🔷next(): 🍉1、一定要读取到有效字符后才可以结束输入。 🍉2、对输入有效字符之前遇到的空白,next()方法会自动将其去掉。 🍉3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 🍉4、next()不能得到带有空格的字符串。

🔷nextLine(): 🍉1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。 🍉2、可以获得空白。

别忘了关掉水龙头scanner.close();

System类

1.

System.exit((int status);终d止当前运行的 Java 虚拟机,非零表示异常 终止

status是零参数,那么表示正常退出程序。

status是1或者非零参数,那么表示非正常退出程序。

2.

System.currentTimeMillis();返回当前时间(以毫秒为单位)

3.

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)将数组 中指定的数据拷贝到另一个数组中。

public class Demo2 {2 public static void main(String[] args) {3 int[] src = new int[]{1,2,3,4,5};4 int[] dest = new int[]{6,7,8,9,10};5 System.arraycopy(src, 0, dest, 0, 3);6 System.out.println("src = " + Arrays.toString(src));7 System.out.println("dest = " + Arrays.toString(dest));8 /*代码运行后:两个数组中的元素发生了变化9 src数组元素[1,2,3,4,5]10 dest数组元素[1,2,3,9,10]11 */12 }13 }

Math类

🔷Math类概述及使用 1、Math类概述 Math 包含执行基本数字运算的方法

2、Math中方法的调用方式 Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用

3、Math类的常用方法

public static int abs(int a) 返回参数的绝对值

public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个 整数 代码实例 public static double floor(double a) 返回小于或等于参数的最大double值,等于一个 整数

public static int round(float a) 按照四舍五入返回最接近参数的int

public static int max(int a,int b) 返回两个int值中的较大值

public static int min(int a,int b) 返回两个int值中的较小值

public static double pow (double a,double b) 返回a的b次幂的值

public static double random() 返回值为double的正值,[0.0,1.0)

public class Demo7 {public static void main(String[] args) {int abs = Math.abs(-4);System.out.println("abs = " + abs);double ceil = Math.ceil(3.01);System.out.println("ceil = " + ceil);double floor = Math.floor(4.99);System.out.println("floor = " + floor);long round = Math.round(4.4);System.out.println("round = " + round);int max = Math.max(3, 4);System.out.println("max = " + max);int min = Math.min(4, 5);System.out.println("min = " + min);double pow = Math.pow(2, 2);System.out.println("pow = " + pow);}}

BigDecimal类

BigDecimal:Float和Double

💫作用:可以用来进行精确计算

💫构造方法

BigDecimal bigDecimal = new BigDecimal(xxx);其中,xxx可以是整型数据,也可以是数字内容 的字符串数据,但不可以是浮点型数据,如:

public class Demo8 {public static void main(String[] args) {BigDecimal decimal1 = new BigDecimal(123);//ok 可以但是不推荐BigDecimal decimal2 = new BigDecimal("123.123");//ok 推荐使用BigDecimal decimal3 = new BigDecimal(123.123); // 不ok 不推荐System.out.println("decimal1 = " + decimal1);System.out.println("decimal2 = " + decimal2);System.out.println("decimal3 = " + decimal3);}}

compareTo:pareTo(B)

A>B 得1;A=B 得0;A<B 得-1。

public class Demo3 {2 public static void main(String[] args) {34 BigDecimal one = new BigDecimal("4.321");5 BigDecimal tow = new BigDecimal("1.234");6 BigDecimal three = tow.subtract(one); // tow - one = -3.08778 int result1 = pareTo(tow);9 System.out.println("result1 = " + result1); // 11011 int result2 = pareTo(one);12 System.out.println("result2 = " + result2); // 01314 int result3 = pareTo(tow);15 System.out.println("result3 = " + result3); // -116 }17 }

💫常用方法

public BigDecimal add(另一个BigDecimal对 象) 加法

public BigDecimal subtract (另一个 BigDecimal对象) 减法

public BigDecimal multiply (另一个 BigDecimal对象) 乘法

public BigDecimal divide (另一个BigDecimal 对象) 除法

public BigDecimal divide (另一个BigDecimal 对象,精确几位,舍入模式) 除法

Object类

🔷Object类概述

Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该 类,换句话说,该类所具备的方法,所有类都会有一份

重写toString方法

toString方法的作用: 以良好的格式,更方便的展示对象中的属性值

哈希值(我说实话没听太懂但感觉问题不大)直接上代码

2public class Demo2 extends Object{3 public static void main(String[] args) {4 //哈希值5 int[] arr = {1,2,3};System.out.println(arr); //[I@1540e19d [=数组 I=int数组 @=分隔符 1540e19d= 哈希值678 String[] arr1 = {"1","2","3"};9 System.out.println(arr1); //[Ljava.lang.String;@677327b61011 //哈希值在Object里面12 //toHexString()=转换十六进制13 /*14 * public String toString() {15 return getClass().getName() + "@" + Integer.toHexString(hashCode());16 }1718 //没有实现方法 native:本地方法 java底层用c写的19 public native int hashCode();20 */2122 //任何对象都有对应的哈希值23 int code = "hello".hashCode();24 System.out.println("code = " + code);25 // 十六进制(简写为hex或下标16)在数学中是一种逢16进1的进位制。26 // 一般用数字0到9和字母A到F(或a~f)表示,Object类是Java类层次结构中的根类,每个类都继承了Object类,当然每一个对象也都可以调用Object类中的方法,其中equals方法是一个非常重要的方法,因此进行详细介绍:1、在Object类中,equals方法比较的是两个对象的地址值,地址值相同返回true,否则返回false27 // 其中:A~F表示10~15,这些称作十六进制数字。28 //转换十六进制29 String hexString = Integer.toHexString(code);30 System.out.println("hexString = " + hexString);3132 int th = "通话".hashCode();33 System.out.println("th = " + th);34 String s1 = Integer.toHexString(th);35 System.out.println("s1 = " + s1);3637 int zd = "重地".hashCode();38 System.out.println("zd = " + zd);39 String s2 = Integer.toHexString(zd);40 System.out.println("s2 = " + s2);41 }42 }

🔷Object类的equals方法

equals方法的作用 用于对象之间的比较,返回true和false的结果 举例:s1.equals(s2); s1和s2是两个对象

重写equals方法的场景 不希望比较对象的地址值,想要结合对象属性进行比较的时候。

重写equals方法的方式 i. alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可 i. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。

Object类中原本的equals方法比较的都是地址值

@Override30 //传入的对象默认为Object类型,此处涉及到多态31 public boolean equals(Object o) {32 //若参与比较的两个对象是同一个对象(地址值相同),直接返回true,只是为了提高代码执行效率33 if (this == o) return true;//若传入的参数o是null或者不是person类型,直接返回false,此处使用了反射技术来判断o是否是person类型3435 if (o == null || getClass() != o.getClass()) return false;36 //使用向下转型,将Object类型的o转化为person类型的o。这是因为在传入时使用了多态,37 //多态不能使用子类特有的属性和方法38 Student student = (Student) o;39 //若两个对象的name和age都相同,返回true,否则返回false40 return age == student.age &&41 Objects.equals(name, student.name);42 }4344 /*@Override45 public int hashCode() {46 return Objects.hash(name, age);47 }*/48 }

基本数据类型和包装类的转换

通过包装类Integer.toString()将整型转换为字符串;

通过Integer.parseInt()将字符串转换为int类型;

通过valueOf()方法把字符串转换为包装类然后通过自动拆箱。

包装类知识

包装类对象的初始值为null(是一个对象);

基本类型包装类

基本类型包装类的作用

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换

基本类型对应的包装类

byte=Byte short=Short int=Integer long=Long float=Floatdouble=Double char=Character boolean=Boolean

装箱:基本数据类型转换为包装类; 拆箱:包装类转换为基本数据类型。

public class Demo3 {2 public static void main(String[] args) {3 //创建包装类4 //1 构造方式 ,可以存入数字,字符串5 Integer integer1 = new Integer(10);6 Integer integer2 = new Integer("10");78 //2.Integer的静态方法9 Integer integer3 = Integer.valueOf(100);10 Integer integer4 = Integer.valueOf("100");1112 System.out.println(integer1);13 System.out.println(integer2);14 System.out.println(integer3);15 System.out.println(integer4);16 }17 }

int和String类型的相互转换(记忆)

int转换为String 转换方式

方式一:直接在数字后加一个空字符串

方式二:通过String类静态方法valueOf()

String转换为int 转换方式

方式一:先将字符串数字转成Integer,再调用valueOf()方法

方式二:通过Integer静态方法parseInt()进行转换

public class IntegerDemo {2 public static void main(String[] args) {3 //String --- int4 String s = "100";5 //方式1:String --- Integer --- int6 Integer i = Integer.valueOf(s);7 //public int intValue()8 int x = i.intValue();9 System.out.println(x);10 //方式211 //public static int parseInt(String s)12 int y = Integer.parseInt(s);13 System.out.println(y);14 }15 }

Arrays工具类

🔷Arrays的常用方法

public static String toString(int[] a) 返回指定数组的内容的字符串表示形式

public static void sort(int[] a) 按照数字顺序排列指定的数组

public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引

public class MyArraysDemo {2 public static void main(String[] args) {3 // 返回指定数组的内容的字符串表示形式4 // int [] arr = {3,2,4,6,7};5 // System.out.println(Arrays.toString(arr));67 // 按照数字顺序排列指定的数组8 // int [] arr = {3,2,4,6,7};9 // Arrays.sort(arr);10 // System.out.println(Arrays.toString(arr));11// public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引1213 int [] arr = {1,2,3,4,5,6,7,8,9,10};14 int index = Arrays.binarySearch(arr, 10);15 System.out.println(index);16 //1,数组必须有序17 //2.如果要查找的元素存在,那么返回的是这个元素实际的索引18 //3.如果要查找的元素不存在,那么返回的是 (-插入点 负数)19 //插入点:如果这个元素在数组中,他应该在哪个索引上.20 }21 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。