Article Directory
foreword
literal constant
Literal constants are fixed quantities during program execution.
Classification of literal constants:
1. String constants: enclosed in parentheses, such as "12345", "hello", "Hello"
2. Integer constants: numbers written directly in the program (note that there is no decimal point), such as: 100, 1000
3. Floating point numbers Constants: decimals written directly in the program, such as: 3.14, 0.49
4. Character constants: individual characters enclosed in single quotes, such as: 'A', '1'
5. Boolean constants: only two types: true and false
6. Empty constant: nul
The boxes above are all literal constants.
Data Types and Variables
Note:
In java, whether it is on a 32-bit system or a 64-bit system, int occupies 4 bytes, and long occupies 8 bytes
Features are: portability, cross-platform
Note:
Both integer and floating point types are signed
Wrapper classes and scopes for variables
scope
Notice:
Local variables must be initialized, otherwise an error will be reported
The correct way of writing is as follows:
It is necessary to clearly write the value of b
integer variable
byte
Size: One byte
Wrapper class: Byte
class Test{
public static void main(String[] args) {
byte b=77;
System.out.println(b);
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
}
}
int
Size: 4 bytes
Wrapper class: Integer
class Test{
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
Note: When assigning a value to a literal constant, do not exceed the range that the variable can represent, otherwise an error will be reported
short
Size: 2 bytes
Wrapper class: Short
class Test{
public static void main(String[] args) {
short a=7;
System.out.println(a);
System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
}
long
Size: 8 bytes
Wrapper class: Long
Add L after the initial value of the long integer variable, it is best not to write l, it is easy to see it as 1
floating point variable
double precision floating point type double
Size: 8 bytes
1.
The value of int type divided by int type will not be a decimal (int will automatically omit the decimal)
If you want to get a decimal, use the double type
or this:
Decimals do not have a very precise number, there may be several or even dozens of decimal places after the decimal point
Precautions:
single-precision floating-point float
Size: 4 bytes
The difference from double: f is added after the value of flaot, indicating that it is a single-precision floating-point type, which is 4 bytes.
character variable
char
Size: 2 bytes
In java, char is 2 bytes, and Chinese characters are actually 2 bytes, so char can store Chinese characters.
class Test{
public static void main(String[] args) {
char a='A';
char b='吴';
System.out.println(a);
System.out.println(b);
}
}
Summary:
Java uses single quotation marks to enclose a single letter to represent a character literal value.
The c language uses ASCII code values to represent characters, while java uses Unicode to represent characters, so the character size is two bytes. Java can represent more types of characters.
Boolean variable boolean
Size: Not specified how big
Precautions:
class Test{
public static void main(String[] args) {
boolean flg =true;
System.out.println(flg);
}
}
The wrapper class of boolean is Boolean
type conversion
automatic type conversion (implicit)
When a value with a small data range is assigned to a value with a large data range, the type with a small range will automatically be converted into a type with a large range.
Summarize:
- Smaller bytes can be assigned to larger bytes, and will be automatically converted to the type with larger bytes.
- A large byte cannot be assigned to a small byte. Pour a large bowl of water into a small bowl of water and it will overflow, which will lead to data loss here. (not safe!)
cast (explicit) (deprecated)
There is also a way to assign a large byte to a small byte,
which is to convert a large byte to a small byte.
But this approach is a bit risky
and can lead to data loss.
The boolean type cannot be converted to other types.
Precautions:
Type promotion in operations
The small type and the large type are added, and the large type is used to receive the result of the addition
Int type and long type are added, int will be promoted to long type, so long should be used to receive the addition result
class Test{
public static void main(String[] args) {
int a =10;
long b=100L;
long c=a+b;
System.out.println(c);
}
}
The addition of types less than 4 bytes will be upgraded to 4 bytes during the operation, so the int type should be used to receive the addition result
byte type and byte type addition
class Test{
public static void main(String[] args) {
byte f =10;
byte f2=100;
int f3=f+f2
System.out.println(f3);
}
}
Summarize
The above is about the content of type data, and I am even more impressed after writing it again.