Java Fundamentals Hardening Data type conversions in 22:java

Source: Internet
Author: User

Data type conversions:

(1). Automatic conversion

Low-level variables can be converted directly to advanced variables, which are called automatic type conversions. Like what:

byte b; int b; long b; float B; double b;

The above statement can be passed directly in Java.

(2). If the low-level type is char, the conversion to the advanced type (int) is converted to the corresponding ASCII value, for example:

char c = ' c '; int i = C; System.out.println ("Output:" +i);

Output: output:99

(3). For Byte/short/char three types, they are the same level, so you cannot automatically convert types to each other, and you can use forced type conversions.

Short i=99; Char c= (char) i; System.out.println ("Output:" +c);

Output: Output:c

(4). Advanced type conversions to low-level types can cause a loss of precision.

(5). Packaging transition type conversion (to compensate for the loss of accuracy in 4)

Java has 6 packaging classes are boolean,character,integer,long,float and double respectively;

String and date itself are classes, and there is no wrapper class.

e.g: float--->double

float f1 = 100.00f;

float F1 = new float (F1);

Double D1 = F1.doublevalue ();

e.g: double--->int

Double D1 = 100.00;

Double D1 = new double (d1);

int i1 = D1.intvalue ();

(6) Conversion of string types to other data types

Class Test {

public static void Main (String args[]) {

int i1 = 10;

float f1 = 3.14f;

Double D1 = 3.1415926;

Integer I1 = new Integer (i1);

float F1 = new float (F1);

Double D1 = new double (d1);

String SI1 = i1.tostring ();

String sf1 = F1 . toString ();

String SD1 = D1. toString ();

System.out.println ("SI1" +SI1);

System.out.println ("sf1" +SI1);

System.out.println ("sd1 " +SI1);

}

}

(7) Convert character type directly to other data types as numeric values

Using the character getnumericvalue (char ch) method

Java Fundamentals Hardening Data type conversions in 22:java

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.