Conversion between Java variable types

Source: Internet
Author: User
Tags date array constructor implement integer variables variable tostring
variables | conversion???? We know that Java data types are grouped into three categories, Boolean, character, and numeric, and the numeric type is divided into integers and floating-point types, and the Java variable type is Boolean, as opposed to the data type; character char; integer byte, short, int, long ; float float, double. The four types of integer variables and two floating-point variables correspond to different precision and range respectively. In addition, we often use two kinds of variables, string and date. The conversion between these variable types is often used in our programming, and in our today's article we will look at how to implement these transformations.





???? The mutual conversion
in integral type, real type and character type variable

???? In Java, Integer, real, and character types are considered to be the same type of data, from low-level to advanced respectively (Byte,short,char)--int--long--float--double, and low-level variables can be directly converted to advanced variables, for example, The following statements can be passed directly in Java:


???? BYTE B;


???? int i=b;


???? When you convert an advanced variable to a low-level variable, the situation is a bit more complicated, and you can use coercion type conversions. That is, you must use the following statement format:


???? int i;


???? byte b= (byte) i;


???? As you can imagine, this conversion is certainly likely to result in an overflow or a drop in precision, so we do not recommend using this conversion.





???? Second, the Java packaging class


???? As we discuss the conversion of other variable types, we need to understand the Java wrapper class, the so-called wrapper class, is that you can directly represent a simple type of variable as a class, in the execution of variable types of mutual conversion, we will use the bulk of these wrapper classes. Java has a total of six wrapper classes, Boolean, Character, Integer, long, float, and double, literally we can see that they correspond to Boolean, char, int, long, float and double. and string and date themselves are classes. So there is no concept of packaging class.





???? Three, simple type variable and wrapper class mutual conversion


???? A variable of a simple type is converted to the appropriate wrapper class, which can take advantage of the constructor of the wrapper class. namely:


???? Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value)


???? and in each wrapper class, the total visible as Xxvalue () method, to get its corresponding simple type of data. By using this method, we can also realize the transformation between different numerical variables, for example, for a double-precision real class, Intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double precision real variable.





???? Iv. Conversion of string classes and other data types


???? For these wrapper classes above, there are constructors that can directly use string arguments in addition to character, which makes it quite simple to convert the string class to these data types, namely:


???? Boolean (string s), Integer (string s), Long (string s), Float (string s), Double (string s)


???? The same constructor can be used to convert the String class to the date class: Date (string s)


???? Now we have one character variable, in fact the string class can be interpreted as a char array, so we can find a way to implement this transformation in the String class: CharAt (int index) can get a character from a position in the string class. ToCharArray () can also convert an entire string class to an array of char.


???? For all wrapper classes, a method named ToString () can be converted to the corresponding string class, and for an integer class and a long integer class, you can also use tobinarystring (int i), tohexstring (int i), The tooctalstring (int i) is converted to the string class in binary, hexadecimal, and octal form respectively.





???? Convert character type directly to other data type


???? There are actually two kinds of correspondence between converting character variables to numeric variables, which we actually convert to the corresponding ASCII code in the transformation we're talking about in the first part, but sometimes we need another transformation relationship, for example, ' 1 ' means the value 1, not its ASCII code, and for this conversion we can use the character getnumericvalue (char ch) method.





???? Vi. Conversion of date classes to other data types


???? There is no direct correspondence between the integral type and the date class. You can just use the int type to represent the year, month, day, time, minute, and second, so that you have a corresponding relationship between the two, and you can use the three forms of the date class constructor when making this transition:


???? Date (int year, int month, int date):
with int type

???? Date (int year, int month, int date, int hrs, int min): An int to represent years, months, days, times, and minutes


???? Date (int year, int month, int date, int hrs, int min, int sec): For years, months, days, hours, minutes, and seconds in int type


???? An interesting correspondence between the long and the date classes is to represent a time as the number of milliseconds from 0:0 Greenwich Mean Time of January 1, 1970 to 0 seconds. For this correspondence, the date class also has its corresponding constructor: date (long date)


???? Gets the year, month, day, time, minute, second, and week in the date class you can use the Date class getyear (), getmonth (), GetDate (), getHours (), getminutes (), getseconds (), Getday ( method, you can also interpret it as converting the date class to int.


???? The GetTime () method of the date class can get the long integer corresponding to the time we mentioned earlier, and like the wrapper class, the date class also has a ToString () method that can convert it to a string class.


???? You have some other methods available in Java data type conversions, but the methods described above are sufficient for your actual programming, aren't they?














Related Article

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.