Conversion between the java type conversion between basic data types and the basic data types and string

A . Type conversion between the basic data types:

1. The value of low precision can be directly assigned to the variable high-precision, high accuracy becomes between

   Accuracy is generally believed that low byte <short <char <int <long <float <double

float f=12.5f;
long m=4;
f=m;

2. The precision values ​​can not be directly assigned to the variable low-precision (unless cast)

a float F = 12.5f ;
 Long m =. 4 ; 
m = F; // error, since the value of high accuracy can not be directly assigned to the low-accuracy variable

Cast, method: = target type variable (type target) value;

float f=12.5f;
long m=4;
m
=(long) f;

Note: The cast may lose precision.

a float F = 12.5f ;
 long m =. 4 ; 
m = ( long ) F; 
System.out.println ( m); // print the results to the console: 12 as long as a long integer, no decimal point, the rounded decimal go with.

3. The results obtained after different types of variable mixing operation type is the highest precision

byte B1 = 123 ;
 char C1 = 'L' ;
 int IL = 12 is ;
 a float Z = B1 + C1 + IL + 12.6f; // If the last 12.6 f without double type that he is being given phrase, because the highest precision type double, can not be assigned directly to the float type, or the type of turn unless strong plus f

Minimum experience: how to distinguish when editing a post-decimal type float or double type of look at the figures is not followed by a f, there was no type float was double.

Two . Converting between strings and basic data types:

1. The basic data types into a string

   Basic data type into a string may be utilized to provide the type String valueOf function method of the following format:

   String.valueOf (the basic types);

int Age = 25 ;
 a float Money = 452.2f ; 
String AGE1 = String.valueOf (Age); 
String MONEY1 = String.valueOf (Money); 
System.out.println ( "Age value is" + AGE1); 
the System.out .println ( "money value is" + MONEY1);

2. The basic data types string into

   String into the basic data types, typically by "base type package type" in, int wrapper class is Byte, Short, Integer, Long, Float category is floating point package and Double, the package type is character Character, Boolean wrapper class It is Bolean, provided they are of type String to convert the basic function of the package type corresponding to the class. It lists a few common situations:

1. The string into an int:

 The Integer.parseInt ( "string");

2. The string into float type:

Float.parseFloat as ( "string");

3. string to double Type:

Double.parseDouble ( "string");

 

 

 Content from school textbooks Java programming and application development and their own understanding

Guess you like

Origin www.cnblogs.com/cuizhufeng/p/11797514.html