目录
Math类
三角函数
指数函数
取整方法
其他方法
String类
常见方法
字符串比较方法
子串和数字与字符串的转换
Math类
Math类在java.lang中,不用显式引入。
三角函数
private static void triangleFunc() {
double degree = Math.toDegrees(Math.PI / 3);
System.out.println("Math.PI / 3 = " + degree);
double radians = Math.toRadians(180);
System.out.println("degree 180 = " + radians);
double sin = Math.sin(radians/2);
System.out.println("sin(pi) = " + sin);
double cos = Math.cos(radians);
System.out.println("cos(pi) = " + cos);
double tan = Math.tan(radians/4);
System.out.println("tan(pi) = " + tan);
}
输出结果:
指数函数
private static void triangleFunc() {
double degree = Math.toDegrees(Math.PI / 3);
System.out.println("Math.PI / 3 = " + degree);
double radians = Math.toRadians(180);
System.out.println("degree 180 = " + radians);
double sin = Math.sin(radians/2);
System.out.println("sin(pi) = " + sin);
double cos = Math.cos(radians);
System.out.println("cos(pi) = " + cos);
double tan = Math.tan(radians/4);
System.out.println("tan(pi) = " + tan);
}
输出结果:
取整方法
private static void intFunc() {
System.out.println("向上取整 0.2 = " + Math.ceil(0.2));
System.out.println("向下取整0.8 = " + Math.floor(0.8));
System.out.println("四舍五入 0.6 = " + Math.rint(0.6));
System.out.println("四舍五入 0.5 = " + Math.round(0.5));
}
输出结果:
其他方法
private static void otherFunc() {
System.out.println("较小的数 = " + Math.min(3,4));
System.out.println("较大的数 = " + Math.max(3,4));
System.out.println("相反的数 = " + Math.abs(3));
System.out.println("随机数 = " + Math.random());
}
输出结果:
String类
String是一个引用类型,不是如int, char和foat的基础类型,是一个预定义的类。
常见方法
private static void normalFunc() {
String message = " Welcome to Java! ";
System.out.println("The length of message = " + message.length());
System.out.println("The 5th char = " + message.charAt(5));
System.out.println(message.concat("Come on."));
System.out.println("Upper all char:" + message.toUpperCase());
System.out.println("Lower all char: " + message.toLowerCase());
System.out.println(message.trim());
}
输出结果:
字符串比较方法
private static void compareFunc() {
String s = "Hello";
System.out.println(s.equals("Hello"));
System.out.println(s.equalsIgnoreCase("hello"));
System.out.println(s.compareTo("Hell"));
System.out.println(s.compareToIgnoreCase("he"));
System.out.println(s.startsWith("He"));
System.out.println(s.endsWith("lo"));
System.out.println(s.contains("ell"));
}
输出结果:
子串和数字与字符串的转换
private static void otherFunc() {
String what = "summer";
System.out.println(what.substring(3));
System.out.println(what.substring(3, 5));
String float_num = "3.14";
String int_num = "314";
System.out.println(Integer.parseInt(int_num));
System.out.println(Double.parseDouble(float_num));
}
输出结果: