Chapter 4 Introduction to data type conversion, operators and methods

1, Data type conversion

The data of participating computers in Java programs must ensure the consistency of data types. If the data types are inconsistent, type conversion will occur.

1.1 automatic conversion

What data type will result from the addition of an int variable and a byte variable?

int i = 1;
byte b = 2;

As a result of the operation, the type of the variable will be int, which is the phenomenon of automatic type conversion of data types.

  • Automatic conversion: type with small value range is automatically refreshed to type with large value range.
public static void main(String[] args){
	int i = 1;
	byte b = 2;
	//byte x = b + i;//   report errors
	//Int type and byte type operation, and the result is int type
	int j = b +i;

Schematic diagram of conversion principle

Byte type memory occupies 1 byte, which will be promoted to int type during operation with int type, and 3 bytes will be automatically supplemented. Therefore, the calculated result is still int type.

Conversion rules

Types with a small range are promoted to types with a large range. byte, short and char operations are directly promoted to int.


1.2 forced conversion

What happens when 1.5 is assigned to a variable of type int? Failed to generate compilation. It must be unable to assign value.

int i = 1.5 / / error

Double type memory is 8 bytes, and int type memory is 4 bytes. 1.5 is double type, and the value range is greater than int. It can be understood that double is an 8-liter kettle and int is a 4-liter kettle. You can't put the water in the big kettle directly into the small kettle.

If you want to assign a value successfully, you can assign a value only by casting a double type to an int type.

  • Forced type conversion: converts a type with a large value range into a type with a small value range.

In comparison, automatic conversion is performed automatically by Java, and forced conversion needs to be performed manually by ourselves.

Conversion format:

Data type variable name = (data type) transferred data value;

Assign 1.5 to int type, and modify the code to:

//double type data is forcibly converted to int type, and the decimal point is directly removed.
int i = (int)1.5;

Similarly, when a short type is added to 1, we know that the type will be promoted, but if we want to assign the result to a short type variable, we need to cast.

public static void main(String[] args){
	//short type variable, 2 bytes in memory
	short s = 1;
		Compilation failure occurred
		s When doing operations with 1, 1 is of type int, and s will be promoted to type int
		s+1 The result after is of type int, and an error occurs when assigning the result to type short
		short Memory 2 bytes, int type 4 bytes
		int must be cast to short to complete the assignment
	s = s + 1; //Compilation failed
	s  = (short)(s+1); //Compilation succeeded

Schematic diagram of conversion principle

Strong attention

  • Converting a floating point to an integer directly cancels the decimal point, which may cause loss of data accuracy.
  • int is forcibly converted to short and 2 bytes are cut off, which may cause data loss.
//Defined as the maximum value in the short range
short s = 32767;
//After the operation, it is forced to convert. After cutting off 2 bytes, there is an uncertain result
s = (short)(s+10);

1.3 ASCII coding table

public static void main(String[] args){
	//Character type variable
	char c = 'a';
	int i = 1;
	//Character type and int type calculations
	System.out.println(c+1);//The output is 98

There are binary 0 and 1 data inside the computer. How can the computer directly recognize human characters? The concept of coding table is generated.

  • Coding table: it is to form a table by corresponding human words with a decimal number.

During the calculation of char type and int type, char type characters first query the coding table to get 7, and then sum with 1, and the result is 98. Char type promoted to int type. Promote char type to int type. Char type memory 2 bytes, int type memory 4 bytes.

2, Operator

2.1 arithmetic operators

In Java, integers use the above operators. No matter how they are calculated, they will not get decimals.

public static void main(String[] args){
	int i = 1234;
	System.out.println(i/1000*1000);//The result is 1000
  • ++Operation, the variable increases by 1. On the contrary, – operation reduces the variable by 1, and the usage is consistent with + +.
    • Independent operation:

      • When variables are operated independently, there is no difference between pre + + and post + +.
      • Before variable + +: for example, + + i.
      • After variable + +: for example, i + +.
    • Mixed operation:

      • Together with other variables, the former + + and the latter + + are different.
      • Before variable + +: add 1 to variable a, and assign the result after adding 1 to b, that is, a calculates first, and the results of a and b are both 2.
public static void main(String[] args){
	int a = 1;
	int b = ++a;
	System.out,println(a);//The result is 2
	System.out.println(b);//The result is 2
  • After variable + +: variable a assigns its own value 1 to variable b. at this time, the value of variable b is 1, and variable a adds 1. The result of a is 2 and the result of b is 1
public static void main(String[] args){
	int a = 1;
	int b = a++;
	System.out.println(a);//The result is 2
	System.out.println(b);//The calculation result is 1
  • +Operation of symbol in string:
    • +When a symbol encounters a string, it indicates the meaning of connection and splicing.
    • The result of "a+b" is "ab", connecting the meaning
public static void main(String[] args){
	System.out.println("5+5="+5+5);//Output 5 + 5 = 55

2.2 assignment operator

  • The assignment operator is to assign the value on the right of the symbol to the variable on the left.
public static void main(String[] args){
	int i = 5;
	i+=5;//The calculation method is i=i+5. Add 5 to variable i first, and then assign variable i
	System.out.println(i);//The output is 10

2.3 comparison operators

  • A comparison operator is an operation that compares two data. The result of the operation is a Boolean value of true or false.
public static void main(String[] args){

2.4 logical operators

  • Logical operator is an operator used to connect two Boolean results. The operation results are Boolean values true or false
public static void main(String[] args){
	System.out.println(true && true);//true
	System.out.println(true && false);//false
	System.out.println(false&& true);//false, not calculated on the right
	System.out.println(false|| false);//false
	System.out.println(false|| true);//true
	System.out.println(true || false);//true, not calculated on the right

2.5 ternary operation

  • Ternary operator format:

Data type variable name = boolean type expression? Result 1: result 2;

  • Ternary operators are evaluated:
    • The result of boolean type expression is true, and the overall result of ternary operator is 1, which is assigned to variable.
    • The result of Boolean expression is false, and the overall result of ternary operator is result 2, which is assigned to variable.
public static void main(String[] args){
	int i  = (1==2? 100 : 200);
	int j = (3<=4? 500 : 600);

3, Introduction to methods

3.1 general

  • Method: extract a function and define the code in a brace to form a separate function.
    When we need this function, we can call it, which not only realizes the reusability of code, but also solves the phenomenon of code redundancy.

3.2 definition of method

  • Define format:
Modifier return value type method name (parameter list){
  • Definition format interpretation:
    • Modifier: the current fixed writing method is public static.
    • Return value type: such as void
    • Method name: name the method we define, which meets the specification of identifier and is used to call the method.
    • Parameter list: can have parameters or no parameters
    • Return: the method ends. Because the return value type is void, the return in the method braces may not be written.
  • give an example:
public static void methodName(){
	System.out.println("This is a method");

3.3. Method call

After the method is defined, the method will not run by itself and must be called to execute. We can call the method defined by ourselves in the main method. In the main method, write the name of the method to be called directly.

public static void main(String[] args){
	//Call the defined method
//Defines a method that is called by the main method
public static void method(){
	System.out.println("Self defined methods need to be main Call run");

Call exercise

Extract the ternary operator code into a custom method and call.

public static void main(String[] args){
	//Call the defined method operator
//Define a method in which ternary operators are defined
public static void operator(){
	int i = 0;
	i = (1==2? 100 : 200);
	int j = 0;
	j = (3<=4? 500 : 600);

3.5 precautions

  • Precautions for method definition:
    • Methods must be defined outside of methods in a class
    • A method cannot be defined inside another method
public class Demo{
	public static void main(String[] args){
	//If it is written correctly, methods can be defined outside the main method in the class
	public static void method(){}
public  class Demo{
	public static void main(String[] args){
		//Wrong writing. One method cannot be defined inside another method
		public static void method(){}

Tags: Java

Posted on Sat, 20 Nov 2021 08:39:15 -0500 by bughunter2