Java Fundamentals 02 Variables, Identifiers and Type Conversion

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Preface

We continue to learn the basics of Java, the content of this chapter is variable and identifier and data type conversion

Identifier

What is an identifier

Identifiers are various names in Java, defined by programmers, including:

  • Class name
  • Method name
  • variable name
  • Package names
  • Constant name
    ...

Identifier naming rules

Identifiers must be defined according to certain rules. Identifiers that violate the naming rules will cause compilation errors
. The naming rules for Java identifiers are:

  1. Must be composed of English letters, numbers, underscores, and $
  2. Cannot start with a number
  3. Cannot use keywords in Java
    Insert picture description here

Identifier naming convention

The naming convention is not mandatory, but a good naming convention is very important to the readability and maintainability of the code.
Java naming conventions include:

  1. See the name to know the meaning, need to have meaningful English words
  2. Variable name and method name, camel case name, composed of one or more words, the beginning of the first word is lowercase and the word after the beginning is uppercase, such as goodsPrice, person, myFirstPersonAge
  3. Class name, similar to camel case name, the beginning of each word is capitalized, such as: HelloWorld, MyFirstHomework
  4. Package name: All words are connected by. In lowercase, domain name. company name. project name. module name, such as: com.sun.oa.login
  5. Constant name, all words are capitalized, separated by underscores, such as MAX_VALUE

Exercise:
Please determine which are legal identifiers:

HelloWorld
username2
user_name
_userName
$abc_123 
2UserName
user#Name
Hello  World
java
class

variable

The concept of variables

Variables are spaces in the memory of the JVM, used to store various data in the Java program, and the data in the variables can be modified at any time.
For example, when executing the game, the hero's name, blood volume, magic value, and level can all be saved with variables.
Insert picture description here

Use variables

Three elements of variables

  1. variable name
  2. type of data
  3. Initial value

Methods of defining variables

  1. Define variables and initialize at the same time.
    Data type variable name = initial value;
    such as: int hp = 100;
  2. Define the variable first, and then initialize the
    data type variable name;
    variable name = value;
    int hp;
    hp = 100;
  3. Define multiple variables of the same type at the same time
    Data type variable name 1 = value, variable name 2 = value;
    int hp = 100, mp = 300;

Variable scope

Scope is the range in which the variable can work

  • Local variable The
    scope of a variable defined in a method is inside the method where the variable is defined. Local variables must be initialized before they are used.
  • Global variables The
    scope of variables defined outside all methods in a class is the entire class. Global variables will have default values ​​if they are not initialized.
public class Demo01 {
	
	int x = 9; //全局变量
	
	public static void main(String[] args) {
		//局部变量
		String name = "貂蝉";
		int hp = 100;
		int mp = 200;
		System.out.println("英雄的名字是:" + name);
		System.out.println("英雄的血量是:" + hp);
		System.out.println("英雄的魔法值是:" + mp);
	}

}

What's wrong with executing the following code?

public class Demo02 {
	public static void main(String[] args) {
		int num1;
		int num2 = num1 + 1;
	}
}

Variable data type

Insert picture description here

note:

  1. When assigning to long type variable, add L or l after the number, it is recommended to use L
    long num = 100L;
  2. When assigning a variable of type float, add f or F after the floating point number, otherwise the default is double type, and the assignment error
    float num = 10.5f;
  3. The value of char is enclosed in single quotes
    char c ='A';

Type conversion

Data type conversion can be carried out between different numeric types.
Numerical types are:
byte\short\int\long\float\double\char
The range of values ​​of numeric types from small to large is:
Insert picture description here
Type conversion:

  1. Automatic type conversion The conversion
    from a small type to a large type
    is done automatically by the compiler
long num = 200; //200属于int类型,编译器自动会将200转换200L,赋值给long类型的变量num。
byte num2 = 100;//100在byte取值范围内,100作为byte保存。
int num1 = num2; //byte -> int
double price = 2500; //int -> double
  1. Coercive type conversion The conversion of
    large types to small types
    needs to be done manually
数据类型 变量名 = (数据类型)值;

Note: Casting may lose precision

/*
对于整数值编译器会看做char数值进行处理,
对于变量编译器不会进行处理,就需要强制类型转换
*/
int num3 = 99;
char char1 = (char)num3;
/*
对于short类型,如果赋值的数字在-32768~32767之间,编译器会作为short数字进行处理
如果超过该范围,就会作为int数字处理
*/
short num5 = (short)50000;

Summary: Type conversion will have an impact on program performance.

Exercise

找出下面代码中不能通过编译的代码
int n1 = 20;
int n2 = 20.5;
int n3 = 100L;
float f1 = 20;
float f2 = 5.5;
float f3 = 3.3f;
double d1 = 15;
double d2 = 33.3f;
double d3 = 20L;
d1 = n1 + f1;  //表达式的类型由表达式中最大的数据类型决定
f1 = d1;

keyboard input

To input values ​​from the keyboard to assign values ​​to variables, you need to use the Scanner class (scanner)

//导入Scanner
import java.util.Scanner;
public class Demo03 {
	public static void main(String[] args) {
		//创建Scanner对象
		Scanner input = new Scanner(System.in);
		//输入字符串
		String name = input.next();
		//输入整数
		int age= input.nextInt();
		System.out.println("英雄的名字是:" + name);
		System.out.println("英雄的年龄是:" + age);
	}

}

End

Homework:
1. Enter the name, attack power, blood volume, level and other information of the game character, and output it.
2. Enter two integers and exchange the values ​​of the two variables.
After you are done, you can send the code to the comment area to discuss together


If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112217062