Java Learning Note (integer Class)

Source: Internet
Author: User

This article describes the basic data type wrapper class, Integer is the wrapper class for int,

The other basic data types of the wrapper class are almost identical to the method of the integer, which can be a plenary

Basic data type wrapper class features: Used to convert between basic data types and strings

These classes belong to the core class of Java and do not require import

Method of the Integer class:

parseint method

Example:

Change a string to a basic type

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 ();    Function2 (); }     Public Static voidfunction1 () {inti = Integer.parseint ("-12")); //you can turn a string into an int typeSystem.out.println (I/2);//-6    }     Public Static voidfunction2 () {inti = Integer.parseint ("1010", 2); //converts a binary number string to a decimal intSystem.out.println (i);//Ten    }}

You can also turn a base type into a string:

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 ();        Function2 ();    Function3 (); }     Public Static voidfunction1 () {inti = 3; String String= i + ""; System.out.println (String+ 1); //The output string here is    }     Public Static voidfunction2 () {inti = 3; //The ToString method here is not a method of overriding the parent classString string =integer.tostring (i); System.out.println (String+ 1); //Output String    }     Public Static voidFunction3 () {inti = 5; String String= Integer.tostring (i, 2);        System.out.println (string); //turn into binary number, output string 101    }}

How to construct the Integerl class:

Example:

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 (); }     Public Static voidfunction1 () {integer integer=NewInteger ("100"); inti =Integer.intvalue (); //here, by the way, review the differences between i++ and ++i.//System.out.println (i++);// -System.out.println (++i);//101    }}

Other methods:

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 ();    Function2 (); }     Public Static voidfunction1 () {//static member variables of the integer classSystem.out.println (Integer.max_value);        System.out.println (Integer.min_value);        System.out.println (integer.size); //Output 2147483647-2147483648    }     Public Static voidfunction2 () {inti = 666; System.out.println (integer.tobinarystring (i));//1010011010 of binary stringsSystem.out.println (integer.tooctalstring (i));//1232 of octal stringsSystem.out.println (integer.tohexstring (i));//16 binary 29a    }}

Features after JDK1.5: Automatic packing, automatic unpacking

Auto-Boxing: Basic data type, directly into object

Auto-unpacking: Data in objects changes back to base data type

Example:

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 (); }     Public Static voidfunction1 () {integer integer= 1; //This writing is appropriate, automatic boxing//In essence: integer in = new Integer (1)Integer = integer + 1; //automatic unpacking, splitting reference types into basic types and doing operations//essentially: integer+1 <==> integer.intvalue () +1 = 2//assign to Integer, auto-boxingSystem.out.println (integer); //prints the object, but not the object address, but 1    }}

Benefits of automatic boxing and unpacking:

Ease of operation, simplifying code, allowing direct calculation between basic and reference types

Cons: For example, integer in = null; in = in + 1; There will be an exception and the corresponding processing method must be added

Notes on auto-boxing and unpacking:

Here's a place where a lot of people hang out in a Java interview,

 Packagedemo; Public classIntegerdemo { Public Static voidMain (string[] args) {function1 ();        Function2 ();    Function3 (); }     Public Static voidfunction1 () {Integer I=NewInteger (1); Integer J=NewInteger (1); System.out.println (i==J);//false//This compares the address of two objects, of course, differentSystem.out.println (I.equals (j));//true//here is the data of the comparison object, which does not compare the address    }         Public Static voidfunction2 () {Integer a= 500; Integer b= 500; System.out.println (A==B);//falseSystem.out.println (A.equals (b));//true    }         Public Static voidFunction3 () {Integer a= 127; Integer b= 127; System.out.println (A==B);//true//note here that a value greater than 128 is false//when the data is within the bytes range, the JVM does not create new objects in order to conserve memory//here the integer b = 127 <==> integer b = aSystem.out.println (A.equals (b));//true    }}

Java Learning Note (integer Class)

Related Article

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.