一.枚举
Enum,特殊的数据类型,即是一个类又有比普通类多一点约束
简洁、安全、方便等特点,有一个特定的范围
常量用finall形容
public enum 枚举名{
}
Values()方法可以返回枚举的所有常量
valuesof()可以通过字符串创建对应的枚举对象
二.Math
数学方法,都是静态常量,常量E和PI
public class math {
public static void main(String[] args) {
System.out.println("常量E"+Math.E);
System.out.println("常量PI"+Math.PI);
System.out.println("9的平方根"+Math.sqrt(9));
System.out.println("8的立方根"+Math.cbrt(8));
System.out.println("2的3次平方"+Math.pow(2,3));
System.out.println(Math.max(6.5,3.5));
System.out.println(Math.min(5.5,9.7));
System.out.println(Math.abs(-8.9));
System.out.println(Math.ceil(10.0003));
System.out.println(Math.floor(10.99999));
System.out.println(Math.random());
System.out.println(Math.round(5.6));
System.out.println(Math.round(6.6f));
System.out.println(Math.rint(6.4));
}
}
三.Random
import java.util.Random;
public class random {
public static void main(String[] args) {
Random random = new Random();
for (int i = 1; i <= 3; i++) {
boolean flag = random.nextBoolean();
System.out.println(flag);
}
for (int i = 1; i <=3; i++) {
double num = random.nextDouble();
System.out.println(num);
}
for (int i = 1; i <= 3; i++) {
float num = random.nextFloat();
System.out.println(num);
}
for (int i = 1; i <=3; i++) {
int num = random.nextInt();
System.out.println(num);
}
}
}
四.String
String实例化有两种方式
1.直接赋值
2.利用构造函数创建实例化对象
public class stringTest {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2);//true
String str3 = new String("World");
String str4 = new String("World");
System.out.println(str3 == str4);//false
}
}
直接赋值的方法:首先会在字符串常量池中开辟一个新的空间,然后再将该内存地址给到栈中,如果第二次再用到相同的字符串则不会再重新开辟新的空间,直接把已有的内存地址赋值给栈,所以==比较会是true
构造函数的方法:new几个String就会开辟几个内存空间,会把不同的内存地址赋给栈,所以==比较会是false
String类对于继承Object类的equals方法进行重写;
在判断两个字符串是否相等时,会直接将字符串转为byte类的数组,然后依次判断字符串的每个值是否相等
public class Test1 {
public static void main(String[] args) {
String str = "Hello";
String str1 = str;
System.out.println(str.equals(str1));
}
}
String常用方法
import java.util.Arrays;
public class stringUse {
public static void main(String[] args) {
char[] array = {'j','a','v','a','H','e','l','l','o'};
String str = new String(array);
System.out.println(str);
System.out.println(str.length());
System.out.println(str.isEmpty());
//下标为2的字符
System.out.println(str.charAt(2));
System.out.println(str.indexOf("H"));
String str1 = "Hello";
System.out.println(str.equals(str1));
String str2 = "HELLO";
//忽略大小写是否相等
System.out.println(str1.equalsIgnoreCase(str2));
System.out.println(str.startsWith("java"));
System.out.println(str.endsWith("java"));
//从2开始截取
System.out.println(str.substring(2));
System.out.println(str.substring(2,6));
System.out.println(str.replaceAll("Hello","javaME"));
System.out.println(Arrays.toString(str.split(",")));
//将字符串转为char类型的数组
System.out.println(Arrays.toString(str.toCharArray()));
System.out.println(str2.toLowerCase());
System.out.println(str1.toUpperCase());
}
}
五.StringBuffer
底层是用一个数组来存储字符串的值,空的StringBuffer默认长度为16
调用有参构造是“值的长度+16”
如果修改范围超过16,则调用底层方法进行扩容
public class stringBuff {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
System.out.println(stringBuffer);
System.out.println(stringBuffer.length());
stringBuffer = new StringBuffer("Hello");
System.out.println(stringBuffer);
//打印下标为2的字符
System.out.println(stringBuffer.charAt(2));
stringBuffer = stringBuffer.append("java");
System.out.println(stringBuffer);
stringBuffer = stringBuffer.delete(3,6);
System.out.println(stringBuffer);
stringBuffer = stringBuffer.deleteCharAt(3);
System.out.println(stringBuffer);
stringBuffer = stringBuffer.replace(2,3,"stringBuffer");
System.out.println(stringBuffer);
String str = stringBuffer.substring(2);
System.out.println(str);
str = stringBuffer.substring(2,8);
System.out.println(str);
stringBuffer = stringBuffer.insert(6,"six");
System.out.println(stringBuffer);
System.out.println(stringBuffer.indexOf("e"));
System.out.println(stringBuffer.indexOf("e",6));
stringBuffer = stringBuffer.reverse();
System.out.println(stringBuffer);
str = stringBuffer.toString();
System.out.println(str);
}
}
六.日期
Java.util.Date 和 Java.util.Calendar
使用java.text.SimpleDateFormate类对日期进行格式化,SimpleDateFormate提供模板标记
import java.text.SimpleDateFormat;
import java.util.Date;
public class date {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS");
String str = simpleDateFormat.format(date);
System.out.println(str);
}
}
七.Calender
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarTest {
public static void main(String[] args) {
//计算2018年8月6日所在周是一年中的第几周
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2018);
calendar.set(Calendar.MONTH,7);
calendar.set(Calendar.DAY_OF_MONTH,6);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println(week);
calendar.set(Calendar.YEAR,2018);
calendar.set(Calendar.MONTH,7);
calendar.set(Calendar.DAY_OF_MONTH,6);
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-21);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String str1 = simpleDateFormat.format(calendar.getTime());
System.out.println(str1);
}
}