Wrapper class in Java (i)

Source: Internet
Author: User

There are nine package classes in Java, namely Boolean,byte,short,integer,long,float,double,character,void

where void is not of much significance to the consumer, nor can any object be constructed, the code is as follows:

 Package Java.lang;  Public Final class Void {    publicstaticfinal class<void> TYPE = Class.getprimitiveclass (" void ");     Private Void () {}}
View Code

In these encapsulation classes, the simplest of the Boolean type, the character is the most complex, character personally think is also the entire Java in the most complex of a class, first say Boolean:

 PackageJava.lang; Public Final classBooleanImplementsJava.io.serializable,comparable<boolean>{     Public Static FinalBoolean TRUE =NewBoolean (true);  Public Static FinalBoolean FALSE =NewBoolean (false);  Public Static Finalclass<boolean> TYPE = Class.getprimitiveclass ("Boolean"); Private Final Booleanvalue; Private Static Final LongSerialversionuid = -3665804199014368530l;  PublicBoolean (Booleanvalue) {         This. Value =value; }     PublicBoolean (String s) { This(ToBoolean (s)); }     Public Static BooleanParseboolean (String s) {returnToBoolean (s); }     Public BooleanBooleanvalue () {returnvalue; }     Public StaticBoolean ValueOf (Booleanb) {return(b?true:false); }     Public StaticBoolean valueOf (String s) {returnToBoolean (s)?True:false; }     Public StaticString toString (Booleanb) {returnB? "True": "false"; }     PublicString toString () {returnValue? "True": "false"; }     Public inthashcode () {returnValue? 1231:1237; }     Public Booleanequals (Object obj) {if(objinstanceofBoolean) {            returnValue = =( (Boolean) obj). Booleanvalue (); }        return false; }     Public Static BooleanGetboolean (String name) {Booleanresult =false; Try{result=ToBoolean (System.getproperty (name)); } Catch(IllegalArgumentException e) {}Catch(NullPointerException e) {}returnresult; }     Public intCompareTo (Boolean b) {returnCompare This. Value, B.value); }     Public Static intCompareBooleanXBooleany) {return(x = = y)? 0: (x? 1:-1); }    Private Static BooleanToBoolean (String name) {return((Name! =NULL) && name.equalsignorecase ("true")); }}
View Code

The Boolean implements the comparable interface, which means that the Boolean type itself can be compared, whereas for a Boolean type, it is assumed that true is greater than false in Java. Examples are as follows:

 PackageDemo.nio; Public classBooleandemo { Public Static voidMain (string[] args) {System.out.println (Boolean.compare (true,false));//1 represents greater thanSystem.out.println (Boolean.compare (false,false));//0 represents equal toSystem.out.println (Boolean.compare (true,true));//0 represents equal toSystem.out.println (Boolean.compare (false,true));//-1 means less than    }}

It is worth mentioning that the Boolean hashcode is a two constant, which guarantees that regardless of where the new object, as long as the internal value field is true, its hashcode is 1231, false is 1237. The Equals method completely invokes the = = operation of the internal value field. All package types contain valueof static functions, and have

Two overloaded versions, which are primitive type parameters and string parameters, respectively. There is also a parse function that can be used internally to avoid duplicate boxing operations by returning data from the cache. The cache for Boolean in Java is the boolean.true and Boolean.false two static fields. They are public types.

 Public class Booleandemo {    publicstaticvoid  main (string[] args) {        = Boolean.valueof (true);         = Boolean.valueof ("true");         = Boolean.parseboolean ("true");}    }
View Code

Wrapper class in Java (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.