Java basic learning variables and constants

Variables and constants in Java basic learning
1. Variables
(1) The nature of variables
1. A section of storage space that can be manipulated
2. Three elements of variables: variable type, variable name, variable scope
3. Variable declaration specification: each line of declaration One and add a corresponding comment to explain
4. Variables must be declared before they can be used
(2) Classification of variables
1. Local variables (need to be initialized before use, named after the Camelback principle): defined in the method or in the statement block Variable, the scope of the variable from the definition of the variable to the end of the method or statement block.
2. Member variables: Variables defined outside the method and inside the class are subordinate to the object and can be automatically initialized as the object executes.
3. Static variables: use static to modify and belong to the class. The declaration cycle is accompanied by the beginning and end of the class, and the life cycle is the longest.

Second, the constant: usually refers to a fixed value, after using the final repair, the value of the constant cannot be changed again
1. Naming convention: all capital letters used and separated by underscores
2. Use final to modify

Third, the basic data type
(1) basic data type
1, numeric type:
① integer type: byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes )
② Floating point type: float (4 bytes), double (8 bytes)
③ Character type: char (two bytes)
④ Boolean type (Boolean): true / false (1 bit)

(Two) integer variables / constants
Integer data type

/**
* 测试基本数据类型
* @author 14323
*
*/
public class TestprimitiveDateType {
       public static void main(String[] args){
       //测试整形变量
       int a = 15;
       int b = 015;//0开头的是八进制
       int c = 0x15;//0x开头的是十六进制
       int d = 0X15;//0X开头的是十六进制
       int v = 0b110001;//0b开头的是二进制
       int x = 0B110001;//0B开头的是二进制
       System.out.println(a);
       System.out.println(b);
       System.out.println(c);
       System.out.println(d);
       System.out.println(v);
       System.out.println(x);
       
       byte age = 20;
       short salary = 30000;
       int papulation = 2000000000;//整型常量默认为int类型
       long globaPopulation = 7400000000L;//后面加L表示为一个long类型,要将默认的int改为long类型
       }
}

(3) Floating-point constants and variables Insert picture description here
⭐ Cautions when assigning a value to float

/**
* 测试浮点型
* @author 14323
*
*/
public class TxstPrimitDataType2 {
       public static void main(String[] args) {
             //float b = 3.14; 错误,需要添加f/F后缀
             float a = 3.14f;
             float c = 3.14F;
             double d = 6.28;
             double e = 628e-2;//科学计数法
             
             System.out.println(a);
             System.out.println(c);
             System.out.println(d);
             System.out.println(e);
             
             //浮点数是不精确的,不能用于比较
             //案例1:两个相等的值显示不等
             float q = 0.1f;
             double p = 1e-1;
             System.out.println(p==q);
             
             //案例2,两个不相等的值显示相等
             float testNumberOne = 423432423F;
             double testNumberTwo = testNumberOne+1;
             if(testNumberOne==testNumberTwo) {
                    System.out.println("两者相等");
             }else {
                    System.out.println("两者不相等");
             }
                           
       }
}

Insert picture description here
You cannot assign a value to float this way, because the default data type for the decimal point is double.
Analysis
1. The default data type of floating-point type is double
2. Floating-point numbers are inaccurate.
3. The way of representing data types in Java
: ①decimal
: 3.14, 314 ②Scientific notation: 314e (E) -2, 3.14e (E) 2
4. When using float = x assignment, you need to add l or L (preferably uppercase) at the end of the data.
5. If you want to compare floating point numbers accurately, you can use BigInteger (large integer, arbitrary precision integer arithmetic in Java) ) And BigDecimal (large floating point, arbitrary precision floating point operation)

             //精确浮点运算
             BigDecimal bd = BigDecimal.valueOf(1.0);
             bd = bd.subtract(BigDecimal.valueOf(0.1));
             System.out.println(bd);
             System.out.println(1.0-0.1);
             
             //精确浮点比较
             BigDecimal ab = BigDecimal.valueOf(0.1);
             BigDecimal ac = BigDecimal.valueOf(10E-2);
             System.out.println(ab.equals(ac));

(4) Character variables / constants
Insert picture description here

/**
* 测试字符类型
* @author 14323
*
*/
public class TestPrimitiveDateType3 {
       public static void main(String[] args) {
             char a = 't';//表示一个字母字符
             char b = '成';//表示一个汉字字符
             char c = '\u0061';
             System.out.println(c);
             
             //转义字符
             System.out.println(""+'a'+'\r'+'b');//打印空格
             System.out.println(""+'a'+'\''+'b');//打印一个单引号
             System.out.println(""+'a'+'\"'+'b');//打印一个双引号
             System.out.println(""+'a'+'\b'+'b');//打印退格
             System.out.println(""+'a'+'\n'+'b');//打印换行
             System.out.println(""+'a'+'\\'+'b');//打印反斜
             System.out.println(""+'a'+'\t'+'b');//打印制表符
             
             //定义字符串用String,实质上是一串字符序列
             String okHaha="zuihouyizhichengdu";
       }
}

Parsing
1. The length of the character constant is two bytes, but the string cannot be stored in it.
2. String is needed to store the string. This is essentially a sequence of characters.

(5) Boolean
type⭐Boolean only takes one bit, the constant value is true / false

/**
* 测试布尔类型
* @author 14323
*
*/
public class TestPrimitiveDataType4 {
       public static void main(String[] args) {
             boolean man = true;
             if(man==true) {
                    System.out.println("男性");
             }
       }
}
Published 6 original articles · won 3 · views 198

Guess you like

Origin blog.csdn.net/weixin_43855187/article/details/104130265