Java wrapper classes and inner classes

Source: Internet
Author: User
Tags wrapper

One: wrapper classes for basic data types
The Java language provides the corresponding wrapper class for all basic data types
Basic data Type wrapper class
Byte (bytes) java.lang.Byte
char (character) Java.lang.Character
Short (shorter integer) java.lang.Short
int (integral type) Java.lang.Integer
Long (L-integer) Java.lang.Long
Float (float type) java.lang.Float
Double (dual precision) java.lang.Double
Boolean (Boolean) java.lang.boolean! []

wrapper classes and basic types
The base data type is converted to the wrapper class: Integer intvalue = new integer (21);
The wrapper class is converted to the basic type: integer integerid=new integer (25); int Intid=integerid.intvalue ();
Two: string related classes

  1. The
  2. String class,
    (1) string, represents the string type, and the contents of the string are immutable, and the string is stored in the string constant pool.
    (2) Two instantiation methods of String
    A: Direct assignment method:
    eg:string str= "Hello World";
    B: Instantiated with the new operator:
    eg:string str=new String ("Hello World");
    (3) A string is an anonymous object of a string class. The
    so-called anonymous object is the space in the heap memory, but there is no reference to the object in the stack memory.
    (4) Common Methods for strings:
    Common method of the string class:
    1.public string (byte[] bytes)
    uses the platform's default character set decoding, converts a byte array to a string
    2.public string ( Byte[] Bytes,charset Charset)
    Use a specified character set to decode, convert a byte array to a string
    3.public char charAt (int index)
    Get a character in a string based on the index location
    4.public Boolean contains (charsequence s)
    Determines whether the string represented by the current object contains the parameter string contents
    5.public Boolean equals (Object anobject)
    Determine if the contents of the string are the same
    6.public byte[] getBytes ()
    Converts a string to a byte array
    7.public int indexOf (string str)
    Returns the index position of the parameter string in the current string
    9.public int lastIndexOf (string str)
    to find the argument string forward, returning the index position of the parameter string in the current string
    10.public int Length ()
    Returns the length of the current string
    11.public string toLowerCase ()
    Converts a string to lowercase
    12.public string touppercase ()
    Converts a string to uppercase

  3. StringBuffer class:
    (1) StringBuffer represents a variable sequence of characters.
    StringBuffer, called a string buffer, works by pre-applying a piece of memory, storing a sequence of characters, and, if the character sequence is full, alters the size of the buffer to accommodate more sequences of characters.
    (2) StringBuffer is a mutable object, which is the biggest difference from the string (if the string object is continuously manipulated, it produces a lot of "garbage" and "disconnects-joins" frequently.) )

3.StringBuilder class
StringBuilder and StringBuffer functions are almost the same, just StringBuilder is thread insecure
Three: Inner class
1. Define another class within the class. If you define a class inner inside a class outer, inner is called an inner class, and outer is called an external class.
2. The definition format of the inner class
public class external class {
External class Members
public class Inner classes {
Inner class member
}
}
3. When the internal class is compiled, the. class file is generated, and the file name is the external class name $ internal class name. class
Reasons for using internal classes:
For example: The heart is part of a human being: But the heart cannot simply represent a heart with attributes or methods, because the heart also has its own properties and behavior, so it needs to be written in the human interior
Benefits:
1 Easy access to private properties of external classes
2 reduced the size of the bytecode file generated after the class file is compiled

Cons: Making the program structure unclear
1. member Inner class
Static variable cannot be defined in a member inner class
member Inner class holds references to external classes
In the case of file management, the member inner class will also produce a. class file after compilation, and the file name is the "external class name $ internal class name."
Externally instantiates the format of a member's inner class:
External class. Inner class Inner Class object = External class instance. New inner class ();
2. Static Inner class
If an inner class uses a static declaration, this inner class is called a static inner class, which is actually equivalent to an external class. It can be accessed through an external class. Internal classes.
Static inner classes do not hold references to external classes and can be created without creating external class objects
Static inner classes can access external static variables if non-static member variables accessing external classes must be accessed through an instance of an external class
Externally instantiating the format of a static inner class object:
External class. Inner class Inner Class object = new External class. Inner class ();
3. Intra-local class
A local inner class is an inline class defined in a method, so the scope of the class is limited to that method, and the object generated by the class can only be used in that method.
A local class cannot contain static members.
Special attention:
Local class in order to access the parameters in the method that contains it, the parameters in the method must precede the final keyword (jdk<=1.7).
4. Anonymous inner class
If an inner class is used only once throughout the operation, it can be defined as an anonymous inner class.
An internal class without a name, which is a mechanism designed by Java to facilitate our programming, because sometimes an internal class needs to create an object of its own, and it will not be used in the future, and it would be appropriate to use an anonymous inner class.

Java wrapper classes and inner classes

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.