Java commonly used basic classes

1. Packaging

definition

Basic data types such as int, float, double, boolean, char, etc. Basic data types do not have the characteristics of objects. For example, basic types cannot call methods and have simple functions. In order to make the basic data types have the characteristics of objects, Java provides a wrapper class for each basic data type, so that we can manipulate the basic data types like operating objects.
Insert picture description here

Second, the Math class

Java's Math contains properties and methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and trigonometric functions.
Math methods are all defined as static forms, which can be directly called in the main function through the Math class.

public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("90度的正弦值:"+Math.sin(Math.PI/2));
        System.out.println("0度的余弦值:"+Math.cos(0));
        System.out.println("60的正切值:"+Math.tan(Math.PI/3));
        System.out.println("1的反正切值:"+Math.atan(1));
        System.out.println("π/2的角度值"+Math.toDegrees(Math.PI/2));
        System.out.println(Math.PI);
        }
 }

The result is
Insert picture description here


Three, String class

The String class has 11 construction methods, these methods provide different parameters to initialize the string, such as providing a character array parameter

package work;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        String result = "qwertyuiop";
        //通过字符的下标,返回字符
        System.out.println(result.charAt(3));
        //拼接
        System.out.println(result.concat("scsc"));
        //找字符下标
        System.out.println(result.indexOf("t"));
        //字符串长度
        System.out.println(result.length());
        //字符串替换
        System.out.println(result.replace('w','c'));
        //字符串截取(把前面的截取了)
        System.out.println(result.substring(5));
        //从前面到后面截取
        System.out.println(result.substring(0,5));
        //大写
        System.out.println(result.toUpperCase());
        //小写
        System.out.println(result.toLowerCase());
    }
}


The result is:
Insert picture description here

Fourth, the StringBuffer class

Insert picture description here

String question = new String("2*5=");
        int answer=10;
        boolean result=(2*5==10);
        StringBuffer sb = new StringBuffer() ;
        sb. append (answer);
        sb. append (question);
        sb. append (result);
        sb. insert (5,',');
        
        System. out. println(sb) ;

Insert picture description here


Five, Date class

The java.util package provides the Date class to encapsulate the current date and time.

The Date class provides two constructors to instantiate Date objects. The first constructor uses the current date and time to initialize the object.

Example: Get the current date and time

package work;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test3 {
    
    
    public static void main(String[] args) {
    
    
        //基本输出时间
        System.out.println(new Date());
        Calendar c = Calendar.getInstance();
        //常用输出时间 --年--月--日 时:分:秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        System.out.println(sdf.format(new Date()));
    }
}

The result is
Insert picture description here

Six, the Calendar class to build a calendar


//get 和 set 的字段数字,指示一个月中的某天
int dayOfMonth = Calendar.DAY_OF_MONTH;
System.out.println(dayOfMonth);

//get 和 set 的字段数字,指示一个星期中的某天。
int dayOfWeek = Calendar.DAY_OF_WEEK;
System.out.println(dayOfWeek);
//get 和 set 的字段数字,指示当前月中的第几个星期。
int dayOfWeekInMonth = Calendar.DAY_OF_WEEK_IN_MONTH;
System.out.println(dayOfWeekInMonth);

The result isInsert picture description here


Seven, Random class

Randomly generated number, the generated result is a random number within 10

package work;

import java.util.Random;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
     //1. 创建键盘录入数据的对象
        Random r = new Random();
        //2随机生成一个数据
            int number = r.nextInt(10);
            //3. 输出数据
            System.out.println("number:"+ number);

    }

}

The result is:
Insert picture description here

Guess you like

Origin blog.csdn.net/s001125/article/details/110192409