Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JAVA regular expressions, wrapper classes, auto-boxing / auto-unpacking and how to use BigDecimal

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the "JAVA regular expressions, packaging classes, automatic packing / automatic unpacking and how to use BigDecimal" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1 Overview of regular expressions Regex1.1

Correct string format rules.

It is often used to judge whether the content entered by the user meets the requirements of the format, and it is strictly case-sensitive.

1.2 Common syntax

1.3 String provides methods to support regular expressions

Matches (regular): whether the current string can match a regular expression

ReplaceAll (regular, substring): replace substring

Split (regular): split strin

1.4 exercise: test enter ID number

Create package: cn.tedu.api

Create a class: TestRegex.java

Package cn.tedu.api;import java.util.Scanner;/* this class is used for getting started with regular expressions * / requirements: receive the ID card number entered by the user and output the result of the judgment to public class TestRegex {public static void main (String [] args) {/ / 1. The rule of editing regular expression / / ID number: there are 18 digits in total, the first 17 digits are numbers, the 18th digit may be a number, or it may be X / / String regex = "[0-9] {17} [0-9X]" / * single\ has a special meaning in Java to indicate an escape symbol and is not considered to be a slash * so if you want to represent a slash It needs to be preceded by an escaped\ * that is\\ to represent a simple slash *\ t-tab\ r carriage return\ nnewline character * / String regex = "\\ d {17} [0-9X]" / / 2. The definition variable is used to receive the ID number entered by the user: String input; / / 3. Determine whether the data entered by the user conforms to the regular expression, and if not, continue to enter do {System.out.println ("Please enter your ID number:"); input = new Scanner (System.in). NextLine (); if (input.matches (regex)) {/ / if the ID number is correct System.out.println ("Congratulations! Enter it correctly! ") ; return;// ends this method}} while (! input.matches (regex)); / / as long as it does not match the regular expression, continue to enter the}} 2 wrapper class

Package the basic types to provide more perfect functions.

The basic type does not have any function, it is just a variable, record value, and the wrapper class can have more functions.

2.1 correspondence with basic types

2.2 Number

The abstract parent class of the digital wrapper class.

Provides a variety of ways to get values.

2.3 Integer create object

Method 1: new Integer (5)

Method 2: Integer.valueOf (5)

The Integer class contains 256 Integer cache objects, ranging from-128 to 127

When using valueOf (), if you specify a value within the range, the direct access cache object is not new; if you specify a value outside the range, create a new object directly.

Common methods

Static int parseInt (String s) parses string parameters as signed decimal integers

2.4exercise: Integer of Number

Create package: cn.tedu. Api

Create a class: TestNumber.java

Package cn.tedu.api;/* this class is used to test the wrapper class * / public class TestNumber {/ / 1. Define member variables and be careful to set them to static, because static can only call static static Integer i0; public static void main (String [] args) {/ / 2. Print the default value of Integer to test System.out.println (i0); / / default is null / / 3. Create the wrapper class Integer object corresponding to the int type-- Integer i1 = new Integer (5); Integer i11 = new Integer (5); System.out.println (i1 = = i11); / / false,== compares the address value / / 4 for the reference type. Create the wrapper class Integer type object corresponding to the int type-method 2 / * Integer has an efficient effect. In this range, the same data will only be saved once, and all subsequent restores will use the previously stored data * / Integer i2 = Integer.valueOf (127); Integer i3 = Integer.valueOf (127); System.out.println (i1 = = i2) / / false System.out.println (i2 = = i3); / / true / / satisfies three conditions for high efficiency: Integer valueOf ()-128 to 127 Integer i4 = Integer.valueOf; Integer i5 = Integer.valueOf (300); System.out.println (i4 = = i5); / / false}} 2.5 Double

Create object

New Double (3.14)

Double.valueOf (3.14) / / is no different from new in common methods

Double.parseDouble ()

Exercise: Double of Number

Create package: cn.tedu.api

Create a class: TestNumber.java

Package cn.tedu.api;/* this class is used to test the wrapper class * / public class TestNumber {public static void main (String [] args) {/ / 1. The way to create int wrapper class Integer objects: 1 Integer i1 = new Integer; Integer i11 = new Integer (100); System.out.println (i1 = = i11); / / false,new twice, two different objects with different address values / / 2. Method 2 / * Integer for creating int wrapper class Integer objects has an efficient effect, but three conditions must be met: * 1. Is the Integer type * 2. Use the creation method of valueOf () * 3. The data is within the range of-128 to 127 * if the above conditions are met, the same data will only be saved once, and the subsequent use is the previously stored data * / Integer i2 = Integer.valueOf (100); Integer i22 = Integer.valueOf (100); System.out.println (i2 = = i22); / / true Integer i3 = Integer.valueOf (300); Integer i33 = Integer.valueOf (300) System.out.println (i3 = = i33); / / false is out of the efficient data range-128 / 127 / / 3. The way to create double wrapper class Double objects: 1 Double D1 = new Double (3.14); Double D11 = new Double (3.14); System.out.println (D1 = = D11); / / false, create two different objects with different address values / / 4. The way to create double wrapper Double objects 2 / * only Integer has an efficient effect Double is not * / Double D2 = Double.valueOf; Double D22 = Double.valueOf (3.14); System.out.println (D1 = = D2); / / false System.out.println (D2 = = D22); / / 5. The purpose of testing the commonly used method / / this method is to convert the data of the incoming String type into what the int / * object is, you can use all the resources of this class i1 is an object of type Integer, so you can use parseInt () to convert the data of type String to an object of type int D1 is an object of type Double. So you can use parseDouble () to convert data of type String to type double * / System.out.println (i1.parseInt) + 8) / / 808-> int+int System.out.println (d1.parseDouble ("2.2") + 3.1); / / 5.300000000000001-> double+double} 3 Overview of automatic packing and automatic unpacking 3.1

Automatic packing: the process of packaging a basic type into a corresponding packaging type

Integer a = 5 position bank an is a reference type that refers to the address of the wrapper object.

The compiler completes automatic boxing of objects: Integer a = Integer.valueOf (5)

Automatic unpacking: from the value of the packing type to the value of the basic type automatically

Int I = a _ Band _ an is now a wrapper type and cannot assign a value to a variable. 5 needs to be taken out.

The compiler will finish automatically unpacking: int I = a.intValue ()

3.2 exercise: automatic packing and automatic unpacking testing

Create package: cn.tedu.api

Create a class: TestBox.java

Package cn.tedu.api;/* this class is used to test automatic boxing and automatic unpacking * / public class TestBox {public static void main (String [] args) {/ / 1. Data defining wrapper types / / Review: two previous ways of creating wrapper types: Integer i1 = new Integer; Integer i2 = Integer.valueOf (127); / / 2. The current way: / * 1. Automatic boxing: the compiler will automatically pack the basic type int 5 into a wrapper type Integer* and then give it to i3 to save, automatically boxing the underlying code Integer.valueOf (5); * the direction of valueOf (): int-> Integer*/ Integer i3 = 5 Landmark / will not report an error, this phenomenon is automatic boxing / * 2. Automatic unpacking: the compiler will automatically unbox i1 of the packaging type, change it back to basic type data 127 * and then give it to i4 to save. The code that occurs at the bottom of the package will be automatically unpacked: i1.intValue (); * intValue () Direction: Integer-> int * * / int i4 = i1X / will not report an error, this phenomenon is automatic unpacking}} 4 BigDecimal

BigDecimal: often used to solve the problem of accurate floating-point operations.

4.1 create objects

Method 1:

BigDecimal (doubleval)

Convert double to BigDecimal, which is the binary floating-point decimal representation of double, with pits!

Method 2:

BigDecimal (String val)

Convert the form of a String type string to BigDecimal

4.2 Common methods

Add (BigDecimal bd): do addition operation

Subtract (BigDecimal bd): do subtraction

Multiply (BigDecimal bd): do multiplication

Divide (BigDecimal bd): do the division operation, and throw an exception when the division is not complete

Divide (BigDecimal bd, reserved bits, rounding mode): use when there is no limit

SetScale (reserved bits, rounding method): ditto

Pow (int n): find the power of the data

4.3 exercise: testing common methods

Create package: cn.tedu.bigdecimal

Create a class: TestBigDecimal.java

Requirements: receive two decimals entered by the user and do the operation

Package cn.tedu.api;import java.math.BigDecimal;import java.util.Scanner;public class TestBigDecimal {public static void main (String [] args) {/ / F1 (); / / using ordinary +-* / four operations exposes the problem of imprecise floating-point operation f2 (); / / uses BigDecimal to solve the problem of imprecise floating-point operation} private static void f2 () {/ / 1. Prompt and receive two decimal System.out.println entered by the user ("Please enter the two decimals you want to calculate:"); double a = new Scanner (System.in). NextDouble (); double b = new Scanner (System.in). NextDouble (); / / 2. Create a tool class object, and give the basic types an and b to the tool class object BigDecimal to save / * 1. It is best not to use double as a parameter to the constructor, otherwise there will be inaccuracies, there will be pits! * / * 2. It is best to use overloading. The parameter type is the constructor of String * double to String, and you can directly spell an empty string * / BigDecimal bd1 = new BigDecimal (a + ""); BigDecimal bd2 = new BigDecimal (b + ""); / / 3. Call its method through the object of BigDecimal to achieve precise operation / / 3.1 define reference type variables of BigDecimal type to save the result BigDecimal bd3; / / 3.2 Add (BigDecimal bd): do addition operation bd3 = bd1.add (bd2); System.out.println (bd3) / / 3.3 Subtract (BigDecimal bd): do subtraction bd3 = bd1.subtract (bd2); System.out.println (bd3); / / 3.4 Multiply (BigDecimal bd): do multiplication bd3 = bd1.multiply (bd2); System.out.println (bd3) / / 3.5 Divide (BigDecimal bd): do the division operation, and throw an exception / * 3 when the division is not complete. Division operation, an exception will be thrown when the division is not complete. ArithmeticException*/ Scheme 1: (there is a problem when the division is not complete) / / bd3 = bd1.divide (bd2) / * divide is which object to divide, n refers to how many bits to keep, o refers to rounding (such as rounding) * / option 2: bd3 = bd1.divide (bd2,3,BigDecimal.ROUND_HALF_UP); System.out.println (bd3);} private static void F1 () {/ / 1. Prompt and receive two decimal System.out.println entered by the user ("Please enter the two decimals you want to calculate:"); double a = new Scanner (System.in). NextDouble (); double b = new Scanner (System.in). NextDouble (); / / 2. Do arithmetic System.out.println (a + b); / / inexact System.out.println (a-b); / / inexact System.out.println (a * b); / / inexact System.out.println (a / b); / / inexact}} 5 extension

Analysis of rounding mode

ROUND_HALF_UP is rounded up, such as: 4.4. result is 4; 4.5 result is 5

ROUND_HALF_DOWN is rounded up. 4.5. result is 4; 4. 6 result is 5

Fair rounding of ROUND_HALF_EVEN (commonly used by banks)

For example, between 5 and 6, close to 5 is discarded into 5, close to 6 is rounded into 6, and if it is 5.5, find an even number and become 6.

ROUND_UP carries directly, not counting 0.1 or 0.9, all carry

ROUND_DOWN gives up directly, not counting 0.1 or 0.9, all are abandoned.

ROUND_CEILING (ceiling) is rounded up to take the large value of the actual value.

If round is positive in the direction of positive infinity, the behavior is the same as round_up, and if it is negative, the behavior is the same as round_down

ROUND_FLOOR (floor) is rounded down to a small value of the actual value.

If round is positive in the direction of negative infinity, the behavior is the same as round_down, and if it is negative, the behavior is the same as round_up

So much for "JAVA regular expressions, wrapper classes, auto-boxing / auto-unpacking and how to use BigDecimal". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report