05-Data type and type conversion

Source: Internet
Author: User

This article is for you to understand 2 questions

    1. What data types in Java are available for storing data?
    2. How is the data type in Java converted?

In the previous article we learned how to use variables, like this to store an integer int age=10; , you can write a line in the development tool such code int age=10.5; will find the development tool error, because the data type in the variable is also not easy to use.

Data type

Let's start by looking at the following set of data

If you spend 2 hours a day on the traffic
January = 60 hours = 2.5 days,
1 years = 730 hours = 30 days,
50 = 36,500 hours = 1520 days = 4 years

This data can be divided into the following 2 categories, Java also contains the 2 major categories

    1. Numerical
      1月In the1
      60小时In the60
      2.5Days in2.5
    2. Non-numeric
      ,
      小时
    • Numerical
      • Integral type
        • Byte
        • Short
        • Int
        • Long
      • Floating point Type
        • Float
        • Double
    • Non-numeric
      • Char
      • Boolean
      • String

        Numeric type

        Here's a look at a detailed description of each data type

Data Type size range of Values Introduction
Byte 1 bytes 8 bits -128 ~ +127 Byte type this range can store the age of the average person
Short 2 bytes 16 bits -32768~+3276 Short-integer
Int 4 bytes 32 bits -2147483648) ~+2147483647 There's no problem with the number of people in China, but it's not enough to save the planet.
Long 8 bytes 64 bits -2^63 ~ + 2^63-1 The number of people who save the earth is more than sufficient
Float 4 bytes 32 bits Single-precision floating-point numbers store decimals
Double 8 bytes 64 bits Double-precision floating-point number

Knock a knock:

public class DemoType {    public static void main(String[] args) {        //int类型使用        int numa=10+1;        System.out.println(numa);//输出11        short numb=20;        //使用long类型时的值需要使用L作为后缀        long time=9223372036854775807L;    }}

Use a case-insensitive L as a suffix mark with integer type when using long
Use a different integer without the suffix symbol, and note that an integer of the assigned value does not exceed the type range.

Knock a knock:

public class DemoFloat {    public static void main(String[] args) {        float money=33.6f;        double length=55.07;    }}

Use a single-precision floating-point number f as a suffix, because any decimal will be considereddouble
Recommended use of double type to prevent loss of precision in daily use

Non-numeric character char

Only one character can be stored, and the value needs to be enclosed in a pair of single quotes, and only one character can be placed in single quotes

Knock a knock:

char sex='男';
String type

Used to store a string of characters that must be enclosed in a pair of double quotes, and note that string is not a basic data type, but another concept in Java .

Knock a knock:

String username="极客大全";String question="What's your name?\nHow old are you?";
  1. StringType is used to store strings, strings can be spliced but not used for mathematical operations
  2. You can also use some 转义字符 of these characters to represent special symbols like spaces, line breaks
    \nCan be used to represent a newline, and then the content will start another line
    \tCan represent a tab character ( Tab key)
Boolean-Type Boolean

The result of storing logic, such as: The elephant's result is true compared to the People's Congress ( true ) The result is False ( false ).

Knock a knock:

boolean isbig=(1>10);System.out.println("isbig:"+isbig);boolean isequal=(100==3);System.out.println("isequal:"+isbig);boolean flag=false;System.out.println("flag:"+flag);

booleanA variable of type can store only two values true orfalse
You can also save the logical comparison result (also true or false )

Data type conversion implicit type conversion

Two flowers one months ago weighed 97.5 pounds, now more than one months ago just add 2 pounds, two flowers is now how many pounds? This is a relatively simple mathematical problem, the correct result is 99.5 pounds, below we use code to simulate again.

Knock a knock:

public class DemoClac {    public static void main(String[] args) {        double weight=97.5;//一个月前的体重        int add=2;//增加的体重        double sum=weight+add;        System.out.println(sum);//输出和    }}

The add variable is an integer type, and weight is a decimal, and the two are added to the double type
This is what happened.隐式类型转换

隐式类型转换is an automatic type conversion that can occur with the following two rules

    1. If one operand is of type double, the entire expression can be promoted to a double type

Like the example above, this rule is fulfilled.

    1. Conditions for automatic type conversions: Two types to be compatible, the target type greater than the source type

Example 1: double a=1; 1 is integral type, double type is greater than integer, that is, auto-convert

Example 2:

char chr=97;System.out.println(chr);//输出字符 a

The char type is compatible with the int type, and each integer is mapped to a character when it is stored in the char type variable, more BaiduASCII

Forcing type conversions

Knock a knock:

int sum=55.3+22;System.out.println(sum);

We want an integer, but the above code will run with an error:cannot convert from double to int

What is this for? Based on the implicit type conversions previously learned, when an operand in an expression is of type double, the entire expression is promoted to double, and the type conversion must be explicitly coerced.

Knock a knock:

int sum=(int) (55.3+22);System.out.println(sum);

This time the operation was successful and the result of the operation was77

Forcing type conversions is often converted from a wide-width type to a small-width type, which results in a loss-of-value precision

Give it a try.
    1. What is the result of the following operation? Why?
int a=5;int b=2;int c=a/5;System.out.println(c);
    1. How did the code in the previous exercise make the results of the calculations more accurate?
    2. Random decimal numbers are generated in the footage below, and the random integers are modified to generate 1-10
public class DemoRandom {    public static void main(String[] args) {        double random=Math.random();        System.out.println(random);    }}

Encourage you to post your answers in your message, communicate with others, and search for related questions on Baidu

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.