1. Variables
Three elements of a variable: data type, variable name, variable value
Naming rules for variables:
1. Package name all letters are lowercase.
2. First capitalization of class name
3. The first letter of the variable is lowercase, and subsequent words with meaning are capitalized. For example Stuname stuage
4. The first letter can only be one of letters, underscores and $ symbols, and subsequent content may be numbers.
Note: The variable name cannot be duplicated with the keyword.
Two ways to define variables
001. Declare first, then assign value
int num;
num=5;
002. Simultaneous and assigned value of declaration
int num=5;
The local variable title may not be been initialized
The local variable "title" is not initialized (assigned value)
2. Data type
Data types are divided into: basic data types and reference data types
The basic data types are divided into: Numeric rows (integer type: Byte, short, int, long, floating-point type: float, double,), character Char, Boolean-Boolean,
Note: The char type can only store a single character, and if you want to save a null character, make a space.
Reference data types: classes, interfaces, arrays, and so on.
3. The concept of an expression
The formula that joins the operand with an operator is called an expression a+b
4. Operators
Assignment operator: =
For example: num=3, assign the right value to the left variable
Arithmetic operators: +,-,*,/,%,
For example: Double wunian=benjin+ (benjin*0.036);
Self-increment operator: ++,--
int num=5;
num++;//is equivalent to num=num+1;
5. Type conversion
Type conversions are primarily applied when arithmetic operations and assignment operators are used. The higher the number of stores, the higher the level of the type
Conversion Mode: Automatic type conversion/forced type conversion
Automatic conversion:
BYTE b=7;
int I=b
Cast:
int num=333;
Byte= by Num; //Error
byte by = (byte) num; That's right
Short Sh=num; //Error
Short sh= (short) num; That's right
Usage of 6.Scanner
Use scanner in three steps:
01. Import the package imports Java.util.Scanner first; (Go to The Mall)
02. Create a Scanner object Scanner input=new Scanner (system.in);
02. Start the Scan button String name=input.next ();
How do I receive the content that the user entered from the keyboard?
Passer-by says: How do you get int?
Professional analysis: Input.nextint ()
Passers-by: how do I receive a double type?
Professional analysis: input.nextdouble ();
How do I receive a string?
Professional analysis: Input.next ()
For example:
Enter the four-digit membership number;
Package cn.happy.one;
import java.util.*; //scanner can be replaced with *,
public class Huiyuan {
public static void Main (string[] args) {
Scanner input=new Scanner (system.in);
System.out.println ("Please enter four-digit membership card number:");
int Kahao;
kahao=input.nextint ();
SYSTEM.OUT.PRINTLN ("Membership card number is:" +kahao);
int gewei= (KAHAO%10);
int shiwei= (KAHAO/10%10);
int baiwei= (KAHAO%100/10);
int qianwei= (kahao/1000);
int sum= (Gewei+shiwei+baiwei+qianwei);
String jl= (sum>20)? ("Ferrari"):("nothing");
System.out.println ("The number of individual digits is:" +sum);
System.out.println ("Congratulations card number:" +kahao+ "the person you won! Prize:" +JL);
Data type operators and variables