Java foundation --day03

Expression is evaluated and operator precedence

Evaluation of Java expressions and mathematical expression evaluates the same

  • First count of brackets brackets
  • Multiplication and division to take over first count
  • Then count addition and subtraction
  • At the same level calculated from left to right

topic:华氏度转换为摄氏度

import java.util.Scanner;
public class FahrenheitToCelsius {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入华氏度:");
        double f = input.nextDouble();
        double c = (5.0 / 9) * (f - 32);
        System.out.println("摄氏度为:" + c);
    }
}

Output:

输入华氏度:100
摄氏度为:37.77777777777778

Note: Results 5/9 to 0, it should be 5.0 / 9

Displays the current time

By calling System.currentTimeMillis()Returns the current time

Sysytem.currentTimeMillisIt returns the number of milliseconds since the UNIX timestamp

Snipaste_2020-02-08_15-05-42

Code:

public class ShowCurrentTime {
    public static void main(String[] args) {
        long totalMilliseconds = System.currentTimeMillis(); //总毫秒数
        long totalSeconds = totalMilliseconds / 1000;        //总秒数
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        long currentHour = totalHours % 24;

        System.out.println("当前GMT时间为" + currentHour + ":" +
                currentMinute + ":" + currentSecond);
    }
}

Check next time: GMT

Note that the data type should be:long

Enhanced assignment operator

Operators name
+= Addition assignment operator
-= Subtraction assignment operator
*= Multiplication assignment operator
/= Division assignment operator
%= Remainder assignment operator

Such as:

\ [x / = 4 + 5.5 * 1.5 \] is equivalent to \ [x = x / (4 + 5.5 * 1.5) \]

And increment and decrement

Operators name Explanation
++ was Preincrement operator Plus the value of var 1 and using the new increased value var
was ++ Post-increment operator Increments the value of the variable var original 1 but using the values ​​var
--where Front and decrement Save the value of the variable var 1 and using the new increased value var
where-- Rear and decrement Var, but using 1 minus the value of the original value var

Conversion Value Type

By explicit conversion, an integer floating point numbers can be converted

3 * 4.5 => 3.0 * 4.5

You can always assign a number to support a larger range of data types of variables, such as: can longassign values to type floatvariable type

But, without the type of conversion , it can not be assigned to a range of smaller types of variable values.

Widening Type: converting a small type of variable type for a wide range of variables

Narrowing Type: converting a wide range of types of variables as the type of small-scale variable

Java will automatically expand a type, however, narrowed type must be explicitly completed.

Explicit type conversion:

System.out.println((double)1 / 2);   // 结果为0.5
System.out.println((int)1.7);        // 结果为1

note:

  1. Type conversion does not change the variable to be converted
double b = 4.5;
int i = (int)b; // i = 4; b 仍然为 4.5
  1. In Java, \ (OP = x1 X2 \) in the form of enhanced assignment expression, the implementation is \ (x1 = (T) (x1 OP X2) \) , where T is the type of x1. Such as:
int sum = 0;
sum += 4.5;  //sum = 4
// sum += 4.5 等价于 sum = (int)(sum + 4.5)
  1. A intvariable assigned to the shorttype or a bytevariable must be explicit conversion.
int i = 1;
byte b = i;  // 编译错误
byte c = (byte)i; // 正确

However, as long integer literals are allowed within the target range, then the amount of the direct assign integer shorttype or a bytetime variable, there is no need to display the type of conversion.

Write by Gqq

Guess you like

Origin www.cnblogs.com/zgqcn/p/12529454.html