current position:Home>Java foundation -- data type conversion

Java foundation -- data type conversion

2022-01-26 22:58:18 #HashMap#

1.Java data type

1.1 Java Data type classification

1.1.1 java Programming language is a strongly typed language , Of course, there are weakly typed languages , such as JavaScript
Strongly typed definition language : Language to enforce data type definitions . in other words , Once a variable is assigned a data type , If not forced , Then it will always be this data type . for instance : If you define an integer variable a, Then it's impossible for the program to a Treat as string type . Strongly typed definition language is a type safe language
Weak type definition language : Languages in which data types can be ignored . It's the opposite of a strongly typed definition language , A variable can assign values of different data types .
 Insert picture description here
TIPS: 1b=1 position
1B( byte )=8 position
1KB( One thousand bytes )=1024B

i) integer
 Insert picture description here
ii) floating-point
 Insert picture description here
iii) Character
 Insert picture description here
about char Characters need to be noted that , Often use escape characters , Such as \t tabs ,、\n Indicates a newline character, etc
It uses \t Shi Shiyi 8 Multiple complement of , for example :

char ch = '\t';
System.out.println("abcdefghabcdefgh");
System.out.println(ch+"abcdefghabcdefgh");// The front is empty 8 position 
System.out.println("abcdefg"+ch+"habcdefgh");// because "abcdefg" Yes 7 position , A filling 1 position 

 Insert picture description here
iiii)boolean type
 Insert picture description here
Common conversion problems
(1)

float num =3.14;//3.14 stay java Default is double
System.out.println(num);// Display error 

 Insert picture description here
The reason for the error report is because java The middle decimal is... By default double type , If float The accuracy will be lost ( Later, we will talk about the specific reasons )
(2)

Test01
int num = 10;
byte n = (byte)num;// Cast is used here to byte Sure  

Test02
int num = 128;
byte n = (byte)num;// The result is -128, The reason is beyond byte Table number range (-128-127), Here you can see the original code, inverse code and complement code 

(3) The relationship between big data types and small data types ( a key )
 Insert picture description here
example :

Test01
int num = 10;
float f = 'A'+num+12+3.14F+12.12;// The data type obtained here depends on the large type , Obviously double

Test02
char ch = 'A'+num// Go to int Type dependence 

2. The storage of integers in a computer

 Insert picture description here

copyright notice
author[#HashMap#],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262258152961.html

Random recommended