~ 5 JavaSE Review: basic data types

type of data

java, there are two types of data: basic data types and reference data types

Basic data types

  • Integer: byte, short, int, long
  • Float: float, double
  • Character: char
  • Boolean: boolean
type of data Digit Defaults Ranges for example
byte (bit) 8 0 -2^7 - 2^7-1 byte b = 10;
short (short integer) 16 0 -2^15 - 2^15-1 short s = 10;
int (integer) 32 0 -2^31 - 2^31-1 int i = 10;
long (long) 64 0 -2^63 - 2^63-1 long l = 10l;
float (single-precision) 32 0.0 -2^31 - 2^31-1 float f = 10.0f;
double (double precision) 64 0.0 -2^63 - 2^63-1 double d = 10.0d;
char (character) 16 air 0 - 2^16-1 char c = 'c';
boolean (Boolean value) 8 false true、false boolean b = true;

Precautions

  • String is not a basic type, but reference types
  • Float may only be an approximation, not a precise value
  • It represents the range does not necessarily correlate with the number of bytes, such as long float ratio represents a wide range of float but is 4 bytes, 8 bytes long
  • The default floating-point type is double, the default integer type is int
  • If you give long variable assignment, constants should be suffixed Lorl
  • If you give floag variable assignment, constant suffix should be added Forf

Data type conversion

When the data is not the same type, data type conversion will occur

Automatic conversion (implicit)

  1. Features: code need not be specified, automatically
  2. Rules: data range from small to large

note:

  • char, byte, short, and other types in the calculation will be converted to type int first, and then performing the calculation
  • boolean type Data type conversion can not occur

Cast (manual)

grammar:(要转换的目标类型) 要转换的数据

  1. How to convert your own life needs
  2. There will be data loss or precision

Guess you like

Origin www.cnblogs.com/wbyixx/p/11846288.html