Java wrapper class

Source: Internet
Author: User
Tags wrapper

1. Packaging Class Introduction

Package cn;/** * Calculates 100 binary, octal, and hex of this data to determine if a data is within the INT range * */public class Integerdemo {public static void main (string[] args) {System.out.println ("100 Binary is:" +integer.tobinarystring);//1100100system.out.println ("Octal 100 is:" + Integer.tooctalstring (+));//144system.out.println ("100 Hex is:" +integer.tohexstring (100));// 64SYSTEM.OUT.PRINTLN (the maximum range of type int is: "+integer.max_value);//2147483647system.out.println (the minimum range of type int is:" + Integer.min_value);//-2147483648}}


2. Basic type Packing class overview

The advantage of encapsulating a basic data type as an object is that more functional methods can be defined in the object to manipulate the data.

One of the most common operations: for converting between basic data types and strings.

Corresponding to the basic type and wrapper class:

Byte,short,integer,long,float,double,character,boolean


3.Integer class overview and how to construct it

An overview of the integer class:

The integer class wraps the value of a primitive type int in the object.

The class provides several methods for converting between int and string types, as well as some other constants and methods that are useful when working with int types.

Construction Method:

public Integer (int value)

Public Integer (String s)

Package cn;/** * Integer construction method * Public integer (int value) * Public integer (string s) * This string s must have numeric characters composed of */public class Integ ErDemo2 {public static void main (string[] args) {//mode 1int i = 100;integer II = new Integer (i); System.out.println ("II:" +II);//ii:100//mode 2String s = "n"; integer is = new Integer (s); System.out.println ("is:" +is);//is:100}}


4.Integer class member methods

Conversion of int type and string type to each other

Package cn;/** *  int type and String type conversion  *  *  int-->String  *   *  string-->int * */public class integerdemo3 {public  static void main (String[] args)  {//int --> stringint number =  100;//way 1string s1 = number+ ""; System.out.println ("S1:" +s1);//s1:100//mode 2string s2 = string.valueof (number); System.out.println ("S2:" +s2);//s2:100//mode 3//int--integer--stringinteger i = new integer ( 100); String s3 = i.tostring (); System.out.println ("S3:" +S3);//s3:100//mode 4string s4 = integer.tostring (number); System.out.println ("S4:" +S4);//s4:100//string--int//mode 1string s =  "" "; integer ii =  new integer (s); Int n = ii.intvalue (); System.out.println ("N:" +n);//n:100//mode 2int m = integer.parseint (s); System.out.println ("M:" +m);//m:100}} 


5. Automatic box packing and unpacking

JDK5 simplifies the way you define it.

Integer x = new Integer (4);

Can be written directly as an integer x = 4;//Automatic Boxing

x + = 5;//Automatic unpacking


6. Practice

Package cn;/** * See program Write Results */public class IntegerDemo4 {public static void main (string[] args) {Integer i1 = new Integer (127) ; Integer i2 = new Integer (127); System.out.println (I1 = = I2);//falsesystem.out.println (I1.equals (I2));//trueinteger i3 = new Integer (+); Integer I4 = New Integer (128); System.out.println (i3 = = i4);//falsesystem.out.println (I3.equals (I4));//trueinteger i5 = 128;integer I6 = 128; SYSTEM.OUT.PRINTLN (i5 = = I6);//falsesystem.out.println (I5.equals (I6));//trueinteger i7 = 127;integer i8 = 127; System.out.println (i7 = = i8);//truesystem.out.println (I7.equals (i8));//true//by looking at the source code, we know that for data between -128~127, Made a data buffer pool. If the data is within that range, an object is not created at a time//if the data is not of that range, an object is created each time}}


7.Character class and its construction method

Character class overview: The character class wraps the value of a base type char in an object.

In addition, the class provides several methods to determine the categories of characters (lowercase letters, numbers, and so on), and to convert characters from uppercase to lowercase, and vice versa.

Construction method: Public Character (char value)

Character class member Methods:

public static Boolean isuppercase (char ch)

public static Boolean islowercase (char ch)

public static Boolean isdigit (char ch)

public static char touppercase (char ch)

public static char toLowerCase (char ch)

Package Cn;public class Characterdemo {public static void main (string[] args) {Character c = new Character (' a '); System.out.println ("C:" +c);//c:a}}
Package cn;/** * public static Boolean isuppercase (char ch) determines whether the given character is uppercase *public static boolean islowercase (char ch) Whether the given character is lowercase characters *public static boolean isdigit (char ch) determines whether the given character is a numeric character * public static char touppercase (char ch) converts the given character to uppercase  Character *public Static char tolowercase (char ch) converts the given character to lowercase characters * */public class Characterdemo {public static void main (string[] args) {System.out.println (character.isuppercase (' A '));//truesystem.out.println (Character.isdigit (' 0 '));// TrueSystem.out.println (Character.islowercase (' a '));//truesystem.out.println (Character.touppercase (' a '));// ASystem.out.println (character.tolowercase (' A '));//a}}


8. Count the number of uppercase, lowercase, and digits in a string

package cn;/** *  Requirements: Count occurrences of uppercase, lowercase, and numeric characters in a given string  *  such as:hello123word *  Result: uppercase  5 *     lowercase characters 4 *     digital 3 */public class  characterdemo1 {public static void main (String[] args)  {//defines a string  s =  "Hello123word";//define uppercase and variable int maxsum = 0;//define lowercase characters and variables int minsum  = 0;//defines a numeric character and a variable int numsum = 0;//method   Converts a string to a character array and then counts char[] chs =  S.tochararray ();for  (int i = 0; i < chs.length; i++)  {if (chs[i ] >= ' 0 '  && chs[i] <=  ' 9 '  ) {numsum ++;} Else if (chs[i] >= ' a '  && chs[i] <= ' Z ') {minsum ++;} Else if (chs[i] >= ' A '  && chs[i] <= ' Z ') {maxsum ++;}} System.out.println (the sum of the uppercase characters is: "+maxsum);//The sum of the uppercase characters is: 5system.out.println (" The total of lowercase charactersAnd yes: "+minsum";//the sum of lowercase characters is: 4system.out.println ("The sum of the numeric characters is:" +numsum);//The sum of the numeric characters is: 3//will empty the contents of the variable maxsum = 0; minsum = 0;numsum = 0;//method Two: Gets each character in a string by the length () method of String and the Charat () method for  (Int i  = 0; i < s.length ();  i++)  {char ch = s.charat (i); if (ch  >= ' 0 '  && ch<=  ' 9 '  ) {numsum ++;} Else if (ch >= ' a '  && ch <= ' Z ') {minsum ++;} Else if (ch >= ' A '  && ch <= ' Z ') {maxsum ++;}} System.out.println (the sum of uppercase characters is: "+maxsum);//The sum of uppercase characters is: 5system.out.println (" The sum of lowercase characters is: "+minsum);// The sum of lowercase characters is: 4system.out.println ("The sum of the numeric characters is:" +numsum);//The sum of the numeric characters is: 3//will empty the contents of the variable maxsum = 0;minsum =  0;numsum = 0;//method Three: Call the method in the character class to judge for  (int i = 0; i <  s.length ();  i++)  {char ch = s.charat (i); if (Character.isuppercase (ch)) {MaxSum  ++;} Else if (Character.islowercase (ch)) {minsum ++;} Else if (Character.isdigit (ch)) {numsum ++;}} System.out.println (the sum of uppercase characters is: "+maxsum);//The sum of uppercase characters is: 5system.out.println (" The sum of lowercase characters is: "+minsum);// The sum of lowercase characters is: 4system.out.println ("The sum of the numeric characters is:" +numsum);//The sum of the numeric characters is: 3}}

This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1860382

Java wrapper class

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.