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

Example Analysis of Java data Type conversion

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of Java data type conversion for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Java data type conversion

Simple types of 1.Java and their wrapper classes

⑴ Java simple types and wrapper classes

We know that Java is a typical object-oriented programming language, but considering the advantages of simple structure, small memory and fast access of some basic Java data types, Java still provides support for these non-object-oriented simple Java data types. Of course, when Java provides a large number of other classes, it also provides wrapper classes corresponding to simple Java data types, so there are things such as int and Integer (float and Float, double and Double …) in Java. Different Java data types of the

There are two main types of Java data types in the Java language: one is simple types, also known as primary types (Primitive), and the other is reference types (Reference). Specific values are stored in simple type variables, while references to objects are stored in variables of reference types.

Java determines the size of each simple type. These sizes do not change with the structure of the machine. The immutability of this size is one of the reasons why Java programs have strong portability.

The following table lists the simple types defined in Java, the number of bits occupied, and the corresponding wrapper classes.

Simple types in table Java

Why ⑵ uses wrapper classes

Take int and Integer as an example. Although they essentially represent a 32-bit integer, they are different Java data types. In fact, the integers used directly in Java are int (in the case of int and Integer), and integer values must be encapsulated into objects with the wrapper Intege corresponding to int only if the data must appear as the identity of the object.

For example, in order to add an integer to the Vector in the java.util package, the integer value must be encapsulated in an Integer instance as follows:

Vector v=new Vector (); int knight 121; v.addElemt (new Integer (k))

In addition, Integer, as the wrapper class corresponding to int, provides many methods, such as the construction method of Integer, the conversion method of Integer to other numerical types, and so on, which are not available in int type data.

2. Conversion between simple Java data types

There are two ways to convert between simple type data: automatic conversion and cast, which usually occurs in an expression or when a method's parameters are passed.

Automatic conversion of ⑴ JAVA data types

Specifically, when a "small" data is operated with a "larger" data, the system will automatically convert the "small" data into "large" data and then perform the operation. When the actual parameter is "small" and the formal parameter data of the called method is larger (if there is a match, of course the matching method will be called directly), the system will automatically convert the "small" data into "big" data, and then call the method. Naturally, for multiple overloaded methods with the same name, they will be converted into the most "close"large" data and called.

These types from "small" to "big" are (byte,short,char)-int--long--float-double. When we say "big" and "small" here, we don't mean the number of bytes occupied, but the size of the range of values.

Take a look at the following example:

The following statement in ① can be passed directly in Java:

Byte b; int iBeb; long lumbago; float fumb; double dumbb

② if the low-level type is char, when converting to a high-level type (integer), it will be converted to the corresponding ASCII code value, such as

Char clockwork; int iTunc; System.out.println ("output:" + I)

Output: output:99

③ for the three types of byte,short,char, they are flat, so they cannot be automatically converted to each other, and the following cast can be used.

Short iTun99; char c = (char) I; System.out.println ("output:" + c)

Output: output:c

If there are methods in ④ object polymorphism:

F (byte x) {. }; f (short x) {. }; f (int x) {. }; f (long x) {. }; f (float x) {. }; f (double x) {. }

There is also: char yawning ordered Aids; so which method will be called by the statement f (y)? The answer is: F (int x) {. Method, because its parameter is "larger" than the actual parameter and is the most "close".

And for the method:

F (float x) {. }; f (double x) {. }

Then, the method called by the statement f (y) is f (float x) {. } .

⑵ forces JAVA data type conversion

You can use cast when converting "large" data to "small" data. That is, you must use the following statement format:

Int n = (int) 3.14159

As you can imagine, this conversion can definitely lead to overflow or loss of precision.

Note:

① when the byte type variable participates in the operation, java promotes the automatic data operation type and converts it to the int type.

Byte b; bread3; b = (byte) (bread3); / / byte must be declared.

② variables with decimals default to type double.

Float f; fallow 1.3f / must declare f.

⑶ wrapper class transition type conversion

In general, we first declare a variable, and then generate a corresponding wrapper class, and we can use the various methods of the wrapper class for type conversion. For example:

① when you want to convert float type to flexible type:

Float f1 returns 100.00f; Float F1=new Float (F1); double d1=F1.doubleValue (); F1.doubleValue () is the method of the Float class that returns the double value type.

② when you want to convert an int to an int:

Double d1 / 1000; Double D1=new Double (D1); int i1=D1.intValue ()

Variables of a simple type are converted to the corresponding wrapper class, and the constructor of the wrapper class can be used. Namely: Boolean (boolean value), Character (char value), Integer (int value), Long (long value), Float (float value), Double (double value)

In each wrapper class, the total visible method is × × Value () to get its corresponding simple type data. Using this method, we can also realize the conversion between different numerical variables. for example, for a double-precision real class, intValue () can get its corresponding integer variable, while doubleValue () can get its corresponding double-precision real variable.

3. Conversion between strings and other types

Conversion of other types of ⑴ to strings

The string conversion method of the ① calling class: X.toString ()

② automatic conversion: X+ ""

③ 's method of using String: String.volueOf (X)

⑵ string as a value, converted to other types

① is first converted to the corresponding wrapper instance, and then the corresponding method is called to convert to other types.

For example, the format of the value of the character "32.1" converting the string type is: new Float ("32.1"). DoubleValue (). You can also use: Double.valueOf ("32. 1"). DoubleValue ()

② static parseXXX method

String s = "1"; byte b = Byte.parseByte (s); short t = Short.parseShort (s); int I = Integer.parseInt (s); long l = Long.parseLong (s); Float f = Float.parseFloat (s); Double d = Double.parseDouble (s)

GetNumericValue (char ch) method of ③ Character

For details, please refer to api.

Conversion between 4.Date classes and other Java data types

There is no direct correspondence between the integer and the Date class, but you can use int to represent the year, month, day, hour, minute, and second, respectively, thus establishing a corresponding relationship between the two. When making this transformation, you can use the three forms of the Date class constructor:

① Date (int year, int month, int date): int type to represent year, month and day

② Date (int year, int month, int date, int hrs, int min): int is used to represent year, month, day, hour and minute.

③ Date (int year, int month, int date, int hrs, int min, int sec): int type represents year, month, day, hour, minute and second

An interesting correspondence between long integers and the Date class is that a time is expressed as milliseconds from 00:00:00 GMT on January 1st, 1970. For this correspondence, the Date class also has its corresponding constructor: Date (long date).

To 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 understand 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 the String class.

This is the end of this article on "sample analysis of Java data type conversion". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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