枚举指由一组固定的常量组成的类型
特点:
类型安全
易于输入
代码清晰
常用JavaAPI查找相关知识

包装类把基本类型数据转换为对象
每个基本类型在java.lang包中都有一个相应的包装类
包装类的作用
提供了一系列实用的方法 集合不允许存放基本数据类型数据,存放数字时,要用包装类型
所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
注意事项
Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
XXXValue():包装类转换成基本类型
toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
valueOf() 所有包装类都有如下方法(基本类型->包装类)public static Type valueOf(type value)
valueOf():除Character类外,其他包装类都有如下方法(字符串->包装类)public static Type valueOf(String s)
- package demo01;
-
- public class Demo01 {
-
- public static void main(String[] args) {
- //包装类常用方法:XXXValue():包装类转换成基本类型
- Byte byte1 = new Byte("12");
- byte num1=byte1.byteValue();
- System.out.println(num1);
-
- Boolean bool = new Boolean("True");
- boolean num2 =bool.booleanValue();
- System.out.println(num2);
-
- Character ch = new Character('a');
- char num3 =ch.charValue();
- System.out.println(num3);
-
- //toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
- byte num4 = 10;
- String str1 = Byte.toString(num4);
- System.out.println(str1);
-
- String str2 = Boolean.toString(false);
- System.out.println(str2);
-
- //parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
- int num5 = Integer.parseInt("30");
- System.out.println(num5);
-
- boolean num6 =Boolean.parseBoolean("qwe");
- System.out.println(num6);
-
- //valueOf():所有包装类都有如下方法(基本类型->包装类)public static Type valueOf(type value)
- Integer int1 = Integer.valueOf(100);
- System.out.println(int1);
- //valueOf():除Character类外,其他包装类都有如下方法(字符串->包装类)public static Type valueOf(String s)
- Long long1 =Long.valueOf("200");
- System.out.println(long1);
-
-
-
- }
- }
装箱和拆箱:实现的是基本数据类型和包装类类型之间的自动转换
装箱:基本数据类型直接赋值给包装类类型的对象
拆箱:包装类类型数据直接赋值给基本类型的变量
- package demo01;
-
- public class Demo02 {
-
- public static void main(String[] args) {
-
-
- byte num1 =10;
- //Byte byte1 =num1;
-
- //装箱:基本数据类型直接赋值给包装类类型的对象
- Byte byte1 = num1;
-
- //拆箱:包装类类型数据直接赋值给基本类型的变量
- Integer int1 = new Integer("123");
- int num2 =int1;
-
- Integer int2 =new Integer("255");
- int num3 =300;
- int result =int2+num3;
- Integer int3 =int2 +num3;
- }
-
- }
包装类的特点
JDK1.5后,允许基本数据类型和包装类型进行混合数学运算
包装类并不是用来取代基本数据类型的
在基本数据类型需要用对象表示时使用
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
生成随机数的其他方式
- package demo02;
-
- public class MathDemo01 {
-
- public static void main(String[] args) {
- //Math类:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
- double pi =Math.PI;
- System.out.println(pi);
-
- System.out.println(Math.max(100, 200));
- System.out.println(Math.min(99, 199));
- System.out.println(Math.abs(-10));
-
- System.out.println(Math.floor(9.8));//返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
- System.out.println(Math.ceil(9.1));//返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
- System.out.println(Math.round(10.5));//返回最接近参数的 int。
-
- //随机数方法:Math.random():随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
- double num1 = Math.random();
- System.out.println(num1);
-
- /*随机返回一个[num1,num2)之间的int类型的整数(num2>num1)
- * int num = (int)(Math.random()*(num2-num1)+num1);
- */
- }
-
- }
String类位于java.lang包中,具有丰富的方法
eg:计算字符串的长度、比较字符串、连接字符串、提取字符串
length()方法,确定字符串的长度
总结:
获取数组长度:数组名.lenght
获取集合长度:集合名.size()
获取字符串长度:字符串对象名.length()
equals( )方法,比较存储在两个字符串对象的内容是否一致
字符串比较的其他方法
使用equalsIgnoreCase()方法
使用toLowerCase()方法
使用toUpperCase()方法
- package demo03;
-
- public class StringDemo01 {
-
- public static void main(String[] args) {
- String string1 = "qwertyuiop";
-
- //获取字符串的长度
- int length = string1.length();
- System.out.println("字符串长度:"+length);
-
-
- //比较两个字符串的内容是否相同
- String str1 = "qwe";
- String str2 = "qwe";
- System.out.println(str1.equals(str2));//true
- System.out.println(str1==str2);//true
-
- String str3 = new String("abc");
- String str4 = new String("abc");
- System.out.println(str3.equals(str4));//true
- System.out.println(str3==str4);//false
-
- //其他比较字符串的方法
- String str5 ="qwert";
- String str6 = "QWERT";
- System.out.println(str5.equals(str6));
- //equalsIgnoreCase()不区分大小写比较
- System.out.println(str5.equalsIgnoreCase(str6));//true
-
- String result = str6.toLowerCase();
- System.out.println(str6);//QWERT //字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品进行了修改
- System.out.println(result);//qwert
-
- System.out.println(str5.toUpperCase());
-
-
- }
-
- }
方法1:使用“+”
方法2:使用String类的concat()方法
- package demo03;
-
- public class StringDemo02 {
-
- public static void main(String[] args) {
-
- System.out.println("qwert");
-
- int num = 10;
- System.out.println(num);
- //+是一个连接符,连接变量与字符串的值
- System.out.println("num里面存储的值是:"+num);
-
- System.out.println("我的班级是:"+"Java2217");//我的班机是:Java2217
-
- System.out.println(10+20+"good");//30good
- System.out.println(10+"good"+20);//10good20
- System.out.println("good"+10+20);//good1020
-
- String str1 ="qwert";
- String str2 ="yuiop";
- String result = str1.concat(str2);
- System.out.println(str1);//qwert
- System.out.println(str2);//yuiop
- System.out.println(result);//qwertyuiop
- }
-
- }
public int indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
public String substring(int index)提取从位置索引开始的字符串到末尾部分
public String substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
public String trim()返回一个前后不含任何空格的调用字符串的副本
- package demo03;
-
- import java.util.Arrays;
-
- public class StringDemo03 {
-
- public static void main(String[] args) {
-
- //字符串常用提取方法
- String str1 ="abcdefghijklmnabcd";
- System.out.println(str1.length());//14
-
- //public int indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
- //public int indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
- int index = str1.indexOf(110);
- System.out.println(index);
- System.out.println(str1.indexOf("bc"));
-
- //public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
- //public int lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
- System.out.println(str1.lastIndexOf(97));
- System.out.println(str1.lastIndexOf("bcd"));
-
- //public String substring(int index)提取从位置索引开始的字符串到末尾部分
- System.out.println(str1);
- System.out.println(str1.substring(3));
-
- //public String substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
-
- System.out.println(str1);
- System.out.println(str1.substring(3, 6));
-
- //public String trim()返回一个前后不含任何空格的调用字符串的副本
- String str2 =" qwert yuiop ";
- System.out.println(str2);
- System.out.println(str2.trim());
-
- //字符串拆分方法
- String str3 ="长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
- String[] strs =str3.split(" ");
- for (String string : strs) {
- System.out.println(string);
- }
-
- String str4 ="我爱你但是你不爱我可是我依然爱你";
- String[] strs2 =str4.split("爱");
- System.out.println(Arrays.toString(strs2));
-
- //判断字符串是否以指定内容结尾
- String str5 ="HelloWorld.java";
- System.out.println(str5.endsWith(".java"));
- //判断字符串是否以执行内容开头
- System.out.println(str5.startsWith("He"));
-
- //将字符串变成byte类型的数组
- byte[] bytes =str5.getBytes();
- for (byte b : bytes) {
- System.out.println((char)b);
-
- }
-
-
- }
-
- }
对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率
String是不可变对象
经常改变内容的字符串最好不要使用String
StringBuffer是可变的字符串
字符串经常改变的情况可使用StringBuffer,更高效
JDK5.0后提供了StringBuilder,等价StringBuffer
- package demo04;
-
- public class StringBufferDemo01 {
-
- public static void main(String[] args) {
- String str1 ="qwertyuiop";
- StringBuffer sb1 = new StringBuffer("qwert");
-
- //String类对象调用方法之后,对原来String对象中的内容没有影响
- System.out.println(str1);
- System.out.println(str1.substring(2));
- System.out.println(str1);
-
- System.out.println("-------------------");
- //StringBuffer类对象调用方法返回结果还是Stringuffer的,这时候会改变StringBuffer对象里的内容
- System.out.println(sb1);
- sb1.substring(2);
- System.out.println(sb1.insert(2, "qwe"));
- System.out.println(sb1);
-
- System.out.println(sb1.toString());
-
- //在末尾追加内容
- System.out.println(sb1);
- System.out.println(sb1.append("asdfghjkl"));
- System.out.println(sb1);
-
-
-
- }
-
- }

- package demo05;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class DataDemo01 {
-
- public static void main(String[] args) {
- //创建Date类对象
- Date date = new Date();
- System.out.println(date);
- //Date类中很多方法都已经过时,使用Calender类替代
- // System.out.println(date.getYear()+1900);
- // System.out.println(date.getMonth()+1);
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = sdf.format(date);
- System.out.println(str);
- }
-
- }
抽象类,java.util.Calendar
Calendar是一个抽象类,不能直接实例化,可以通过该类中一个静态方法创建其引用
用于设置和获取日期/时间数据
- package demo06;
-
- import java.util.Calendar;
-
- public class CalendarDemo01 {
-
- public static void main(String[] args) {
- Calendar cal =Calendar.getInstance();
- //获取年
- int year = cal.get(Calendar.YEAR);
- System.out.println(year);
- //获取月
- int month = cal.get(Calendar.MONTH);
- System.out.println(month);
- //获取日
- int date = cal.get(Calendar.DATE);
- System.out.println(date);
-
- //获取时、分、秒
- System.out.println(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
-
- //获取星期
- int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
- System.out.println(day_of_week);
-
- //获取当前日期是这一年中的第多少天
- System.out.println(cal.get(Calendar.DAY_OF_YEAR));
- }
-
- }