Java Basics (a) basic data types

Basic data types introduced

Java basic data types There are eight species, divided into two categories: boolean types and value types, and numeric types can be divided into integer types, character types, floating-point type.

Integer

  • byte: 1 byte, range: (- 2 ^ 7) to (2 ^ 7-1)
  • Short: 2 bytes, in the range: (- 15 ^ 2) - (2 ^ 15-1)
  • int: 4 bytes, in the range: (- 31 ^ 2) - (2 ^ 31-1)
  • long: 8 bytes, in the range: (- 63 ^ 2) - (2 ^ 63-1)

int is the most common type, usually directly gives a default integer value that is of type int, for example:

int a = 128;   //100会被系统默认当成int类型,然后赋予变量a

There are two special cases must be pointed out:

  • A smaller integer value (within the range of type byte or short) is assigned a variable byte or short, the system will be automatically converted to an int type byte, or short process.
  • The integer a large (beyond the scope of type int) confer long type, it will not be converted to a long handle type, it must be added after the integer value l or as a suffix L.
byte a = 56;   //代码正确,系统自动把56转换byte类型

long bigValue1 = 99999999999999999;  //代码错误,整数值超过int类型范围
long bigValue2 = 99999999999999999L;  //代码正确,在整数值后添加L,强制使用long类型

Note: an integer value (within the range of type int) assigned long type, the compiler can, it is not a special case, this is an automated type conversion.

long bigValue3 = 99;  //99是int类型,赋值时,自动类型转换为long类型

Integer representation:

  • Decimal: normal use value
  • Binary: begin with 0b or 0B
  • Octal: beginning with 0
  • Hex: with 0x or 0X, wherein 10 ~ 15 a ~ f or A ~ F are represented
//十进制212的各种表示方式
int binVal = 0b11010100;  //二进制
int octalVal = 0324;   //八进制
int hexVal = 0xd4;  //十六进制

Note: binary and hexadecimal form of the performance of the integer store (complement) in memory, the default value is 32-bit binary integer, the most significant bit (bit 32) is a sign bit.

int binVal = 0B10000000000000000000000000000011;
System.out.println(binVal);  //-2147483645,最高符号位为第32位,为负数

byte binVal2 = (byte)0b11101001;  //整数值默认为int,最高符号为第32位,为正数
System.out.println(binVal2);  //-23,强制转换后,最高符号位为第8位,为负数

long binVal3 = 0B10000000000000000000000000000011L; //二进制后缀加L,代表long类型
System.out.println(binVal3);  //2147483651,最高符号位为第64位,为正数

Character

char type is a 16-bit Unicode character encoding, a 16-bit unsigned integer in nature, can be 0 ~ (2 ^ 16-1) int integer in a range of directly assigned char type variable, the system will automatically be converted to type int char type.

Representation of the character values:

  • Individual character, 'A', '9', etc.
  • Escape character, '\ n', '\ t', etc.
  • Unicode value using the format '\ uXXXX', where XXXX is a hexadecimal integer in the range '\ u0000' ~ '\ uFFFF', wherein the first 256 ( '\ u0000' ~ '\ u00FF') characters and ASCII character codes coincide completely.
char aChar = 'a';       //单个字符
char enterChar = '\r';  //转义字符
char ch = '\u9999';     //Unicode编码
char c = 97;            //给char类型变量赋值int整数值
int zhongValue = '疯';  //char类型自动转换为int

Float

  • float: 4-byte (32-bit), bit 1 is the sign bit, the next 8 to identify the index, then the next 23 is a mantissa
  • double: 8 bytes (64 bits), bit 1 is the sign bit, the next 11 to identify the index, then the next 52 mantissa

Floating-point representation:

  • Decimal: normal use value, for example 3.14. It must contain a decimal point, or else will be treated as an int
  • Scientific notation: e.g. 3.14e2 or 3.14E2 (i.e., 3.14 * 10 ^ 2)

double is the most common type, usually, the default value is given directly to a floating-point type double, the type of treatment, if desired as a float, floating point values ​​after adding f or F.

double a = 3.14;  //代码正确,3.14被系统默认当成double类型,然后赋值给a
float b = 3.14;   //代码错误,3.14默认为double类型
float c = 3.14f;  //代码正确,3.14f为float类型

Special floating point values:

  • Positive infinity: Double Float class or classes of POSITIVE_INFINITY, a positive number divided by 0
  • Negative infinity: Double Float class or classes of NEGATIVE_INFINITY, negative divided by 0
  • Not a Number: Double Float class or classes of NaN, 0.0 or 0.0 divided by the square root of a negative number
double a = 3.14;
System.out.println(a/0);  //Infinity

int b = 4;
System.out.println(b/0);   //报错

Boolean

Only the boolean true and false two types, can not be used in place of zero or non-zero, the value can not be converted into other types of boolean types, which are different, and C ++. Java does not dictate the type of boolean share memory space, usually eight.

boolean b1 = true;
boolean b2 = false;

System.out.println(b1 + "-" + b2);  //true-false

Basic types of casts

7 kinds of numeric type aside outer Java boolean types are interchangeable, the conversion in two ways: automatic conversion and casts.

Automatic type conversion

When a small number of tables or variable value range assigned directly to a large number of other variables table range, the system performs automatic type conversion, without cast. One can imagine there are two bottles of water, when the small bottle of water poured into a large bottle will not have any problems.

Figure above, the array type can be automatically converted left of the arrow to the right of the array type.

byte c = 2;
//byte类型自动转换为double
double d = c;
System.out.println(d);  //2.0

Cast

To a small value or range number table directly assigned to the variable range of a large number of variables another table, you need to cast. The conversion process may be data loss, it is conceivable to put a large bottle of water into a small bottle, a large bottle of water if not much better, but if a large bottle of water a lot, will cause an overflow, resulting in data lost.

int iValue = 233;
//将int类型强制转换为byte类型
byte bValue = (byte) iValue;
System.out.println(bValue);  //-23

Expression type automatic upgrade

When the value contained in an arithmetic expression of a plurality of basic types, the entire data type arithmetic expression will occur automatically upgrade:

  • All type byte, short, and char type type will be promoted to an int;
  • Arithmetic expression result type is automatically elevated to the highest level of expression operands of the same type.
short sValue = 5;
//错误,sValue是short类型,在表达式中自动提升到int类型,数值2默认是int类型,
//sValue-2整个表达式结果类型为int,不能赋给short类型
sValue = sValue - 2;
short value = 3;
//正确,value被自动提升到int类型,23也是int类型,整个表达式结果为int类型
int initResult = 23/value;

Guess you like

Origin www.cnblogs.com/zongmin/p/11329893.html