1.API 1.1 API概述
1.2 键盘录入字符串 1 2 3 4 5 Scanner类 : next() nextLine()
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.itheima.api;import java.util.Scanner;public class Demo2Scanner { public static void main (String[] args) { Scanner sc = new Scanner (System.in); System.out.println("请输入整数:" ); int num = sc.nextInt(); System.out.println("请输入字符串:" ); String s = sc.nextLine(); System.out.println(num); System.out.println(s); } }
2.String类 2.1 String概述 1 String 类在 java.lang 包下,所以使用的时候不需要导包
2 String 类代表字符串,Java 程序中的所有字符串文字(例如“abc”)都被实现为此类的实例也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象
3 字符串不可变,它们的值在创建后不能被更改
2.2 String类的构造方法 常用的构造方法:
1 2 3 4 5 6 7 public String () public String (char [] chs) public String (String original) String s = "abc"
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.itheima.string;public class Demo2StringConstructor { public static void main (String[] args) { String s1 = new String (); System.out.println(s1); char [] chs = {'a' ,'b' ,'c' }; String s2 = new String (chs); System.out.println(s2); String s3 = new String ("123" ); System.out.println(s3); } }
2.3 创建字符串对象的区别对比
2.4 字符串的比较 2.4.1 字符串的比较
== 比较基本数据类型:比较的是具体的值
== 比较引用数据类型:比较的是对象地址值
String类 : public boolean equals(String s)
比较两个字符串内容是否相同、区分大小写
代码 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.itheima.stringmethod;public class Demo1Equals { public static void main (String[] args) { String s1 = "abc" ; String s2 = "ABC" ; String s3 = "abc" ; System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s2)); } }
2.5 String常用方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public boolean equals (Object anObject) public boolean equalsIgnoreCase (String anotherString) public int length () public char charAt (int index) public char [] toCharArray() public String substring (int beginIndex, int endIndex) public String substring (int beginIndex) public String replace (CharSequence target, CharSequence replacement) public String[] split(String regex)
3.StringBuilder类 3.1 StringBuilder类概述 StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器,这里的可变指的是 StringBuilder 对象中的内容是可变的
3.2 StringBuilder类和String类的区别
String类:内容是不可变 的
StringBuilder类:内容是可变 的
3.3 StringBuilder类的构造方法 常用的构造方法:
1 2 3 public StringBuilder () public StringBuilder (String str)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 public class StringBuilderDemo01 { public static void main (String[] args) { StringBuilder sb = new StringBuilder (); System.out.println("sb:" + sb); System.out.println("sb.length():" + sb.length()); StringBuilder sb2 = new StringBuilder ("hello" ); System.out.println("sb2:" + sb2); System.out.println("sb2.length():" + sb2.length()); } }
3.4 StringBuilder常用的成员方法
1 2 3 public StringBuilder append (任意类型) public StringBuilder reverse ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class StringBuilderDemo01 { public static void main (String[] args) { StringBuilder sb = new StringBuilder (); StringBuilder sb2 = sb.append("hello" ); System.out.println("sb:" + sb); System.out.println("sb2:" + sb2); System.out.println(sb == sb2); sb.append("hello" ); sb.append("world" ); sb.append("java" ); sb.append(100 ); sb.append("hello" ).append("world" ).append("java" ).append(100 ); System.out.println("sb:" + sb); sb.reverse(); System.out.println("sb:" + sb); } }
3.5StringBuilder和String相互转换 StringBuilder————》String:
1 public String toString ()
String————》StringBuilder:
1 public StringBuilder (String s)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class StringBuilderDemo02 { public static void main (String[] args) { StringBuilder sb1 = new StringBuilder (); sb1.append("hello" ); String s1 = sb1.toString(); System.out.println(s1); String s2 = "hello" ; StringBuilder sb2 = new StringBuilder (s2); System.out.println(sb2); } }
4.Math类 1、Math类概述:包含执行基本数字运算的方法
2、Math中方法的调用方式:Math类中无构造方法,但内部的方法都是静态的,可以通过 类名.方法名
进行调用
3、Math类的常用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static int abs (int a) public static double ceil (double a) public static double floor (double a) public static int round (float 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) public static double random ()
5.System类 System类的常用方法
1 2 3 4 5 public static void exit (int status) public static long currentTimeMillis () public static void arraycopy (数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)
6.Object类 Object 是所有类的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份。
6.1 toString方法 为了能以良好的格式,更方便的展示对象中的属性值,通常会在类中重写方法,把展示地址变为展示属性。
原始方法:
重写后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 class Student extends Object { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } @Override public String toString () { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}' ; } } public class ObjectDemo { public static void main (String[] args) { Student s = new Student (); s.setName("林青霞" ); s.setAge(30 ); System.out.println(s); System.out.println(s.toString()); } }
运行结果:
1 2 Student{name='林青霞' , age=30 } Student{name='林青霞' , age=30 }
6.2 equals方法 重写equals方法的场景:不希望比较对象的地址值,想要结合对象属性进行比较的时候
原始方法:
1 2 3 public boolean equals (Object obj) { return (this == obj); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } @Override public boolean equals (Object o) { if (this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Student student = (Student) o; if (age != student.age) return false ; return name != null ? name.equals(student.name) : student.name == null ; } } public class ObjectDemo { public static void main (String[] args) { Student s1 = new Student (); s1.setName("林青霞" ); s1.setAge(30 ); Student s2 = new Student (); s2.setName("林青霞" ); s2.setAge(30 ); System.out.println(s1.equals(s2)); } }
面试题
1 2 3 4 5 6 7 8 9 10 11 12 public class InterviewTest { public static void main (String[] args) { String s1 = "abc" ; StringBuilder sb = new StringBuilder ("abc" ); System.out.println(s1.equals(sb)); System.out.println(sb.equals(s1)); } }
6.3 其他方法 1 2 3 4 5 6 7 public static String toString (对象) public static String toString (对象, 默认字符串) public static Boolean isNull (对象) public static Boolean nonNull (对象)
7.BigDecimal类 作用:可以用来进行精确计算
构造方法
1 2 3 BigDecimal(double val) BigDecimal(String val)
常用方法
1 2 3 4 5 6 7 8 9 10 11 12 public BigDecimal add (另一个BigDecimal对象) public BigDecimal subtract (另一个BigDecimal对象) public BigDecimal multiply (另一个BigDecimal对象) public BigDecimal divide (另一个BigDecimal对象) public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)
8.包装类 8.1 基本类型包装类 基本类型包装类的作用:将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本类型对应的包装类:
基本数据类型
包装类
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
8.2 自动拆箱和自动装箱 自动装箱:把基本数据类型转换为对应的包装类类型
自动拆箱:把包装类类型转换为对应的基本数据类型
示例代码:
1 2 Integer i = 100 ; i += 200 ;
8.3 Integer类 Integer类概述:包装一个对象中的原始类型 int 的值
Integer类构造方法:
1 2 3 4 5 6 7 public Integer (int value) public Integer (String s) public static Integer valueOf (int i) public static Integer valueOf (String s)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class IntegerDemo { public static void main (String[] args) { Integer i1 = new Integer (100 ); System.out.println(i1); Integer i2 = new Integer ("100" ); System.out.println(i2); System.out.println("--------" ); Integer i3 = Integer.valueOf(100 ); System.out.println(i3); Integer i4 = Integer.valueOf("100" ); System.out.println(i4); } }
8.4int和String类型的相互转换 8.4.1 int转换为String 转换方式:
方式一:直接在数字后加一个空字符串
方式二:通过String类静态方法valueOf()
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class IntegerDemo { public static void main (String[] args) { int number = 100 ; String s1 = number + "" ; System.out.println(s1); String s2 = String.valueOf(number); System.out.println(s2); } }
8.4.2 String转换为int 转换方式:
方式一:先将字符串数字转成Integer,再调用valueOf()方法
方式二:通过Integer静态方法parseInt()进行转换
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class IntegerDemo { public static void main (String[] args) { String s = "100" ; Integer i = Integer.valueOf(s); int x = i.intValue(); System.out.println(x); int y = Integer.parseInt(s); System.out.println(y); } }
9.Arrays类 常用方法:
1 2 3 4 5 public static String toString (int [] a) public static void sort (int [] a) public static int binarySearch (int [] a, int key)
10.时间日期类 10.1 Date类 计算机中时间原点:1970年1月1日 00:00:00
时间换算单位:1秒 = 1000毫秒
Date类概述:Date 代表了一个特定的时间,精确到毫秒
10.1.1 Date类构造方法 1 2 public Date () public Date (long date)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class DateDemo01 { public static void main (String[] args) { Date d1 = new Date (); System.out.println(d1); long date = 1000 *60 *60 ; Date d2 = new Date (date); System.out.println(d2); } }
10.1.2 常用方法 1 2 3 public long getTime () public void setTime (long time)
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class DateDemo02 { public static void main (String[] args) { Date d = new Date (); System.out.println(d.getTime()); System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年" ); long time = System.currentTimeMillis(); d.setTime(time); System.out.println(d); } }
概述:SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
构造方法:
1 2 3 public SimpleDateFormat () public SimpleDateFormat (String pattern)
常用方法:
1 2 3 4 5 public final String format (Date date) public Date parse (String source)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class SimpleDateFormatDemo { public static void main (String[] args) throws ParseException { Date d = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss" ); String s = sdf.format(d); System.out.println(s); String ss = "2048-08-09 11:11:11" ; SimpleDateFormat sdf2 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ); Date dd = sdf2.parse(ss); System.out.println(dd); } }
10.3 JDK8时间日期类
LocalDate 表示日期(年月日)
LocalTime 表示时间(时分秒)
LocalDateTime 表示时间+ 日期 (年月日时分秒)
10.3.1 LocalDateTime创建方法 方法说明:
1 2 3 public static LocalDateTime now () public static LocalDateTime of (年,月,日,时,分,秒)
示例代码:
1 2 3 4 5 6 7 8 9 public class JDK8DateDemo2 { public static void main (String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println(now); LocalDateTime localDateTime = LocalDateTime.of(2020 , 11 , 11 , 11 , 11 , 11 ); System.out.println(localDateTime); } }
10.3.2 LocalDateTime获取方法 方法说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 public int getYear () public int getMonthValue () public int getDayofMonth () public int getDayofYeara () public DayofWeek getDayofWeek () public int getMinute () public int getHour ()
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class JDK8DateDemo3 { public static void main (String[] args) { LocalDateTime localDateTime = LocalDateTime.of(2020 , 11 , 11 , 11 , 11 , 20 ); int year = localDateTime.getYear(); System.out.println("年为" +year); int month = localDateTime.getMonthValue(); System.out.println("月份为" + month); Month month1 = localDateTime.getMonth(); System.out.println(month1); int day = localDateTime.getDayOfMonth(); System.out.println("日期为" + day); int dayOfYear = localDateTime.getDayOfYear(); System.out.println("这是一年中的第" + dayOfYear + "天" ); DayOfWeek dayOfWeek = localDateTime.getDayOfWeek(); System.out.println("星期为" + dayOfWeek); int minute = localDateTime.getMinute(); System.out.println("分钟为" + minute); int hour = localDateTime.getHour(); System.out.println("小时为" + hour); } }
10.3.3 LocalDateTime转换方法 方法说明:
1 2 3 public LocalDate toLocalDate () public LocalTime toLocalTime ()
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class JDK8DateDemo4 { public static void main (String[] args) { LocalDateTime localDateTime = LocalDateTime.of(2020 , 12 , 12 , 8 , 10 , 12 ); LocalDate localDate = localDateTime.toLocalDate(); System.out.println(localDate); LocalTime localTime = localDateTime.toLocalTime(); System.out.println(localTime); } }
10.3.4 LocalDateTime格式化和解析 方法说明:
1 2 3 4 5 public String format (指定格式) public LocalDateTime parse (准备解析的字符串, 解析格式) public static DateTimeFormatter ofPattern (String pattern)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class JDK8DateDemo5 { public static void main (String[] args) { } private static void method1 () { LocalDateTime localDateTime = LocalDateTime.of(2020 , 11 , 12 , 13 , 14 , 15 ); System.out.println(localDateTime); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss" ); String s = localDateTime.format(pattern); System.out.println(s); } private static void method2 () { String s = "2020年11月12日 13:14:15" ; DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss" ); LocalDateTime parse = LocalDateTime.parse(s, pattern); System.out.println(parse); } }
10.3.5 LocalDateTime增加或者减少时间的方法 1 2 3 4 5 6 7 8 9 10 11 12 13 public LocalDateTime plusYears (long years) public LocalDateTime plusMonths (long months) public LocalDateTime plusDays (long days) public LocalDateTime plusHours (long hours) public LocalDateTime plusMinutes (long minutes) public LocalDateTime plusSeconds (long seconds) public LocalDateTime plusWeeks (long weeks)
10.3.6 LocalDateTime减少或者增加时间的方法 1 2 3 4 5 6 7 8 9 10 11 12 13 public LocalDateTime minusYears (long years) public LocalDateTime minusMonths (long months) public LocalDateTime minusDays (long days) public LocalDateTime minusHours (long hours) public LocalDateTime minusMinutes (long minutes) public LocalDateTime minusSeconds (long seconds) public LocalDateTime minusWeeks (long weeks)
10.3.7 LocalDateTime修改方法 1 2 3 4 5 6 7 8 9 10 11 12 13 public LocalDateTime withYear (int year) public LocalDateTime withMonth (int month) public LocalDateTime withDayOfMonth (int dayofmonth) public LocalDateTime withDayOfYear (int dayOfYear) public LocalDateTime withHour (int hour) public LocalDateTime withMinute (int minute) public LocalDateTime withSecond (int second)
10.4 Period类 方法说明:
1 2 3 4 5 6 7 8 9 public static Period between (开始时间,结束时间) public int getYears () public int getMonths () public int getDays () public long toTotalMonths ()
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class JDK8DateDemo9 { public static void main (String[] args) { LocalDate localDate1 = LocalDate.of(2020 , 1 , 1 ); LocalDate localDate2 = LocalDate.of(2048 , 12 , 12 ); Period period = Period.between(localDate1, localDate2); System.out.println(period); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); System.out.println(period.toTotalMonths()); } }
10.5 Duration类 方法说明:
1 2 3 4 5 6 7 public static Durationbetween (开始时间,结束时间) public long toSeconds () public int toMillis () public int toNanos ()
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class JDK8DateDemo10 { public static void main (String[] args) { LocalDateTime localDateTime1 = LocalDateTime.of(2020 , 1 , 1 , 13 , 14 , 15 ); LocalDateTime localDateTime2 = LocalDateTime.of(2020 , 1 , 2 , 11 , 12 , 13 ); Duration duration = Duration.between(localDateTime1, localDateTime2); System.out.println(duration); System.out.println(duration.toSeconds()); System.out.println(duration.toMillis()); System.out.println(duration.toNanos()); } }
Author:
不久之
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY ?