Java-day03 study notes

December 30, 2020

1. Common naming rules

  • Package (write the domain name upside down, generally words are lowercase)

    • One word com
    • Multiple words com.baidu
  • Class, interface (the first letter of each word must be capitalized)

    • One word Hello, Demo
    • Multiple words HelloWorld
  • Methods, variables (the first letter of the first word is lowercase, the first letter of the remaining words must be uppercase)

    • One word sum, max, result
    • Multiple words getSum, getMax
  • Constants, enumerations (words should be all capitalized, separated by underscores)

    • One word PI, E
    • Multiple words MAX_VALUE, INT_MAX_VALUE

Two, data type

2.1 Conversion of data types

①The default data type conversion
默认的就是不需要我们管的,会自动进行的转换。
从小到大到的转换,不会发生数据丢失的风险。

总结规律如下:
	1. 如果数据类型是int范围之下的(包括int),那么计算的时候会先将这些提升为int类型进行计算,并且结果也为int类型。
	2. 如果数据类型是int范围之上的(包括int),那么计算的时候会先将较小的类型自动提升为较大的类型,并且结果也为较大的类型。

数据类型从小到大排列:byte < short = char < int < long < float < double
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
		
		//byte类型可以顺利赋值给short类型
		//byte b = 5;
		//char c = b;   //不匹配的类型,不能从byte到char
		//short s = b;
		
		//byte类型可以顺利赋值给int类型
		//byte b = 5;
		//int a = b;
		
		//int类型可以顺利赋值给double类型
		int a = 50;
		double d = a;
		
		System.out.println(d);
	}
}
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
		
		/*
		int a = 3;
		int b = 5;
		int sum = a + b;
		*/
		
		/*
		byte b = 3;
		byte c = 5;
		//byte sum = b + c;  //错误,不能从int类型转换为byte类型
		int sum = b + c;
		*/
		
		/*
		short s = 3;
		short s2 = 5;
		//short sum = s + s2;  //错误,不能从int类型转换为short类型
		int sum = s + s2;
		*/
		
		/*
		int a = 3;
		long b = 5;
		//int sum = a + b;     //错误,不能从long类型转换为int类型
		long sum = a + b;
		*/
		
		/*
		int a = 3;
		float b = 5.0F;
		//int sum = a + b;   //错误,不能从float类型转换为int类型
		float sum = a + b;
		*/
		
		float a = 3;
		double d = 5;
		//float sum = a + d;   //错误,不能从double类型转换为float类型
		double sum = a + d;
		
		System.out.println(sum);
	}
}
②Forced data type conversion
强制的那应该就是需要我们进行手动操作的。
从大到小的转换,有数据丢失的风险。

格式:
较小的数据类型 变量名 = (较小的数据类型)较大的数据类型;
public class Demo3 {
    
    
	
	public static void main(String[] args) {
    
    
	
		int a = 3;
		//byte b = a;  //可能会发生数据丢失,所以不能从int类型转换为byte类型
	
		/*
			因为我们知道,3是一定在byte范围之内的,是可以用byte存储的。
			但是系统不让我们进行直接的赋值,那么就必须得通过强制的转换赋值了。
			
			强制的数据类型转换格式如下:
			较小的数据类型 变量名 = (较小的数据类型)较大的数据类型;
		 */
		byte b = (byte)a; 
	
		System.out.println(b);
	}
}
public class Demo5 {
    
    
	
	public static void main(String[] args) {
    
    
	
		byte a = 3;
		byte b = 5;
		//byte c = a + b;   //错误
		byte c = (byte)(a + b);
		System.out.println("--------我是分割线--------");
		
		byte d = 3 + 5;   //正确,常量的转换机制
		//byte e = a + 6;   //错误,不能从int类型转换为byte类型
		byte e = (byte)(a + 6);
		/*
			1.两个常量进行计算的时候,会先进行运算,
			得到结果之后再看这个结果是否属于要赋给类型的范围之内
			如果是在范围之内,那就直接赋值成功。
			
			2.如果是一个常量加一个变量,那么还是要进行类型转换的。
		 */

		System.out.println(c);
		System.out.println(d);
	}
}

2.2 Character type

Use char to declare, the data is enclosed in single quotation marks in English

public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
		
		char ch = '中';
		
		ch = '公';
	
		System.out.println(ch);
	}
}
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
	
		/*
		char c = '中';
		int a = 1;
		//char ch = c + a;  //错误,不能从int类型转换为char类型
		int ch = c + a;
		*/
		
		
		char c = 'a';   
		char c2 = 'A';
		char c3 = '0';
		int a = 1;
		
		//int result = c + a;    //98
		//int result = c2 + a;   //66
		int result = c3 + a;    //49
		/*
			通过以上运算,我们发现了字符a其实等于整数97,字符A等于65,字符0等于48
			
			因为有规律,每次都是递增1,所以以后我们记得如下三个数据就可以了
			字符   对应的整数
			 a         97
			 A         65
			 0         48
	     */
		
		System.out.println(result);

	}
}

2.3 String type

The string type needs to be declared by String, and the content is enclosed in double quotes in English.

The string and any content are connected by a plus sign +, and the result is a string type.

public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
		
		String s = "hello";
		
		/*
			尝试字符串是否能进行运算?
			
			可以运算,但是结果还有点不太懂。
		 */
		//int ss = s + 1;   //错误,String不能转换为int类型。这说明结果是String类型
		String ss = s + 1;  //hello1
		
		System.out.println(ss);
	}
}
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
		
		/*
			探究字符串计算之后的结果到底是什么类型
		 */
		String s = "hello";
		
		System.out.println(s + 5);           //hello5
		System.out.println(s + 5 + 5);       //hello55
		System.out.println(s + (5 + 5));     //hello10
		
		System.out.println(5 + s);           //5hello
		System.out.println(5 + 5 + s);       //10hello
		System.out.println("" + 5 + 5 + s);  //55hello
		
		String ss = s + false + 12.35 + '中';
		System.out.println(ss);
	}
}

Three, operator

Operator: A symbol that enables variables and constants to perform operations. Such as: +

Expression: A formula that conforms to the Java grammar specification connected by operators. Such as: a + b

3.1 Arithmetic operators

+ - * / %

+的作用:
	1. 表示正数
	2. 相加
	3. 拼接
-的作用:
	1. 表示负数
	2. 相减
/的作用:求整
	如果就是想要小数结果,怎么办?
	只需要让其中一个数变成小数即可。可以让任何一个数乘以1.0,也可以让任何一个数强制转换为double或float
%的作用:求余数

3.2 Increment and decrement operators

++ --

++和--在不参与运算的时候:
	++和--无论在前还是在后都是自增1
	
++和--参与运算的时候:
	++和--在前:
		会先进行自增1或者自减1,然后将变化后的结果赋值给变量
	++和--在后:
		会先将数据赋值给变量,然后自身再自增1或者自减1
/*
	不参与运算的时候
 */
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
	
		//System.out.println(3++);  //错误,自增自减运算符只能作用于变量,不能作用于常量
	
		int a = 3;
		
		//a++;
		//++a;
		
		//a--;
		--a;
		
		System.out.println(a);
	
	}
}
/*
	参与运算的时候
 */
public class Demo3 {
    
    
	
	public static void main(String[] args) {
    
    
		
		int a = 3;
		
		//int b = a++;

		//System.out.println(a);  //4
		//System.out.println(b);  //3
		
		System.out.println("--------我是分割线--------");
		
		int b = ++a;
		
		System.out.println(a);   //4
		System.out.println(b);   //4
	}
}
/*
	练习:
 */
public class Test {
    
    
	
	public static void main(String[] args) {
    
    
		
		int a = 3;
		int b = 5;
		
		int c = a++ + b-- + (b = a--);
	//参与运算  a=3   b=5     a=4 b=4    c=3+5+4=12     
	//实际值    a=4   b=4     a=3 b=4
		

		System.out.println(a);  //3
		System.out.println(b);  //4
		System.out.println(c);  //12
	}
}

3.3 Assignment operator

基本的:
	=

扩展的:
	+=
	-=
	*=
	/=
	%=
注意:扩展的赋值运算符默认带有强制的数据类型转换。
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
	
		int a = 3;  //这个式子实际上应该被读为将3赋值给变量a
	
		System.out.println();
	}
}
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
	
		int a = 3;
		int b = 5;
		
		/*
			a += b; 
			相当于等号左边的a加上等号右边的b的结果赋值给了a,
			也就是等式a = (int)(a+b);
		*/
		a += b;  
	
		System.out.println(a);   //8
		System.out.println(b);   //5
		
		System.out.println("------------分隔线------------");
		
		byte c = 3;
		byte d = 5;
		
		//c += d;  //相当于c = (byte)(c + d);
		
		System.out.println(c);
		System.out.println(d);
		
	}
}

3.4 Comparison operators

==  等于   
!=  不等有
>   大于
>=  大于等于
<   小于
<=  小于等于

注意:
	1. 比较运算符得到的结果为boolean类型,要么是true,要么是false
	2. 一定要注意区分=和==
			=是赋值
			==是比较
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    
	
		int a = 3;
		int b = 5;

		System.out.println(a == b);   //false
		System.out.println(a != b);   //true
		System.out.println(a > b);    //false
		System.out.println(a >= b);   //false
		System.out.println(a < b);    //true
		System.out.println(a <= b);   //true
	}
}

3.5 Logical operators

&  与
|  或
!  非
^  异或

注意: 
	1. & | ^ 两边是要放结果为boolean类型的表达式
	2. !只有一边放boolean类型的表达式
public class Demo {
    
    
	
	public static void main(String[] args) {
    
    

		/*
			与& :
				两边同为true的时候才为true,
				两边只要有一个为false就为false
		 */
		System.out.println(true  & true);   //true
		System.out.println(true  & false);  //false
		System.out.println(false & true);   //false
		System.out.println(false & false);  //false
		System.out.println("---------分隔线---------");

		/*
			或|:
				两边同为false的时候,才为false
				两边只要有一个为true就为true
		 */
		System.out.println(true  | true);   //true
		System.out.println(true  | false);  //true
		System.out.println(false | true);   //true
		System.out.println(false | false);  //false
		System.out.println("---------分隔线---------");
		
		/*
			异或^:
				两边相同的时候,才为false
				两边不同的时候,才为true
				
				注意:可以简记为异性才是真爱,是正确的。
		 */
		System.out.println(true  ^ true);   //false
		System.out.println(true  ^ false);  //true
		System.out.println(false ^ true);   //true
		System.out.println(false ^ false);  //false
		System.out.println("---------分隔线---------");
		
		/*
			非!:
				如果是true的时候,那么结果为false
				如果是false的时候,那么结果为true
		 */
		System.out.println(!false);  //true
		System.out.println(!true);   //false

	}	
}
短路与&& 
短路或||
public class Demo2 {
    
    
	
	public static void main(String[] args) {
    
    
	
		System.out.println(true  && true);   //true
		System.out.println(true  && false);  //false
		System.out.println(false && true);   //false
		System.out.println(false && false);  //false
		System.out.println("------分割线------");
		
		/*
			短路与&&:
				如果第一个为true,那么再看第二个是true还是false
					如果第二个为true,那么结果为true
					如果第二个为false,那么结果为false
				如果第一个为false,那么第二个根本不用执行,而直接给结果false
		 */
		int a = 3;
		//System.out.println(true && (++a > 4));  //a=4执行到了后半句,因为有true有false,所以结果为false
		//System.out.println(false && (++a) > 4);  //a=3证明后半句根本就没有执行到。因为只要有一个false,结果就为false
		
		//System.out.println(false & (++a) > 4); //a=4执行到了后半句,因为两个都为false,所以结果也为false
		System.out.println("----------------------");

		/*
			短路或||:
				如果第一个为false,那么还需要看第二个是true还是false
					如果第二个为false,那么结果为false
					如果第二个为true,那么结果为true
				如果第一个为true,那么直接给结果true
		 */
		System.out.println(false || (++a > 4));  
		
		System.out.println(a);
		 
	}
}

3.6 shift operator

右移 >> :每右移一位都会在原来的基础之上除以2
左移 << :每左移一位都会在原来的基础之上乘以2
无符号右移 >>> :每右移一位都会在原来的基础之上除以2(只作用于正数,负数会得到不正确的结果)

3.7 Ternary expression

格式:
条件表达式 ? 表达式1 : 表达式2;

注意:
	1. 条件表达式其结果必须为boolean类型,要么是true、要么是false
			如果是true,那么执行表达式1
			如果是false,那么执行表达式2
	2. 三元表达式既不会都执行,也不会都不执行,而是只会根据条件表达式的结果执行其中一个表达式。
	3. 表达式1和表达式2可以是具体的值。
	4. 三元表达式得到的结果,有两种处理方案:
			1. 直接输出
			2. 先赋值再输出

Guess you like

Origin blog.csdn.net/ChangeNone/article/details/111994643