java 99% 是对象处理的,还有1%是primitive(int,short,char...等字面值)的处理,与之相对的wrapper class 是Integer,Short,Character...等,因此,总需要要在primitive和wrapper之间不断的转换...
Tiger来使用boxing和unboxing来解决这个问题...自动执行这些转换...即autoboxing和auto-unboxing..
1.primitive to wrapper
引用
//Tiger之前定义个Integer必须如下
Integer i = new Integer(100);
//Tiger只需这样
Integer i = 100;
int j = 100;
Integer x = j;
还有,JVM将特定primitive都给box成相同的不可变的wrapper对象(ture,false -128——127,部分char),放在缓存...高效率。
看下面两个例子..
引用
Integer counter1 = 100;
Integer counter2 = 100;
System.out.print("counter1 == counter2: " );
System.out.println(counter1 == counter2);
Integer counter3 = 256;
Integer counter4 = 256;
System.out.print("counter3 == counter4: ");
System.out.println(counter3 == counter4);
结果输出:
引用
counter1 == counter2: true
counter3 == counter4: false
Why?
因为counter1 和counter2 的值在-128——127之间,是不可变的wrapper,所以会将Integer boxing 成int类型,然后进行比较,所以是相等的。
因为counter3 和counter4 的值不在-128——127之间,没有将Integer进行boxing因此比较的还是对象(两个对象是在不同的内存空间),所以不相等的。
2.wrapper to primitive
引用
Integer j = 100;
int i = j;//自动将wrapper转换为primitive
但是如果是这样..
引用
Integer j = null;
int i = j;//将抛出NullPointerExcepion异常,所以建议初始换wrapper对象的值.
3.wrapper的递增与递减
wrapper对象也可以递增与递减,如
引用
Integer counter = 1;
counter++;
--counter;
//先把Integer先unboxing成int,递增或递减,然后再boxing回去
4.Boolean and boolean
通过Boolean 也可以直接使用逻辑运算符(||、&&、!....),先unboxing成primitive(不可变的wrapper),再做比较...如
引用
Boolean case1 = true;
Boolean case2 = true;
Boolean case3 = false;
//Boolean对象先会被unboxing成primitive,然后进行比较,再boxing最后值成为Wrapper对象赋值给result
Boolean result = (case1 || case2) && case3;
分享到:
相关推荐
Starting with basic 3D<br>terminology and concepts, we take you through basic primitive assembly, transformations,<br>lighting, texturing, and eventually bring you into the full power of the ...
Starting with basic 3D<br>terminology and concepts, we take you through basic primitive assembly, transformations,<br>lighting, texturing, and eventually bring you into the full power of the ...
Chapter 8Primitives as Typesexplores the relationship between the primitive types and objects of their corresponding wrapper classes, and how boxing and unboxing can transparently convert between ...
输入: Couple<L>, Err<E>, FailCode, Failure<E>, Fault<E>, Pos, adPosition, Primitive, Report, Res, Unique, VBlock, Wildcard, Y Couple<L>是一个实现为函数处理程序的二元组。 Err<E>是一个灵活的错误类,...
At ACNS 2007, Ateniese and Green proposed the concept of ID-based proxy re-encryption<br> (IBPRE), where a semi-trusted proxy with some information (a.k.a. re-encryption key), can<br> transform a ...
supports parsing and callback for JS primitive types Modular management support System WebView and Custom WebView permission authentication is implemented by Native, JS do not need to depend other ...
Flex辅助设计工具四合一:<br>Flex3StyleExplorer_V3.0Beta.swf<br>Flex2 Filter Explorer.swf<br>EnhancedButtonDemo.swf<br>Flex2PrimitiveExplorer.swf
template<>,藉此告訴編譯器哪些符號是 型別參數( type parameters),如圖 8。 template <typename T> class Rect : public Shape 侯捷觀點 JDK 1.5的泛型實現(Generics in JDK 1.5)7 { ...
<artifactId>BeanUtils</artifactId> <version>1.0.11</version> </dependency> 特征: 支持带有 Java 原始类型的副本自动转换为其 Java 类型。 例如 int <=> 整数 支持数组类型的复制。 例如 int[] <=> ...
$ certik tx oracle create-operator < account> < collateral> --name < operator> --from < account> --fees 5000uctk -y -b block 在certik主页中创建oracle运算符配置文件(默认为.certik/config/oracle-...
Scala伽罗瓦 Scala 中的伽罗华域算术库。 例子 该库支持隐式和显式语法: 隐式语法示例: > // step 1....< 22> val field = GaloisFieldInt(primitive_poly) > > // step 3. inject a field to i
适用于React的UI Primitive,具有CSS-in-JS支持 目录 安装 npm install styled-view 用法 <View>组件支持所有默认的作为道具。 由转换和处理的样式。 import React from 'react' ; import { View } from 'styled-...
原始hamcrest 考虑到 Java 原语的基本 hamcrest 匹配器。 安装说明 将 mvn 包安装到本地存储库。 git clone https://github.com/inf295uci-2015/primitive-hamcrest.git ... <artifactId>primitive-hamcrest
这里仅仅是定义了Converter并且简单的放在<convert….>元素之内,任何的<convert….>元素内容都有两个必须定义的属性.一个是对converter定义的引用和converter能够转换的类. 例如最简单的converter是null converter,...
CompactFormatterPlus是CompactFormatter的进阶版,作了很大改进,支持...List<T> Dictionary<T Key, T1 Value> DataSet DataTable Complex objects composed of the above types 用法跟CompactFormatter基本相同
主要给大家介绍了关于Vue报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' 的解决方法,文中介绍的非常详细,需要的朋友们下面来一起看看吧。
Web Service and primitive SOA INTRODUCE
由于自己vue学习不深入,老是这个报错,找了好久(确切的说是整整一下午^…^)才找到原因 -v- Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' 点开错误的文件...
@ skypilot / common-types TypeScript项目的可重用通用类型库出口产品便利别名type AnyRecord = Record ... args : any [ ] ) : T}type Enum < E> = Record < keyof> & { [ key : number ] : string }type Primitive
Laravel开发-primitive php oop基元类型