Organize constants, variables and data types

Install IDEA first before studying. Tutorials can be searched online.

" Constant ": Refers to some values ​​that are allowed to appear directly in the "Code". For example: 10, 5, 3.14, "Hello" ...
Classification of constants:
1). Integer
2). Decimal
3). Character: a character enclosed in a pair of single quotes
4). Boolean: true or false-yes Two predefined values ​​of Java, indicating: true / false
5). String: Multiple characters enclosed in a pair of double quotes.

public class Demo {
public static void main(String[] args) {
//1.整数
System.out.println(10);
System.out.println(20);
//2.小数
System.out.println(777.888);
System.out.println(12.34);
//3.字符
System.out.println('b');
System.out.println('.');
System.out.println('你');
System.out.println('\'');
//4.布尔
System.out.println(true);
System.out.println(false);
//5.字符串
System.out.println("字符串");
System.out.println("你好");
} }

" Variable ": It is a named memory space in the memory, which contains a value; this value can be obtained by its "name", and this value can be changed;
2). Define variable:
data type variable name = Initial value;
for example: store an integer:

int a = 0;
System.out.println(a);//0
a = 220;
System.out.println(a);//220

Java data types :
basic data types: four types and eight types
1. integers :
1) .byte 1 byte -128 ~~ 127
2) .short 2 bytes
3) .int 4 bytes (commonly used)
4) .long 8 Byte
2. Floating point ( decimal):
1). Float 4 bytes (single precision) greater than long
2). Double 8 bytes (double precision) greater than float (common)
3. character
1). Char 2 bytes ( Represents a character in a Unicode code table)
4. Boolean
1 ) . Boolean 4 bytes (true / false)
Reference data type: string
Note:
1). For "integer constants", Java uses int storage by default:

long g = 12345678900;//编译错误,int无法存储右边的值
long g = 12345678900**L**;//编译正确

2). For "float literal constants", Java uses double storage by default:

float f = 2.0;//编译错误。2.0会先用double存储。将double赋给float错误
float f = 2.0F;//编译正确

3). Within the same scope (a pair of braces), variables with the same name cannot appear, even if the data types are different

int a = 10;
double a = 20;//编译错误

4). When the variable is defined, the initial value must be assigned, otherwise it cannot be accessed:

long g ;
System.out.println(g);//错误

Variable name: It belongs to the "custom identifier", which is defined by our programmers and used when the program is running.
A). Naming "rules" for custom identifiers:
1). The name can only contain: letters (including Chinese), numbers, $ and _ symbols.
2). Numbers cannot start;
3). Cannot contain "spaces";
4). Cannot have the same name as "keywords" (the same capitalization)
B). Naming "norm":
general norms: see names and names, camel case naming
– Camelback naming:
1). Big camel case: the first letter of the first word is capitalized, and the first letter of each subsequent word is capitalized; the
class name
For example:

public class HelloWorld{//大驼峰
}

· 2). Small camel case: the first
letter of the first word is lowercase, each subsequent word is capitalized;
variable name, method name

public class HelloWorld{
public static void main(String[] args){
int stuAge = 20;//小驼峰
} }

Initial value: Assign an initial value to the variable within the range it stores;

int a = 0;
double b = 0.0;
String s = "";

Supplement the little knowledge
"Scanner class": a "class" that comes with the JDK, it can receive data input by the user from the "console".
2). Use steps of Scanner class:
1). Guide package: after package statement, in front of class definition:
import java.util.Scanner;
2). Create object: Create Scanner object in main ():
Scanner sc = new Scanner (System.in);
3). Receive data:
1). Receive string: sc.next ();
2). Receive integer: sc.nextInt ();
3). Receive floating point: sc. nextDouble ();
sample code:

import java.util.Scanner;//导包
public static void main(String[] args) {
//2.创建对象
Scanner sc = new Scanner(System.in);
//3.接收数据:
//3.1:接收字符串:next()方法
System.out.println("请输入你的姓名:");
String name = sc.next();
//3.2:接收整数:nextInt()方法
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
//3.3.接收浮点数:nextDouble()方法
System.out.println("请输入你的身高:");
double h = sc.nextDouble();
System.out.println("=====以下是你输入的信息=====");
System.out.println(name);
System.out.println(age);
System.out.println(h);
}
Published 8 original articles · Likes0 · Visits 38

Guess you like

Origin blog.csdn.net/FearSun/article/details/105380705