Java Encapsulation Class (i)

Source: Internet
Author: User

To compensate for the lack of basic data types in object-oriented terms, the Java language introduces encapsulation classes (wrapper Classes), which provide the appropriate reference data types for a variety of basic data types.

Encapsulated classes are defined as final, so they cannot be inherited, and once created, the content cannot be changed.


public class Demo1 {public

	static void Main (string[] args) {
		byte byte1 = new Byte ((Byte) 1);
		
		NumberFormatException
		Integer i = new Integer ("a");
		
		System.out.println (i.ToString ());
		System.out.println (I.intvalue ());//int  
		
		Boolean flag = new Boolean ("true")
		; SYSTEM.OUT.PRINTLN (flag);
		
		
		Java.lang.NumberFormatException
		String s= "100s";
		
		Object-oriented
		Double d = double.valueof (s);
		Base data type
		double DD = double.parsedouble (s);
	}
}

the construction methods for each wrapper class have several overloaded forms, which are summarized as follows:

All encapsulation classes can construct their instances using the corresponding base type data as parameters. For example:

Boolean Bln=new Boolean (true); BYTE b=new byte (byte 1);

In addition to the character class, other wrapper classes can construct their instances with strings as arguments. For example:

Integer I =new integer ("123"); Double D=new double ("123.45D");

When the parameter of the constructor of a Boolean class is a string type, if the string content is true (regardless of case), the Boolean object represents true, otherwise it represents false. For example:

System.out.println (Newboolean ("true"));   Print true System.out.println (new Boolean ("AAAA")); Print False

When the arguments for the construction method of Byte, short, Integer, Long, float, and double are string, the string cannot be null, and the string must be resolvable to the corresponding base type of data, or, if the compilation passes, The runtime, however, throws a NumberFormatException run-time exception.

The Java language uses encapsulated classes to convert basic type data to objects. Each Java base type has a corresponding encapsulated class in the Java.lang package.

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.