Wrapper classes for basic data types

Source: Internet
Author: User

At the beginning of Java design, everything was the object. But the base data type is not an object.

At the beginning of the human packaging a basic data type

class myint{privateint  num; public Myint (int  num) {//package this. num = num;}
public int Intvalue () {//Unpacking
return this.num;
}
.....
}

Java provides a number of wrapper classes.

BYTE (byte)

Short (short)

int (Interger) .....

Long (long)

Float (float)

Double (double)

char (Character) ...

Boolean (Boolean)

The above are divided into two seed types:

1. Object type wrapper class: Character and Boolean inherit from Object

2. Data type wrapper class: Inherit from number

Packing and unpacking:

There are already basic data types and wrapper classes, conversions between the two variables

Boxing: Changing the base data type into a wrapper class

Unpacking: Removing data

Complete with the Xxxvalue () of the number class.

Int:

 Public class demo{    publicstaticvoid  main (String arg[]) {        new Integer (10); // Boxing a basic data type        int temp = obj.intvalue ();         * 2);}    }

Boolean

 Public class demo{    publicstaticvoid  main (String arg[]) {        new Boolean (true);         Boolean temp = obj.booleanvalue ();        SYSTEM.OUT.PRINTLN (temp);}    }

The above code was before JDK1.5, and later Java optimizations:

Example:

ImportJavax.swing.JCheckBox; Public classdemo{ Public Static voidMain (String arg[]) {Integer obj= 10 ; inttemp =obj; Obj++ ;                 System.out.print (obj); Integer Obja= 10 ; Integer OBJB= 10;//Direct AssignmentInteger OBJC =NewInteger (10);//Construction MethodSystem.out.println (Obja = = OBJB);//trueSystem.out.println (Obja = = OBJC);//falseSystem.out.println (Obja.equals (OBJC));//true    }}

Be sure to use equals () when judging if they are equal.

 Public class demo{    publicstaticvoid  main (String arg[]) {        = ten ;     Wrap and convert         int integer = (integer) object first;        System.out.println (integer);}             }


Double Dou = 10; Error
Double double1 = 10.2;//correct

Data type conversions

The most commonly used wrapper class is the data type conversion feature, which converts a string type to another type of conversion.

Example:

int type

Float type

Double type

Character type

CharAt ();

String string = "123" ;         int temp = integer.parseint (string);        

 Public class demo{    publicstaticvoid  main (String arg[]) {        = "ADFADF" ;         boolean bo = Boolean.parseboolean (string);         if (bo) {            System.out.println ("true");        } Else {            System.out.println ("false");}}}            
false Result
If the characters are not "true" and "false" at the time of conversion, then long will be converted to false

Overview :: Any character type and string type will become string after using the + operation;

However, there is a garbage problem with this operation. num + "";

There are some methods in string:

ValueOf ();

int num = + ;         = string.valueof (num);        System.out.println (String.replaceall ("0", "1"));

Wrapper classes for basic data types

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.