Overview of Java basic type wrapper class and usage analysis of Integer class and Character class

  • 2021-07-09 08:04:09
  • OfStack

This article gives an example of Java basic type wrapper class overview and Integer class, Character class usage. Share it for your reference, as follows:

Overview of basic type wrapper classes

The advantage of encapsulating a basic data type into an object is that you can define more functional methods in the object to manipulate the data.

Common Operation 1: Used to convert between basic data types and strings.

Correspondence between base type and wrapper class

Byte, Short, Integer, Long, Float, Double, Character, Boolean

Class Integer

To allow more manipulation of primitive types of data, Java provides a corresponding wrapper class type for each primitive type


package cn.itcast_01;
/*
 *  Demand 1 : I ask everyone to put 100 Of this data 2 Binary system, 8 Binary system, 106 Calculated in binary system 
 *  Demand 2 : I ask everyone to judge 1 Whether the data is int Within the scope. 
 *      First of all, you know int What is the range of ?
 *
 *  In order to perform more operations and more convenient operations on basic data types, Java For every 1 The basic data types provide corresponding class types. Wrapper class type. 
 * byte       Byte
 * short      Short
 * int        Integer
 * long        Long
 * float      Float
 * double      Double
 * char        Character
 * boolean      Boolean
 *
 *  Used to convert between basic data types and strings. 
 */
public class IntegerDemo {
  public static void main(String[] args) {
    //  It's no trouble to come 
    // public static String toBinaryString(int i)
    System.out.println(Integer.toBinaryString(100));
    // public static String toOctalString(int i)
    System.out.println(Integer.toOctalString(100));
    // public static String toHexString(int i)
    System.out.println(Integer.toHexString(100));
    // public static final int MAX_VALUE
    System.out.println(Integer.MAX_VALUE);
    // public static final int MIN_VALUE
    System.out.println(Integer.MIN_VALUE);
  }
}

The Construction Method of Integer


package cn.itcast_02;
/*
 * Integer The construction method of: 
 * public Integer(int value)
 * public Integer(String s)
 *      Note: This string must consist of numeric characters 
 */
public class IntegerDemo {
  public static void main(String[] args) {
    //  Mode 1
    int i = 100;
    Integer ii = new Integer(i);
    System.out.println("ii:" + ii);
    //  Mode 2
    String s = "100";
    // NumberFormatException
    // String s = "abc";// This string must consist of numeric characters 
    Integer iii = new Integer(s);
    System.out.println("iii:" + iii);
  }
}

Conversion between String and int


package cn.itcast_03;
/*
 * int Type and String Conversion of types to each other 
 *
 * int -- String
 *     String.valueOf(number)
 *
 * String -- int
 *     Integer.parseInt(s)
 */
public class IntegerDemo {
  public static void main(String[] args) {
    // int -- String
    int number = 100;
    //  Mode 1
    String s1 = "" + number;
    System.out.println("s1:" + s1);
    //  Mode 2
    String s2 = String.valueOf(number);
    System.out.println("s2:" + s2);
    //  Mode 3
    // int -- Integer -- String
    Integer i = new Integer(number);
    String s3 = i.toString();
    System.out.println("s3:" + s3);
    //  Mode 4
    // public static String toString(int i)
    String s4 = Integer.toString(number);
    System.out.println("s4:" + s4);
    System.out.println("-----------------");
    // String -- int
    String s = "100";
    //  Mode 1
    // String -- Integer -- int
    Integer ii = new Integer(s);
    // public int intValue()
    int x = ii.intValue();
    System.out.println("x:" + x);
    // Mode 2
    //public static int parseInt(String s)
    int y = Integer.parseInt(s);
    System.out.println("y:"+y);
  }
}

Operation of binary conversion of Integer


package cn.itcast_04;
/*
 *  Commonly used basic binary conversions 
 * public static String toBinaryString(int i)
 * public static String toOctalString(int i)
 * public static String toHexString(int i)
 *
 * 10 Binary to other binary 
 * public static String toString(int i,int radix)
 *  From this, we also see the scope of the binary system: 2-36
 *  Why ?0,...9,a...z , add up 36 A 
 *
 *  Other binary to 10 Binary system 
 * public static int parseInt(String s,int radix)
 */
public class IntegerDemo {
  public static void main(String[] args) {
    // 10 Binary to 2 Binary system, 8 Binary system, 106 Binary system 
    System.out.println(Integer.toBinaryString(100));
    System.out.println(Integer.toOctalString(100));
    System.out.println(Integer.toHexString(100));
    System.out.println("-------------------------");
    // 10 Binary to other binary 
    System.out.println(Integer.toString(100, 10));
    System.out.println(Integer.toString(100, 2));
    System.out.println(Integer.toString(100, 8));
    System.out.println(Integer.toString(100, 16));
    System.out.println(Integer.toString(100, 5));
    System.out.println(Integer.toString(100, 7));
    System.out.println(Integer.toString(100, -7));
    System.out.println(Integer.toString(100, 70));
    System.out.println(Integer.toString(100, 1));
    System.out.println(Integer.toString(100, 17));
    System.out.println(Integer.toString(100, 32));
    System.out.println(Integer.toString(100, 37));
    System.out.println(Integer.toString(100, 36));
    System.out.println("-------------------------");
    // Other binary to 10 Binary system 
    System.out.println(Integer.parseInt("100", 10));
    System.out.println(Integer.parseInt("100", 2));
    System.out.println(Integer.parseInt("100", 8));
    System.out.println(Integer.parseInt("100", 16));
    System.out.println(Integer.parseInt("100", 23));
    //NumberFormatException
    //System.out.println(Integer.parseInt("123", 2));
  }
}

New Features of JDK5--Automatic Packing and Unpacking


package cn.itcast_05;
/*
 * JDK5 New features of 
 *  Automatic boxing: converting basic types to wrapper class types 
 *  Automatic unpacking: converting wrapper class types to basic types 
 *
 *  Attention 1 A small question: 
 *      When in use, Integer x = null; The code will appear NullPointerException . 
 *      It is suggested to judge whether it is null And then use it again. 
 */
public class IntegerDemo {
  public static void main(String[] args) {
    //  Defines the 1 A int Wrapper class type variable of type i
    // Integer i = new Integer(100);
    Integer ii = 100;
    ii += 200;
    System.out.println("ii:" + ii);
    //  Code after decompilation 
    // Integer ii = Integer.valueOf(100); // Automatic packing 
    // ii = Integer.valueOf(ii.intValue() + 200); // Automatic unpacking and then automatic packing 
    // System.out.println((new StringBuilder("ii:")).append(ii).toString());
    Integer iii = null;
    // NullPointerException , if iii If it is an empty object, an error will be reported. It is necessary to judge whether it is empty or not 
    if (iii != null) {
      iii += 1000;
      System.out.println(iii);
    }
  }
}

-Data buffer pool problem between 128 and 127


package cn.itcast_06;
/*
 *  Look at the program and write the result 
 *
 *  Note: Integer If you assign the data directly in the -128 To 127 Data is fetched directly from the buffer pool 
 */
public class IntegerDemo {
  public static void main(String[] args) {
    Integer i1 = new Integer(127);
    Integer i2 = new Integer(127);
    System.out.println(i1 == i2);
    System.out.println(i1.equals(i2));
    System.out.println("-----------");
    Integer i3 = new Integer(128);
    Integer i4 = new Integer(128);
    System.out.println(i3 == i4);
    System.out.println(i3.equals(i4));
    System.out.println("-----------");
    Integer i5 = 128;
    Integer i6 = 128;
    System.out.println(i5 == i6);
    System.out.println(i5.equals(i6));
    System.out.println("-----------");
    Integer i7 = 127;
    Integer i8 = 127;
    System.out.println(i7 == i8);//true
    System.out.println(i7.equals(i8));
    //  By looking at the source code, we know that for the -128 To 127 Between the data, did 1 Data buffer pool, if the data is in this range, no new space is created every time 
    // Integer ii = Integer.valueOf(127);
  }
}

Character

The Character class wraps a value of a primitive type char in an object


package cn.itcast_01;
/*
 * Character  Class wraps in an object 1 Basic types  char  Value of 
 *  In addition, the class provides several methods to determine the category of characters (lowercase letters, numbers, and so on) and convert characters from uppercase to lowercase, and vice versa 
 *
 *  Construction method: 
 *     Character(char value)
 */
public class CharacterDemo {
  public static void main(String[] args) {
    //  Create an object 
    // Character ch = new Character((char) 97);
    Character ch = new Character('a');
    System.out.println("ch:" + ch);
  }
}

Character class, common method.

Determine the category of characters (lowercase letters, numbers, and so on) and convert characters from uppercase to lowercase


package cn.itcast_02;
/*
 * public static boolean isUpperCase(char ch): Determines whether a given character is an uppercase character 
 * public static boolean isLowerCase(char ch): Determines whether a given character is lowercase 
 * public static boolean isDigit(char ch): Determines whether a given character is a numeric character 
 * public static char toUpperCase(char ch): Converts a given character to uppercase 
 * public static char toLowerCase(char ch): Converts a given character to lowercase 
 */
public class CharacterDemo {
  public static void main(String[] args) {
    // public static boolean isUpperCase(char ch): Determines whether a given character is an uppercase character 
    System.out.println("isUpperCase:" + Character.isUpperCase('A'));
    System.out.println("isUpperCase:" + Character.isUpperCase('a'));
    System.out.println("isUpperCase:" + Character.isUpperCase('0'));
    System.out.println("-----------------------------------------");
    // public static boolean isLowerCase(char ch): Determines whether a given character is lowercase 
    System.out.println("isLowerCase:" + Character.isLowerCase('A'));
    System.out.println("isLowerCase:" + Character.isLowerCase('a'));
    System.out.println("isLowerCase:" + Character.isLowerCase('0'));
    System.out.println("-----------------------------------------");
    // public static boolean isDigit(char ch): Determines whether a given character is a numeric character 
    System.out.println("isDigit:" + Character.isDigit('A'));
    System.out.println("isDigit:" + Character.isDigit('a'));
    System.out.println("isDigit:" + Character.isDigit('0'));
    System.out.println("-----------------------------------------");
    // public static char toUpperCase(char ch): Converts a given character to uppercase 
    System.out.println("toUpperCase:" + Character.toUpperCase('A'));
    System.out.println("toUpperCase:" + Character.toUpperCase('a'));
    System.out.println("-----------------------------------------");
    // public static char toLowerCase(char ch): Converts a given character to lowercase 
    System.out.println("toLowerCase:" + Character.toLowerCase('A'));
    System.out.println("toLowerCase:" + Character.toLowerCase('a'));
  }
}

Statistic the number of uppercase alphabetic characters, lowercase alphabetic characters and numeric characters in a string


package cn.itcast_03;
import java.util.Scanner;
/*
 *  Statistics 1 The number of times that uppercase alphabetic characters, lowercase alphabetic characters and numeric characters appear in a string. ( Other characters are not considered )
 *
 *  Analysis: 
 *     A: Definition 3 Statistical variables. 
 *       int bigCont=0;
 *       int smalCount=0;
 *       int numberCount=0;
 *     B: Keyboard entry 1 Strings. 
 *     C: Converts a string to an array of characters. 
 *     D: Traverse the character array to get every 1 Characters 
 *     E: Determine that the character is 
 *        Capitalized   bigCount++;
 *        Lowercase   smalCount++;
 *        Figures   numberCount++;
 *     F: Output the result 
 */
public class CharacterTest {
  public static void main(String[] args) {
    //  Definition 3 Statistical variables. 
    int bigCount = 0;
    int smallCount = 0;
    int numberCount = 0;
    //  Keyboard entry 1 Strings. 
    Scanner sc = new Scanner(System.in);
    System.out.println(" Please enter 1 String: ");
    String line = sc.nextLine();
    //  Converts a string to an array of characters. 
    char[] chs = line.toCharArray();
    //  Calendar character array gets to every 1 Characters 
    for (int x = 0; x < chs.length; x++) {
      char ch = chs[x];
      //  Determine the character 
      if (Character.isUpperCase(ch)) {
        bigCount++;
      } else if (Character.isLowerCase(ch)) {
        smallCount++;
      } else if (Character.isDigit(ch)) {
        numberCount++;
      }
    }
    //  Output the result 
    System.out.println(" Capital letters: " + bigCount + " A ");
    System.out.println(" Lowercase letters: " + smallCount + " A ");
    System.out.println(" Numeric characters: " + numberCount + " A ");
  }
}

PS: Here are some online tools with similar functions for your reference:

Online arbitrary binary conversion tool:
http://tools.ofstack.com/transcoding/hexconvert

Word count tool:
http://tools.ofstack.com/code/zishutongji

Online letter case conversion tool:
http://tools.ofstack.com/transcoding/upper

For more readers interested in java related contents, please check the special topics of this site: "Summary of Java Character and String Operation Skills", "Summary of Java Array Operation Skills", "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structure and Algorithm" and "Summary of Java Operation Skills of DOM Node"

I hope this article is helpful to everyone's java programming.


Related articles: