java的基本类型

 

The numeric types are the integral types and the floating-point types.

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).

The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.

The boolean type has exactly two values: true and false.


整数的运算:

1.比较运算,产生的结果为booelan类型的值。

2.数值运算,产生的结果为int或者long类型。这一点要特别注意。

3.条件运算:?:

4.类型转换:

5.String的+运算:


理解整数的“扩展”和“窄化”。注意,尽管事实上精度损失可能会发生,但是基本类型之间的扩展转换永远不会导致运行期异常。

隐形类型转换不需要显性的的强制转换晕算符,而是自动发生的;它仅仅发生在扩展转换的情况下。为了方便,当变量是byte,short或char类型且表达式的值一定为int型时,窄化转换也可以是隐形的。当然这个表达式的值不能超过变量类型的数值范围。

比如 byte b1=127;是正确的,而byte b2=128;却是错误的。


隐形类型转换能够发生在3种情况下:赋值,方法调用和算术运算。


异常情况:

The built-in integer operators do not indicate overflow or underflow in any way. Integer operators can throw a NullPointerException if unboxing conversion 5.1.8) of a null reference is required. Other than that, the only integer operators that can throw an exception (§11) are the integer divide operator /15.17.2) and the integer remainder operator %15.17.3), which throw an ArithmeticException if the right-hand operand is zero, and the increment and decrement operators ++15.15.1, §15.15.2) and --15.14.3, §15.14.2), which can throw an OutOfMemoryError if boxing conversion (§5.1.7) is required and there is not sufficient memory available to perform the conversion.


参考:

java语言规范

Java Pitfalls

你可能感兴趣的:(java)