Data Types in JSP and mutual conversion

Source: Internet
Author: User

Data Type

There are four basic types:
Int data types include: byte (8 bits), short (16 bits), INT (32 bits), long (64 bits ),
Float data types include: single precision (32 bits float) and double precision (64 bits double)
Values of boolean type variables include true and false.
Char data types include: Unicode characters, 16 characters
Corresponding class types: integer, float, Boolean, character, double, short, byte, long

Conversion Principle

Conversion from low-precision to high-precision
Byte, short, Int, long, float, double, char
Note: When two char operations are performed, they are automatically converted to the int type. When char and other types are operated, they are automatically converted to the int type before other types are automatically converted.

Convert basic types to class types

Forward conversion: Use the class wrapper to generate a new class type variable.
Integer A = new INTEGER (2 );
Reverse conversion: using the class package
Int B = A. intvalue ();

Class type conversion to string

Forward conversion: because each class is a subclass of the object class, And all object classes have a tostring () function, you can use the tostring () function to convert it.
Reverse conversion: a new class type variable is generated through the class package.
Eg1: int I = integer. valueof ("123"). intvalue ()
Note: In the above example, a string is converted into an integer object, and then the intvalue () method of this object is called to return the corresponding int value.
Eg2: Float F = float. valueof ("123"). floatvalue ()
Note: In the above example, a string is converted into a float object, and then the corresponding float value is returned by calling the floatvalue () method of this object.
Eg3: Boolean B = Boolean. valueof ("123"). booleanvalue ()
Note: The preceding example is to convert a string into a Boolean object, and then call the booleanvalue () method of this object to return its corresponding Boolean value.
Eg4: Double D = double. valueof ("123"). doublevalue ()
Note: The preceding example is to convert a string into a double object, and then call the doublevalue () method of this object to return its corresponding double value.
Eg5: Long L = long. valueof ("123"). longvalue ()
Note: In the above example, a string is converted into a long object, and then the longvalue () method of this object is called to return the corresponding long value.
Eg6: Char = character. valueof ("123"). charvalue ()
Note: The preceding example is to convert a string into a character object, and then call the charvalue () method of this object to return the corresponding char value.

Conversion from basic type to string
Forward conversion:
For example, int A = 12;
String B; B = a + "";

Reverse conversion:
Package through class
Eg1: int I = integer. parseint (123 ")
Note: This method can only be used to convert strings into integer variables.
Eg2: Float F = float. valueof ("123"). floatvalue ()
Note: In the above example, a string is converted into a float object, and then the corresponding float value is returned by calling the floatvalue () method of this object.
Eg3: Boolean B = Boolean. valueof ("123"). booleanvalue ()
Note: The preceding example is to convert a string into a Boolean object, and then call the booleanvalue () method of this object to return its corresponding Boolean value.
Eg4: Double D = double. valueof ("123"). doublevalue ()
Note: The preceding example is to convert a string into a double object, and then call the doublevalue () method of this object to return its corresponding double value.
Eg5: Long L = long. valueof ("123"). longvalue ()
Note: In the above example, a string is converted into a long object, and then the longvalue () method of this object is called to return the corresponding long value.
Eg6: Char = character. valueof ("123"). charvalue ()
Note: The preceding example is to convert a string into a character object, and then call the charvalue () method of this object to return the corresponding char value.

Supplement String Conversion

String. valueof (123 );

Or use this method: system. Out. Print (12 + "");

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.