Java learning notes - operators, loops, Number & Math class methods

Java operator

Conditional operator (?:)

variable x = (expression) ? value1 if true : value2 if false

Simply put, if expression is true, the value of x is assigned value1, otherwise it is assigned value2

instanceof operator
This operator is used to manipulate an object instance to check whether the object is a specific type (class type or interface type).

( Object reference variable ) instanceof  (class/interface type)

If the object referred to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true.

Note: if the object being compared is compatible with the type on the right, the operator still returns true.

Java loop

Java enhanced for loop

for(Declaration statement : expression)
{
   //Code sentence
}

Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the array element at this time.

Expression: an expression is the name of the array to be accessed, or a method whose return value is an array. (in short, array)

continue keyword
continue applies to any loop control structure. The function is to make the program jump to the iteration of the next cycle immediately.

Number & math class method

Common methods and descriptions

xxxvalue()
Convert the Number object to the value of xxx data type and return it.
Usage: returns the specified value in the form of xxx. Variable xxxValue()

compareTo()
The compareTo() method is used to compare the Number object with the parameters of the method. It can be used to compare Byte, Long, Integer, etc.
This method is used for the comparison of two same data types. Two different types of data cannot be compared with this method.

public int compareTo( NumberSubClass referenceName )

variable.compareTo(value)

Returns 0 if the specified number is equal to the parameter.

If the specified number is less than the parameter, - 1 is returned.

Returns 1 if the specified number is greater than the parameter.

valueOf()
The valueOf() method is used to return the value of the native Number object of a given parameter. The parameter can be a native data type, String, etc.

Simply put, it is a function used to convert other data types to number
Usage:

static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)

give an example:

Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");              
Integer b = Integer.valueOf("444",16);   // Use hexadecimal

result:

x = 9
c = 5.0
a = 80.0
b = 1092

toString()
The toString() method is used to return the value of the Number object represented by a string.

If the method uses the native data type as the parameter, it returns the String object value of the native data type.
If the method has two parameters, the string representation of the first parameter represented by the specified cardinality of the second parameter is returned.

toString() is the opposite of valueOf(). toString() converts the number object into a string
Usage:

String toString()
static String toString(int i)

x.toString()

parseInt()
The parseInt() method is used to parse string parameters as signed decimal integers.
If the method has two parameters, use the cardinality specified by the second parameter to parse the string parameter into a signed integer.

The usage is similar to valueOf, but it can only be parsed as int

static int parseInt(String s)
static int parseInt(String s, int radix)
Example:
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);
9
5.0
1092

abs(): used to return the absolute value of the number
ceil(): round up
floor(): round down
rint(): returns the integer closest to the parameter. The return type is double. (the return value of 100.5 is 100, not rounded)
round(): round

The algorithm is math Floor (x + 0.5), that is, add 0.5 to the original number and then round it down, so, math round(11.5)
The result is 12, math The result of round (- 11.5) is - 11.

min(): returns the minimum of the two parameters.
max(): returns the maximum of the two parameters.
exp(): used to return the parameter power of base e of natural number,.

System.out.printf("e The value of is %.4f%n", Math.E);
System.out.printf("exp(%.3f) by %.3f%n", x, Math.exp(x));  

e The value of is 2.7183
exp(11.635) 112983.831(Namely e^11.635)

log(): returns the logarithm of the natural base of the parameter.
pow(): returns the power of the second parameter of the first parameter.
double pow(double base, double exponent)
Continuous multiplication function
sqrt(): find the arithmetic square root of the parameter.

sin(): find the sine value of the specified double type parameter.
**asin() * * find the arcsine value of the specified double type parameter.
In short, a is the inverse triangle, which is the abbreviation of arc

toDegrees(): convert parameters to angles.

double x = 45.0;
double y = 30.0;
System.out.println( Math.toDegrees(x) );
System.out.println( Math.toDegrees(y) );
2578.3100780887044
1718.8733853924698

toRadians(): converts an angle to radians.

double x = 45.0;
double y = 30.0;
System.out.println( Math.toRadians(x) );
System.out.println( Math.toRadians(y) );
0.7853981633974483
0.5235987755982988

random(): returns a random number. (what you see is what you get)

Keywords: Java

Added by ckk on Sat, 19 Feb 2022 23:44:50 +0200