Chapter 4: Mathematical functions, characters, strings
1. Commonly used mathematical functions
1. Trigonometric functions
method | description |
---|---|
sin(radians) | Radian -> sine function value |
cos(radians) | Radian -> cosine function value |
tan(radians) | Radian -> Tangent function value |
toRadians(degree) | Angle -> Radian |
toDegrees(radians) | Radian -> Angle |
asin(a) | Numerical value -> arc sine function value |
acos(a) | Numerical value -> arc cosine function value |
atan (a) | Value -> Arctangent function value |
double sin=Math.sin(Math.PI/2);//1.0
//cos、tan同理
double radians = Math.toRadians(30);//0.5235987755982988
double degree=Math.toDegrees(Math.PI/4);//45.0
double radians=Math.asin(1.0);//1.5707963267948966
//acos、atan同理
2. Exponential function
method | description |
---|---|
log(x) | Logarithm to base e |
exp(x) | e raised to the power of x |
log10(x) | Base 10 logarithm |
pow(a,b) | a to the b power |
sqrt(x) | square root of x (x>=0) |
double x = Math.log(Math.pow(Math.E, 2));//2.0 其中Math.E为e
double y=Math.exp(2);//7.38905609893065
double z=Math.log10(1000);//3.0
double s=Math.sqrt(4);//2.0
2. Service method
1. Rounding method
method | description |
---|---|
ceil(x) | Improvement arrangement |
floor(x) | Round down |
rint (x) | x is rounded to its nearest integer. If the distance between x and two integers is equal, the even integer is returned as a double precision value |
round(x) | If x is a single precision number, return (int)(Math.floor(x+0.5); if x is double precision, return (long)(Math.floor(x+0.5) |
Math.ceil(3.1);//4.0
Math.ceil(-3.1);//-3.0
Math.floor(3.9);//3.0
Math.floor(-3.1);//-4.0
Math.rint(3.1);//3.0
Math.rint(3.5);//4.0
Math.rint(2.5);//2.0
Math.rint(-2.5);//-2.0
Math.rint(-3.5);//-3.0
Math.round(-2.6)//-3 long型
Math.round(2.4)//2 long型
Math.round(2.4f)//2 int型
Math.round(-2.4f)//-2 int型
[ Personal summary ]
- The ceil and floor methods can be imagined as a one-dimensional axis
- When using the rint method, when the distance between x and two integers is equal, odd numbers are carried, even numbers are not carried, and negative numbers are the same.
- When using the round method, single precision returns int type, double precision returns long type, both add 0.5 and then round down.
2.min, max, abs method
Service data type: int, long, float, double
return minimum, maximum, absolute value
3. Random method The
random method was explained in the previous chapter, so I won't go into details here.
Three. Character data types and operations
1. Unicode yard
- 16 bits, two bytes
- The 4-digit hexadecimal number at the beginning of \u
- Range: \u0000-\uFFFF
character | Decimal | Unicode |
---|---|---|
‘0’-‘9’ | 48-57 | \u0030-\u0039 |
‘A’-‘Z’ | 65-90 | \u0041-\u005A |
‘a’-‘z’ | 97-122 | \u0061-\u007A |
2. Increment and decrement can be used on char characters
char ch='b';
++ch;//'c'
char ch2='b';
--ch2;//'a'
int x=ch-ch2;//2
3. Common conversion
- Floating point type -> char type
char ch=(char)65.9;//'A'
//转换过程:先将65.9转换为int型:65,在转换为char型'A'
- Any hexadecimal number from 0 to FFFF can be implicitly converted to char type, if it is not in this range, the conversion needs to be displayed
char ch=0x0041;//'A'
- If the other operator is a character or a number, it is automatically converted to a number, if it is a string, it is connected
int i='2'+'3';
System.out.println("i is "+i);//i is 101
4. Escape sequence of special characters
- \b: backspace key
- \f: form feed
- \r: Enter key
5. Commonly used methods of characters
Since the methods in the Character class are static methods, there is no need to create an instance to call. Static methods are recommended to be called directly by the class name.
Character.isDigit('5');//true 是否为数字
Character.isLetter('5');//false 是否为字母
isLetterOrDigit(ch);// 数字或字母
isLowerCase(ch);// 小写字母
isUpperCase(ch);// 大写字母
toLowerCase('A');//'a' 转为小写
toUpperCase('a');//'A'
Four. String type
1. The String type is not a basic data type, but a reference type
String message="Java Is Fun";
//message引用了一个内容为Java Is Fun的字符串对象
Because the methods in String can only be called by a specific string instance, they are also called instance methods, and non-instance methods are called static methods.
2. Simple method of String object
String str="welcome";
str.length();//7 字符串长度
str.charAt(3);//c 返回指定下标的字符,下标从0开始
str.concat("hello")//welcomehello 连接字符串,也可以使用+连接
str.toUppercase();//WELCOME 返回全大写
str.toLowerCase();//welcome 返回全小写
String str2="welcome to ".trim();
System.out.println(str.length());//10 去掉两边空格
[ Note ] Java allows the use of strings to directly quote strings without creating new variables.
"Welcome to java".length();//15
"".length()//0
3. Read the string from the console
[ Note ] The difference between next() method and nextLine() method is that next() method ends with a blank ('','\t','\f','\r','\n'); nextLine() The method reads a whole line of text, and ends with the Enter key.
There is no special way to get characters from the console, only the following methods:
String x=in.nextLine();//读取一条字符串
char ch=x.charAt(0);//取第一个字符
4. Comparison method of String objects
String str="welcome";
String str2="welcome";
String str3="hello";
str.equals(str2);//true 字符串内容是否相等,==比较的是是否指向同一个对象
str.equalsIgnoreCase(str2);//true 不区分大小写比较
str.compareTo(str3);//15 比较第一个不相同的字母,返回一个大于0,=0,<0的整数,此例中w比h大15,所以返回正15
str.compareToIgnoreCase(str2);//15 比较不区分大小写,比较规则与上例中相同
str.startsWith("wo");//false 是否以wo开头
str.endsWith("me");//true 是否以me结尾
str.contains(str2);//true 是否包含str2子串
5. Get the substring
String str="welcome to java";
str.substring(3);//come to java 从开始下标到结束
str.substring(3,6);//com 从开始下标到结束下标-1,注意:不包含结束下标
str.indexOf('c');//3 返回字符或字符串下标
str.indexOf('a',4)//12 从下标4之后第一个出现的字符或字符串
str.lastIndexOf('a');//14 最后一个出现的字符或字符串下标
str.lastIndexOf('a',13);//12 从下标13之前出现的最后一个'a'
[ Note ] indexOf (str, fromindex) from after fromindex find
lastIndexOf (str, fromindex) from before fromindex Find
6. Conversion between string and number
String str="123";
int x=Integer.parseInt(str);//123 转为int类型
double y=Double.parseDouble(str);//123 转为double型
……
String str2=123+"welcome";//123welcome
Five. Formatted output
1. Syntax: printf(formate,item1,item2……)
2. Format identifier
Format identifier | description |
---|---|
%b | Boolean value |
%c | character |
%d | Decimal |
%f | Floating point |
%e | Standard scientific notation |
%s | String |
System.out.printf("%5c",'c');// c c前有四个空格
System.out.printf("%6b",true);// true true前加两个空格
System.out.printf("%5.3f",3.1415926);//3.142 保留三位小数,并共占5位的空间
[ Note ]
- If an item requires more space than the specified width, the width is automatically increased
- Right-aligned by default, you can add a negative sign to achieve left-aligned
[ Supplement ] 2020-04-26
Add a new method to String in jdk1.8: String.join()
Syntax: String.join("separator", "string array/collection")
//获取所有bean对象如下:
for(String beanObject:ac.getBeanDefinitionNames()){
//获取每个beanObject的别名
String[] beanName=ac.getAliases(beanObject);
//打印每个bean对象名称和他的别名 String.join()方法:将字符串数组用特定字符进行连接
//String.join("特定字符","字符串数组")
System.out.println(String.format("beanObject:%s,别名:[%s]",beanObject,String.join(",",beanName)));
//beanObject:helloWord,别名:[helloWorld_2,helloWord_1,he]
}
Seven. Summary
Through the study of Chapter 4, I have learned the commonly used mathematical functions and service methods. The rounding method in the service method may be a bit convoluted. Please pay more attention to my friends. I also learned how to deal with characters and strings, and format The output is very helpful for future exercises. In short, the harvest is great, and I hope my friends are too! The first four chapters are the foundation of the foundation. In the next chapter, we will come into contact with the cycle, the introduction to the foundation, keep up with the pace!
Come on! Chapter 5 To Be More...