Java Fundamentals-Conversions between data types

Source: Internet
Author: User

    • Java data types fall into three main categories, namely, Boolean , character , and numeric . The numerical type is divided into integral type and floating point type. The basic data types of Java (8 types) are Boolean Boolean (1 bytes), character char (2 bytes), integer byte (1 bytes), short (2 bytes), int (4 bytes), Long (8 bytes), float float (4 bytes), Double (8 bytes). In addition, two kinds of variables, string and date, are often used in programming.
    • The conversion of Java data types is generally divided into three types: conversion between simple data types, conversion of strings to other data types, conversion of other useful data types.
    1. conversion between simple data types                             &N Bsp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp        in Java, Integer, real, and character types are considered simple data type from low to Advanced (Byte, short, char)---> int---> Long- -Folat---> Double . Conversions between simple data types can be divided into: low-to-advanced automatic type conversion, advanced to low-level coercion type conversion, wrapper class transition type conversion.
    • Automatic type conversion : Low-level variables can be directly converted to advanced variables, which are automatic type conversions. Note: if the low-level type is char, converting to the advanced type translates to the corresponding ASCII code value .
      They are the same level for Byte, short, and char three types, so they cannot be converted automatically , but you can enforce type conversions . Eg:short s = 99; char C = (char) s; C represents the character ' C '.
    • coercion Type conversions: When you convert an advanced variable to a low-level variable, you need to use the coercion type conversion, which can result in a drop in overflow or precision.
    • wrapper class Transition type conversion : Java wrapper class is a simple type can be directly represented as a class, Java a total of 6 wrapper classes , namely, Boolean, Character, Integer, Long, float, double, literally can be seen to correspond to a Boolean, char, int, long, float, double.
      When converting between simple data types (auto-convert or cast), you can use wrapper classes for intermediate transitions. In general, a variable is declared first, then a corresponding wrapper class is generated, and the various methods of the wrapper class are used to convert the type.
      float f = 100.00fnew  float (f); double D1 = F1.doublevalue ();

2. Conversion of strings to other data types

By looking at the member methods provided by each class in the class library, it can be seen that almost all classes derived from the Java.lang.Object class provide the ToString () method, which can be converted to a string. the wrapper class for the data type can use the ToString () method to convert the corresponding data into a string .

3. Convert character type directly to other data types as numbers

The conversion of a character variable to a numeric variable actually has two correspondence: one is to convert it to the corresponding ASCII value, and the other is a conversion relationship, such as converting ' 4 ' to 4 instead of ASCII value. For the second type of conversion, you can use the character getnumericvalue (char ch) method.

Java Fundamentals-Conversions between data types

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.