Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to determine the type of result when Java characters are converted into integers?
How to determine the type of result when Java characters are converted into integers?
1, type conversion mainly occurs in assignment, method call and arithmetic operation. A, assignment and method call conversion rules: automatic conversion from low-order types to high-order types; Type conversion is required from high-order types to low-order types: (1) Boolean types and other basic data types cannot be converted to each other; ? (2) byte types can be converted into short, int, long, float and double;; ? (3)short can be converted into int, long, float and double;; ? (4)char can be converted into int, long, float and double;; ? (5)int can be converted into long, float and double;; ? (6)long can be converted into float and double;; ? (7)float can be converted into double;;

In addition, there is the assignment of direct sign: first, judge its type by direct sign, and then the basic principle is basically the same as the assignment principle mentioned above; Only when the direct number is an integer is it special. When it is in the representable range, it can be directly assigned to three types of byte short characters.

B, type conversion in arithmetic operation: 1 basically, it is converted into high-order data type first, and then participates in the operation, and the result is also high-order data type; 2-byte short character operations will be converted to Int;;

(1) If one of the operands is double, the other operand is converted to double first, and then participates in the arithmetic operation.

(2) If neither operand is a double, when one operand is a float, the other operand is converted to a float first, and then participates in the operation.

(3) If neither operand is a double or a float, if one operand is a long, then the other operand is converted into a long first, and then participates in the arithmetic operation.

(4) If the two operands are not double, float or long, first convert the two operands into int, and then participate in the operation.

Special:

(1) If abbreviation operators such as+= and * = are used, the system will automatically convert the operation result to the type of the target variable.

(2) ? When the operator is an automatic increment operator (++) or an automatic decrement operator (-), if the operand is byte, the type of short or char will not change;

1, the basic types and corresponding packaging classes can be automatically converted, which is the principle of automatic packaging folding;

Integer c 1 = new integer (1); Integer C2 = 2;; Int cc = new integer (3); //in addition: conversion between String and basic type Int I = integer. parse int(" 13 "); int ii = integer . value of(" 12 "); string s 1 = string . value of( 123); string S2 = integer . tostring(I); String S3 = ""+i;

2. Conversion between two reference types:

1, subclasses can be directly converted into parent classes or interface types;

2. When the parent class is converted into a subclass, type conversion should be forced; And if it is not actually the corresponding object at runtime, it will throw a ClassCastException runtime exception;