In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you how to convert variable types in JAVA. The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn how to convert variable types in JAVA.
Java's data types are divided into three categories, namely Boolean, character and numerical, and numerical types are divided into integer and floating point; relative to data types, Java variable types are Boolean; character char; integer byte, short, int, long; floating point float, double. Four integer variables and two floating point variables correspond to different precision and range, respectively. In addition, we often use two kinds of variables, namely String and Date. These conversions between variable types are often used in our programming, and in today's article we'll look at how to implement them.
I. Interconversion among integer, real and character variables
In Java integer, real type, character type is regarded as the same type of data, these types from low-level to high-level are (byte, short, char)--int--long--float--double, low-level variables can be directly converted to high-level variables, for example, the following statement can be directly passed in Java:
byte b;
int i=b;
Converting high-level variables to low-level variables is a bit more complicated, and you can use coercion. That is, you must use the following sentence format:
int i;
byte b=(byte)i;
As you can imagine, this conversion would certainly result in overflow or loss of precision, so we do not recommend it.
Java wrapper classes
Before we talk about conversions between other variable types, we need to understand Java wrapper classes, which can directly represent variables of simple types as a class, and we will use these wrapper classes extensively when performing conversions between variable types. Java has six wrapper classes, Boolean, Character, Integer, Long, Float, and Double, which literally correspond to boolean, char, int, long, float, and double. String and Date are classes themselves. So there is no concept of packaging.
III. Conversion between simple type variables and wrapper classes
Simple variables are converted to the corresponding wrapper class, using the wrapper class constructor. Namely:
Boolean(boolean value)、Character(char value)、Integer(int value)、Long(long value)、Float(float value)、Double(double value)
In each wrapper class, there is always a method named ××Value() to get its corresponding simple type data. This method can also be used to convert 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 between String class and other data types
For these wrapper classes above, except Character, there are constructors that can directly use string parameters, which makes it quite easy for us to convert String classes to these data types, namely:
Boolean(String s)、Integer(String s)、Long(String s)、Float(String s)、Double(String s)
The String class can also be converted to the Date class using the constructor Date(String s).
Now we are left with a char variable, in fact the String class can be understood as a char array, so we can find methods in the String class to implement this conversion: charAt(int index) can get a character at a certain position in the String class, toCharArray() can even convert the entire String class into an array of chars.
For all wrapper classes, there is a method called toString() that converts them to the corresponding String class, and for integer and long integer classes, toBinaryString(int i), toHexString(int i), and toOctalString(int i) can also be converted to String classes in binary, hexadecimal, and octal form, respectively.
5. Convert character type directly as numeric value to other data types
There are actually two ways to convert character variables to numeric variables. In the conversion we talked about in Part 1, we actually convert them to ASCII, but we sometimes need another conversion relationship. For example,'1' refers to the numeric value 1, not its ASCII code. For this conversion, we can use the getNumericValue(char ch) method of Character.
VI. Conversion between Date class and other data types
There is no direct correspondence between integers and Date classes, but you can use int to represent year, month, day, hour, minute, and second, respectively, thus establishing a correspondence between the two. When doing this conversion, you can use the Date class constructor in three forms:
Date(int year, int month, int date): int to represent the year, month, day
Date(int year, int month, int date, int hrs, int min): int type to represent year, month, day, hour, minute
Date(int year, int month, int date, int hrs, int min, int sec): int to represent year, month, day, hour, minute, second
There is an interesting correspondence between the long integer and the Date class, which is to express a time as milliseconds from 0:00 GMT on January 1, 1970. For this correspondence, the Date class also has its corresponding constructor: Date(long date)
Get the year, month, day, hour, minute, second, and week in the Date class You can use the getYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(), getDay() methods of the Date class, or you can think of it as converting the Date class to int.
The getTime() method of the Date class can get the long integer corresponding to a time we mentioned earlier. Like the wrapper class, the Date class also has a toString() method that can convert it to a String class.
The above is "JAVA variable types how to convert each other" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.