Knowledge points that must be learned for elementary Java


sample image

Basic composition of Java language

  1. Hello! I am Wei Tiehammer, an ordinary but extraordinary young man. I like to learn and share Java. While teaching others, I gain happiness and consolidate my knowledge.
  2. If you are going to learn Java or have just started learning Java and are not clear about the underlying principles of the language, please be sure to take a look at this article, which is specially formulated for elementary Java.

Java type conversion

The Java basic data types we have learned can be freely converted under the same type of data without error reporting, so is it not the same type? How should they convert?

type coercion

  1. First, let's look at a code block:
public satic void main(String[] args){
    
    
	int a = 1;//创建了一个int类型的常量a,并把1赋值给了a;
		int b = a;//把这个a里面的数据赋值b;
			System.out.println(b);
	}
  • From this we can see that assignment operations can be directly performed between the same data types.
  1. Let's look at another one:
public static void main(String[] agrs){
    
    
		int a = 1;
		byte b = a;//会报错
	}
  • When we store a large data type into a small data type, an error will be reported, because the storage size of the data type is different. Let’s take the integer type as an example. There are four integer types in total, in order of storage from small to large To arrange, the order is byte, short, int, long. See the table below for specific storage information:

Integer Type Size Distribution Table

integer type storage size
byte 8bit
short 16bit
int 32bit
long 64bit

Knowledge point explanation

  1. The forced conversion of this data is like when you eat rice. You put one in a pot and the other in a bowl. If you want to change the big rice in the pot to a small bowl, there will be extra However, if there is any excess, just don’t want it, because it can’t fit, then the rice in your small bowl now cannot be said to be all rice in the basin, because some of it has been poured out.
  2. Our data is like rice. When we convert the data, if the large data is stored in the small one, an error will be reported, and the conversion can also be forced, but the data will change, and the small one is stored in the large one. No problem.
  3. But some people will ask, the same number 1 can be directly assigned to int or baty, which means that they can all hold the number 1, but why can't it be converted? In fact, the reason is very simple. If the number 1 occupies a redundant space, it will be filled with null (the default value), so if there is no storage, it does not mean that the storage space does not exist.

Assignment operation between spaces with different data types

  1. Basic types can be converted directly. There are two ways. One is automatic conversion, where the smaller storage space is assigned to the larger one, and the other is forced conversion, where the larger storage space is assigned to the smaller one.
  2. Reference types can also be converted directly. There are four conversion methods: automatic, mandatory, upcast, and downcast.
  3. It is not possible to convert directly between reference types, but it can be converted indirectly, and the conversion is performed through the wrapper class/encapsulation class.

example

public static void main(String[] args){
    
    
		int a = 1;
		byte b = (byte)a;
	}

Note: Conversion can only occur between the same large data types

Guess you like

Origin blog.csdn.net/m0_65909361/article/details/129510946