Learn to use java --- statement of judgment

Knowledge Review:
1) emphasize different learning methods, it and other knowledge, math, thinking skills, the ability to abstract (difficult) language back. It is very studious, 22 years, 15 years in the software business (beginner, intermediate, advanced, system analysts, architects, technical director, later walked management) knowledge is applied repeatedly knock more practice. For example main, knock on the status quo.
2) What is boxing and unboxing?
a) packing, the basic type of the variable conversion type packaging
(packaging type) Integer i = 10; (primitives) that
significantly = left to the right, instead of a same type, why java, automatic packing (converted)
real codes :( type of packaging) Integer i = new Integer (10 ); ( package type)
B) unpacking, the package type convert a basic type
(basic type) int j = i; (package type)
significantly = left to the right, instead of a same type, Why java, automatic unboxing (converted)
real Code: int j = i.intValue (); ( package type integer intValue providing method, which results in a value of type integer, integer 10)
Why not write error, the underlying work principle. Can understand. Really develop you write the above code can be run.
3) ternary operator, java only ternary operator (ternary operator)
A) has three parameters
b) :( Boolean expression fixed format, only two results, true / false) (x> y ) x:? y (rote)
4) 2 seeking the maximum number?

package test;
	//导入单元测试import org.junit.Test;
	//求最大值public class Max {
	@Test	//求2个数最大值
	public void max2() {

		int x = 10;
    	int y = 20;
	
		//三目表达式
		int max = (x>y) ? x : y;
		
		//下面+不是一个数字,如果遇到字符串,+连接符,连接两边字符串
		System.out.println("最大值是:" + max);
	}
	
	@Test	//求3个数最大值
	public void max3() {
		//思路?2次三目运算符应用
		int x = 10;
		int y = 20;
		int z = 30;
		
		int max = (x>y) ? x : y;	//把x和y中最大值就赋值给max变量
		//复用上面max变量,变量可以再次赋值
		max = (max>z) ? max : z;
		
		System.out.println("最大值是:" + max);
	}}

Today Content: After the development of the most common syntax
flow control:
1) Abnormal
2) branch Analyzing
a) a single limb IF
B) a multi-branch Switch
. 3) of the cyclic structure
a) for
B) the while
C) do-the while
the structure is almost later development use it every day! !

Process control:
1) the general order of the code execution, are executed
2) determining whether to perform, according to the conditions of the code execution, the code portion is not performed, wherein the code portion is skipped (IF statements, ternary operator)
3) cycle is repeated implementation of a code

1) Abnormal
a) judgment execution (part code execution, not part of the code execution)
b) encountered keyword: try attempt / catch to catch exceptions / finally final / throw thrown / throws receiving abnormal
c) all exceptions father: Exception
2 ) classic exceptions, the initiative to create an exception
by 0, mathematically 1/0, the denominator can not be zero, regulations. Error, java how to handle errors?

Red is the wrong word console appear under normal circumstances.

How to read English: Exception keywords to find some anomalies, main thread (operating system)
java.lang.ArithmeticException, the Java provide common exceptions Arithmetic Mathematics
specific error: / by zero, in violation of the rules of the denominator is 0

package test;
public class TestArea { 	
public static void main(String[] args) { 		
//处理异常 try/catch,语法:固定格式
		try { 			
		 	//没有就执行下面的语句,一旦出错跳入catch中 	
		 	//可能出错代码 	int y = 100/0;		
		 	//编写,保存:编译都不会做检查 		
		 	System.out.println(y);		  
			System.out.println("正确"); 		
		}catch(Exception e) { //已经出错,才会执行,不出错,就不执行
			System.out.println("错误"); 		
			}
		 	System.out.println("执行完成"); 	
		 }
 }

finally a try / catch code execution regardless of normal or error code that must execute
a special effect, such as after the database code release Connection links, resource-intensive, resources must be released.

package test;
public class TestArea {
	public static void main(String[] args) {
				try {
			int y = 100/0;
			System.out.println("正确");
		}catch(Exception e) {
			//展示具体错误信息
			System.out.println("错误:"+ e.getMessage());
		}finally {
			//System.err打印字红色,error错误
			System.err.println("管你正确还是错误,都必须执行我!!!");
		}
		System.out.println("执行结束");
	}
}

The try, catch is a typical process control, if the normal, which we try {} is executed only part of the code, if an error occurs, the error in the try statement is not executed after the code, the code jumps directly catch. Regardless of the implementation of the code which try Ye Hao, catch or whatever, must finally content in execution.

Throw thrown
Throws reception error
Try / catch its own exception handling, if an exception can not handle it?
Stalls event, I can not hack it to solve? you can throw an exception to the higher level (java is your caller)

package test;
public class TestArea {	//手写,提示生成,两种方式都可以接收异常
	public static void main(String[] args) throws Exception{
		try {
		int y = 100/0;		// by zero
		System.out.println("正确");
	}catch(Exception e) {
		//展示具体错误信息
		System.out.println("错误:"+ e.getMessage());
		
		//抛出异常
		throw new Exception("出错啦,分母不能是零!");	//创建一个新的异常,抛出,字符串错误信息
	}finally {
		//System.err打印字红色,error错误
		System.err.println("管你正确还是错误,都必须执行我!!!");
	}
	
	System.out.println("执行结束");
}}

Classic judgment, branching statements
1) if statement (most)
men (men are responsible for working to earn money) and women (women responsible for beautiful flower)
Package Penalty for the Test;

public class TestArea {// handwritten instructions to generate the two ways can receive the abnormal
public static void main (String [] args) {
Boolean = isMan to false; // to true, man, false, Woman

	/*
	 * 如果是男人控制台输出:男人负责工作挣钱
	 * 如果是女人控制台输出:女人负责貌美如花
	 */
	
	//分支判断语句
	if (isMan) {		//括号内是布尔表达式
		System.out.println("男人负责工作挣钱");
	}else {			//是女人要做的事情
		System.out.println("女人负责貌美如花");
	}
}

}

2) switch statement (rarely)
Switch can fully use the if statement to achieve, but it is more concise
swtich and case, default, key
business requirements: Phone
Tel: 110 police, 112 doctors, 114 inquiries phone number, 119 fire

package test;

public class TestArea {// handwritten instructions to generate the two ways can receive the abnormal
public static void main (String [] args) {
// Tel: 110 police, doctors 112, 114 queries the telephone number, 119 fire
/ *
* This is a special switch points, and different ways of thinking routine
* inside the first matching statement execution, after the back is not determined, all are executed
* found not matching the previous code execution are skipped
* /
int Phone = 135;
switch (Phone) {
Case 110:
System.out.println ( "I am the police");
BREAK; // execution is complete, jumped out of the switch statement
Case 112:
System.out.println ( "I am a doctor");
BREAK;
Case 114:
System.out.println ( "I'm a directory assistance");
BREAK;
Case 119:
System.out.println ( "I'm a fire");
BREAK;
default:
System.out.println ( "the phone does not recognize ");
}
System.out.println (" execution is complete ");
}
}

Switch statement can be replaced with an if statement

package test;

import org.junit.Test;

public class TestArea {
	@Test		//多重if语句嵌套
	public void testIf() {
		int phone = 120;
		if(phone==110) {
			System.out.println("我是警察");
		}else if(phone==120) {
			System.out.println("我是医生");
		}else if(phone==114) {
			System.out.println("我是查号台");
		}else if(phone==119) {
			System.out.println("我是火警");
		} else {
			System.out.println("这个电话不认识");
		}
	}
@Test
public void testSwitch(){
	int phone = 135;
	switch (phone) {
	case 110:
		System.out.println("我是警察");
		break;		//执行完成,就跳出switch语句
	case 120:
		System.out.println("我是医生");
		break;
	case 114:
		System.out.println("我是查号台");
		break;
	case 119:
		System.out.println("我是火警");
		break;
	default : 
		System.out.println("这个电话不认识");
	}
	System.out.println("执行完成");
}

}

AM Summary:
1) exception: the program will be an error, an error in the daily development tools (compiler error), the program hidden errors. (Runtime error)
A) general exceptions, all exceptions father, Exception, all exceptions root
b) try attempt, as long as the abnormal code, such code may occur must be placed in try
c) catch catch the exception, if an error occurs on the implementation of catch inside the code E exception, e.getMessage ()
d) a finally to release resources for complex objects (database link object Connection) whether the code is executed correctly error
e) throw an exception is thrown, not to deal with our own, we can not deal with, pay to the code calls the method, main to deal with.
f) throws reception abnormality, main () throws Exception {} , then set Man
2) Flow Control
a) sequential structure: code is executed from top to bottom, left to right perform I = 100 * 200 is int;
B) a branched structure: Some code execution, some of the code is not executed, according to the conditions
c) recycling the structure: implementation of a code is repeated
3) branch statements
a) a single limb: if statement
b) a multi-branch: if-if else-else, switch statements

Published 36 original articles · won praise 13 · views 1081

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104702878