JavaSE Basics (a)

Foreword

  JavaSE is the basics of Java developers must be familiar with, and I'm here usually relatively easy to overlook some of the points recorded for later review.

A data type conversion  

  Java data types can be divided into two categories, basic data types and reference data types. The basic data types are divided into four eight basic types, including integer, floating point, character, Boolean; major class reference data types, arrays, and other interfaces.

  Four kinds of eight kinds of data types

 

1.1 automatic conversion

  When the data type java assignment operator, automatically a small range of data types to lift large range of data types (e.g., byte, short, char when operation is automatically promoted to int).

  It is worth mentioning that, b3 = 1 + 2 sentence, compiles without error, as we said byte, short, char when the operation will be automatically promoted to int, at this time we do not coerce, why not being given it? The reason is that 1 and 2 are constants at compile time it has been determined that they will not exceed the sum of byte ranges, the compiler will

They result of the addition into byte type. And b4 = b1 + b2, the compiler will be given, because the two variables are b1 and b2, the compiler can not determine their values ​​at compile time, their values ​​will be added as int type of treatment, with byte received naturally the error.

  + =, - =, * =, / = compound assignment operators which, when evaluated, will implicitly int down into byte, short, char and other types, b4 + = 1 statements without error.

1 public static void main(String[] args){
2   byte b1=1;
3   byte b2=2;
4   byte b3=1 + 2; 
5   byte b4=b1 + b2;
6   b4+=1;
7   System.out.println(b3);
8   System.out.println(b4); 
9 }

 

1.2 cast

  When the value of the large ranges of data types assigned to the range of small data type of the variable must be cast to convert the value of large ranges of data types into smaller ranges of data types, or the compiler It will complain, and the conversion process will cause some loss of precision.

  

  

 

    

Guess you like

Origin www.cnblogs.com/linfuxian/p/11846854.html