Java language programming (four) type conversion and escape characters

   In Java, if we encounter two operands of different types, we can also perform binary operations. For example, if one of the operands is an integer and the other operand is a floating-point number, Java will automatically convert the integer to a floating-point value. For example, 3*3.5 is converted to 3.0*3.5. 

      In Java, you can assign a value to a variable that supports a larger range of values. For example, you can assign a long value to a float variable. But without type conversion, you cannot assign a value to a variable with a smaller range. Type conversion is an operation that converts the value of one type of data into another type of data. The widening type does not require explicit conversion, and the conversion can be performed automatically, and the narrowing type must be done explicitly.

      The syntax of type conversion requires the target type to be placed in parentheses, followed by the name or value of the variable to be converted, for example:

      System.out.prinrln((int)1.7); The display result is 1. When the double type value is converted to int type, the decimal part is truncated.

      System.out.println((double)1/4); shows the result as 0.25, because 1 is first converted to 1.0, and then 1.0 is divided by 4.

      System.out.println(1/2); The display result is 0, because 1 and 2 are integers, so the result of dividing them must also be an integer.

      If a value is assigned to a variable of a smaller type, type conversion must be performed. If type conversion is not used, a compilation error will occur. Note that type conversion does not change the variable being converted, for example:

      double a=2.5;

      int i=(int)a;

      a becomes 2, but the value of a remains the same, still 2.5

      Let's take an example from the book and take a look. We need to give a procedure for keeping the turnover tax to two decimal places:

import java.util.Scanner;

/**
 *
 * @author mjd
 */
public class SalesTax {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter purchase amount:");
        double purchase = input.nextDouble();
        double tax = purchase*0.06;
        System.out.println("SalesTax is"+(int)(tax*100)/100.0);
        
    }
    
}

image

      The character data type char is used to represent a single character, and the character type is enclosed in single quotes.

      char letter = 'A';

      char numChar = '3';

      The first statement assigns the character A to the char variable letter. The second statement assigns the numeric character 4 to the char variable numChar

      Escape character

      If we want to print quoted information when outputting, we need to use escape characters. Next, we will list some commonly used escape characters.

      \b Backspace key \t Tab key \n Line feed symbol \r Enter key \f Paper feed \\ Backslash

      \'Single quote \" Double quote

      System.out.println("He said\"Java is fun\"");

      Its output is He said "ava is fun"


Guess you like

Origin blog.51cto.com/15064656/2602784