The following are the articles I compiled before. Look at them. Maybe they can help you.
Java data types are divided into three categories, Boolean, Character and Numeric, and Numeric is divided into Integer and Floating-point. Relative to the data type, the variable type of Java is boolean. Character char;; Integers byte, short, int, long;; Floating point floating point number and double precision floating point number. Among them, four integer variables and two floating-point variables correspond to different precision and range respectively. In addition, we often use two variables, namely string and date. The conversion between these variable types is often used in our programming, and the following is a concrete explanation.
I. conversion between integer, real number and character variables
In Java, integer type, real type and character type are regarded as the same data. These types are (byte, short, char)-int-long-float-double from low to high, and low-level variables can be directly converted into high-level variables. For example, the following statement can be passed directly in Java:
Byte b;
int I = b;
When converting a high-level variable into a low-level variable, the situation is complicated, and you can use forced type conversion. In other words, you must use the following sentence format:
int I;
Byte b= (byte) i;
As you can imagine, this conversion will definitely lead to overflow or decrease in accuracy, so we do not recommend this conversion.
Second, Java packaging classes
When we discuss the conversion between other variable types, we need to understand the wrapper classes of Java. The so-called wrapper classes can directly represent simple types of variables as a class, and we will use these wrapper classes a lot when converting between variable types. Java*** has six wrapper classes, namely boolean, character, Integer, long, float and double. Literally, we can see that they correspond to Boolean, Char, int, Long, Float and Double respectively. Strings and dates are classes themselves. So there is no concept of packaging class.
Third, the conversion between simple type variables and encapsulated classes
Simple type variables are converted into corresponding wrapper classes, and the constructor of wrapper classes can be used. Namely:
Boolean (Boolean value), char (character value), Integer(int value), Long (long value), Float (floating point value), Double (double precision value).
In each package class, the total tangible method is ××Value () to get its corresponding simple type data. Using this method, the conversion between different numerical variables can also be realized. For example, for a double-precision real number class, intValue () can get its corresponding integer variable and doubleValue () can get its corresponding double-precision real number variable.
Fourth, the conversion between string classes and other data types
For these wrapper classes above, besides Character, there are constructors that can directly use string parameters, which makes it very simple for us to convert string classes into these data types, namely:
Boolean (string s), Integer (string s), Long (string s), Float (string s), Double (string s)
You can also use such a constructor to convert a String class into a Date class: Date(String s s).
Now we have a character variable. In fact, the String class can be understood as a char array, so we can find a way to realize this transformation in the String class: charAt(int index) can get the characters in a certain position in the String class, and toCharArray () can even transform the whole String class into a char array.
All wrapper classes have a method called toString (), which can be converted into the corresponding string class. For integer classes and long integer classes, toBinaryString(int i), toHexString(int i) and toOctalString(int i) can also be converted into binary, hexadecimal and octal string classes respectively.
5. Convert the character type directly as a numeric value to other data types.
There are actually two corresponding relationships when converting a character variable into a numerical variable. In the conversion we mentioned in the first part, it is actually converted into the corresponding ASCII code, but we sometimes need another conversion relationship. For example,' 1' refers to the value1,not its ASCII code. For this transformation, we can use the getNumericValue(char ch) of the character.
Conversion between intransitive verb date class and other data types
There is no direct correspondence between integer and Date classes, but you can use int types to represent year, month, day, hour, minute and second respectively, thus establishing the correspondence between them. When doing this conversion, you can use three forms of date class constructors:
Date(int year, int month, int date): use int type to represent year, month and day.
Date (int year, int month, int date, int hrs, int min): year, month, day, hour and minute are expressed in the form of int.
Date (int year, int month, int date, int hrs, int min, int sec): year, month, day, hour, minute and second are expressed in the form of int.
There is an interesting correspondence between long integers and date classes, that is, a time is expressed as milliseconds from 0: 00: 00 GMT19701October1. For this correspondence, the Date class also has its corresponding constructor: Date (long date).
Gets the year, month, day, hour, minute, second and week in the date class. You can use the methods of date classes getYear (), getMonth (), getDate (), getHours (), getMinutes (), getSeconds () and getDay (), which can also be interpreted as converting date classes into int.
The getTime () method of the Date class can get a long integer, which corresponds to the time we mentioned earlier. Like a wrapper class, the Date class has a toString () method to convert it into a String class.
There are other methods you can use in Java data type conversion, but these methods described above are enough for your actual programming, aren't they?
I have encountered coding problems before, and the solution is to put a layer of filter to transcode after input, and put another layer of filter when output, so as long as there are other transplants, just change the code of the filter directly.
The filter code is as follows
Public static string syconvertcode (String TempSQL) (
if (tempSql==null)
tempSql =
other
tempSql = tempSql . trim();
String returnString = tempSql
Try {
//byte[]ascii = return string . getbytes(" GBK ");
// returnString =new String(ascii,“ISO-8859- 1”);
byte[]ascii = return string . getbytes(" ISO-8859- 1 ");
ReturnString = new string (ascii, "gbk");
}catch (exception e){
e . printstacktrace();
}
Return returns a string;
}