This article is for you to understand 2 questions
- What data types in Java are available for storing data?
- 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
- Numerical
1月
In the1
60小时
In the60
2.5
Days in2.5
- Non-numeric
,
年
月
小时
- Numerical
- Integral type
- Floating point Type
- Non-numeric
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?";
String
Type is used to store strings, strings can be spliced but not used for mathematical operations
- You can also use some
转义字符
of these characters to represent special symbols like spaces, line breaks
\n
Can be used to represent a newline, and then the content will start another line
\t
Can 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);
boolean
A 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
- 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.
- 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.
- What is the result of the following operation? Why?
int a=5;int b=2;int c=a/5;System.out.println(c);
- How did the code in the previous exercise make the results of the calculations more accurate?
- 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