The article directories

      • Types commonly used in Java development
      • Conversion between Long, BigInteger, and String

Types commonly used in Java development

In project development, it is common to encounter type conversions after fetching data from tables, summarized here.

There are eight basic types (plus void plus String) :

Boolean: Boolean does not involve type conversions. No other type can be converted to and from Boolean types

Arr [ch ‘-‘ a ‘] arr[ch ‘-‘ a ‘] leetcode [ch ‘-‘ a ‘] arr[ch ‘-‘ a ‘]

Bype short int long; bype short int long; bype short int long

There are two types of floating-point types: float double, large for items, and double

There are four types of numeric types, generally long type is used more, because in Mysql, all numeric types can be corresponding to long, the general principle is to use long instead of int, so the following directly use long to represent numeric types to participate in type conversion.

Two floating-point, general use less than floating point, because the floating point precision is missing, that involves numerical, especially involving money, floating point can be made of two integer backwards (that is, the decimal point made), the other, the network game, integral or point, is directly with integer, hundreds of thousands of start, if the data is not important, And you do need to use a decimal place, so if you use an integer, you can use a double, but you don’t need to use a float, because the default is to use a large one, so you don’t have to worry about memory.

Large number types: BigInteger (operates on large integers), BigDecimal (specifies the reserved number of digits of a decimal)

Conversion between Long, BigInteger, and String

These three types are the most commonly used in project development and are given directly to the demo as follows:

Conversion between long and BigInteger

Class Solution{public static void main(String[] args) {// Convert long to BigInteger long a=2L; BigInteger bigInteger=BigInteger.valueOf(a); LongValue (); long b= biginteger.longValue (); }}Copy the code

Conversion between long and String

Class Solution{public static void main(String[] args) {public static void main(String[] args) { So the String class must provide methods that allow various types to convert themselves long a=2L; String string=Long.toBinaryString(a); // Any type of long BigInteger, to convert to String, just +""That's it. String string1=a+""; // String converts to long String resolves to other types long b= long.parselong (string1); }}Copy the code

Conversion between String and BigInteger

Class Solution {public static void main(String[] args) {// BigInteger Then the String class must provide methods that allow various types to convert themselves // three: +""String.valueof toString() You can change the basic and reference types to strings in any of the three ways. BigInteger BigInteger = new BigInteger("2");
        String string1 = bigInteger + ""; String string2 = String.valueOf(bigInteger); String string3 = bigInteger.toString(); To convert a String to a BigInteger type, the BigInteger class must provide a method that allows each type to convert itself. BigInteger provides bigInteger.valueof and the constructor new BigInteger(long) // For String types to convert to themselves, New BigInteger(String) BigInteger bigInteger1=new BigInteger(string1); }}Copy the code