Learn programming from scratch a basic grammar grammar --java

Previous article describes how to configure complete environment ( environment configuration syntax), after the next step is to configure the environment to start learning Java, including the evolution of language and java java.

Evolution of java language

java language history earlier, the formation of the following three technical architecture:

javaSE: complete the development of desktop applications, is the basis for the other two;

javaEE: application under development enterprise environment, primarily for the development of web applications;

javaME: the development of consumer electronics and embedded devices, such as mobile phones in the program;

Now the market has no JavaME, mainly because the mobile end Android and iOS, Android is also used for java language development, so learning Android only need to have the knowledge to JavaSE, it is to master the basic grammar java on it, I began to say the following syntax part of java.

Note

Note the meaning of the interpreted code, as in the following example:

//定义一个变量
int a = 5;
/* 再定义一个变量 */
int b = 6;

The above characters is explained on the following code, the computer identification code is ignored when the content of the comment. There are three ways in java comments:

Single-line comments: // ... use to represent

Multi-line comments: using / ... / to represent

Documentation NOTE: Use the / * ... * / to represent

Single-line comments in the actual development and the use of multi-line comments are more basic need documentation comments.

Basic data types

We know that computers are only recognizes a binary, that is, in the computer world only 0 and 1, but for ease of programming, it abstracts a lot of high-level languages ​​such as java

In these high-level languages ​​do not have to use two classifications and computer communication, but also need to define some data type, like some of the words, Chinese idioms we defined, we say these words are composed, in java language defines eight basic data types, namely:

byte、short、int、long、float、double、char、boolean

Int: byte, short, int, long
float: float, double
char: char
Boolean: boolean

Integer without explanation, that is, they use these types to represent integers, the difference between them is the size of four different computer memory.

byte one byte
short accounts word
int total of four bytes
long occupies eight bytes

In the actual development byte and short less than basic, long use is relatively small, int is most frequently used.

Floating point variable to indicate the difference between the decimal, float and double are different in memory size.

float total of four bytes
double accounted for eight bytes

float in the actual development of the use of more and less double use.

Indicates the character string variables, such as 'a', the character is in java single quotation marks.

char is two bytes

char character is not much use in actual development, because it can only represent a character, such as 'a', we will generally represent a string of characters, such as "this is home", which needs to say behind a string variable String .

Boolean variables boolean, the only two values, true and false, to see that this variable is mainly used to judge.

We just use it for judgment in actual use, boolean theoretically accounted eighths of a byte, because a switch you can decide whether true or false, but the java boolean type is not explicitly specify his size

Java variables are defined in the following format:

int a = 5;
类型  名字 = 变量

The above wording that is a definition of a variable, a type is an int, then copy this integer to a 5, after executing the above sentence is a computer, the computer will know a is equal to 5, is used to a later 5.

The following give examples of the eight basic types of data:

//整数类型
byte b = 10;            
short s = 20;       
int i = 30;         //整数默认的数据类型就是int类型
long x = 8888L;     //占八个字节 如果long类型后面加L进行标识最好加大L,因为小l太像一了

//浮点类型
float f = 12.3F;    //后面一半跟一个F,表示float类型
double d = 33.4;    //小数默认的数据类型是double,double类型后面也可以用D或d标识,但是一般不加

//字符类型
char c = 'a';           //占两个字节

//布尔类型
boolean b1 = true;

While the above byte, short, int watching is an integer, but the situation in the computer's memory is not the same, the actual development usually use to define int.

Operators

When these variables we have been defined above operation is required, such as addition, subtraction, such as greater than or less, for these operations require the use of the operator.

Arithmetic operators: + - * / ++ -

Front four is arithmetic, not to say, talk about the two ++ back (from Canada) and - (decrement).

++: the original data + 1
-: the original data -1

int a = 5;
a++; //++完以后a就等于6

int b = 4;
b--; //--完以后b就等于3

Note that, in addition to a ++ wording, as well as ++ a wording, the difference between them:

When time + (A +) after the variable, the first value of the variable will be done in the assignment taken, and then add 1 itself.

When the time ++ (++ a) in front of the variable itself will first add 1, then the result will be assigned.

int a = 5;
b = a++;  //执行完这句以后b = 5  a = 6,因为是a++,计算机会先把a原来的值赋值给b,然后在自增

int c = 5;
d = ++c;  //执行完这句以后,d = 6  c= 6

In the actual development of each of the arithmetic operators are used in a lot.

Assignment operators: + = = - = = * / =

They are equal sign, and the like add, subtract, etc., in addition to other

// 等号表示把5赋值给a变量
int a = 5;
//表示把a和5相加,然后把结果再赋值给a,最后a = 10
a += 5;

= In the actual development is the most used, followed a few basic need.

Comparison operators: == =>> = <<=!

They are equal, not equal, greater than, greater equal, less than, or less.

Comparison operators used to compare two variables, the result is a boolean variable.

==: comparison of two equal signs, an equals sign represents the assignment
=:!! No use in java expressed this expression does not equal

Several behind not specifically, the following are examples

int a = 5;
int b = 6;
//定义一个变量c 来接收a == b 的结果
boolean c = a == b; //结果是false
boolean d = a != b; //结果是true

Many use comparison operators in the actual development.

Logical operators:! & | ^ && ||

Respectively: AND, OR, XOR, NOT, and double, or double.

Logical operators normally used to connect the boolean expression or value.

& Logic: there is false false
| logical OR: true there is true
^ XOR logic: the same false, different true
! Logical NOT: Non false is true, then the non-true false

int a = 10;
int b = 20;
int c = 30;
a < b & b < c           //true & true = true

//逻辑或 或or 遇true则true
a < b | b < c           //true | true = true

//逻辑异或 ^ 两边相同为false,两边不同为true
a < b ^ b < c       //true ^true = false

//逻辑非!
!true

Because & criterion is: is there false false.

So there && (and double), i.e., only determination && previous results, false if it is not in the back of the variable to judge true or false

int a = 10;
int b = 20;
int c = 30;
//前面的 a>b 已经是个false了已经能够确定这条语句的结果了
//此时因为是&&,所以计算机不会再去判断后面的 b<c 了。对于 || (双或)同理
a > b && b < c

In the actual development, ^ (XOR) with very little, quite many other uses.

A variety of sentence

There are a variety of java in this sentence a few: select statement, loop, control jumps statements, these sentences are to simulate realistic scenarios to achieve the effect of language and computer can communicate.

Select statement: if if ... else switch

if statement: that is: if ... then ..., and the language is almost our reality, in the code format is as follows:

if(比较表达式) {
    语句体;
}

First calculate the comparative value of the expression, to see the return value is true or false.

If this is true, then the statement is executed body; if it is false, the statement does not execute body.

if ... else statement: is represented by: if or if ... 88 ... 88, corresponding to two determined, code format is as follows:

if(比较表达式) {
    语句体;
}else(比较表达式){
    语句体;
}

switch statement: is represented by: performing different logic depending on the result code format is as follows:

switch语句:表示的是:根据不同的结果执行不同的逻辑,代码格式如下:

Above, switch back to a calculation expression will result, to match the result of the calculation the following values ​​are below Case, which executes the matching sentences corresponding body, if no match on the back of the body will default to execute the statement, when it encounters a break keyword, the statement ended.

int a = 3;
int b = 4;
if(a > b){
    System.out.println("a 是最大值");
}else {
    System.out.println("a 是最小值");
}
switch(a){
    case 3:
        System.out.println("a 的值为3");
        break;
    case 4:
        System.out.println("a 的值为4");
        break;
    default:
        System.out.println("a 既不等于3也不等于4");
        break;
}

In the actual development of these three statements used very much.

Loop: for while

Loop means that will execute until the task is finished came to a halt, such as displaying live in the teacher let us copy the text five times, we shall continuously perform five times, know five times over the implementation came to a halt.

for statement: code format is as follows:

for(初始化表达式;条件表达式;循环后的操作表达式) {

    循环体;
}

This view may be a bit abstract, look at the following specific examples:

for (int i = 1;i <= 10 ;i++ ) {

    System.out.println("helloworld");
}

Code execution flow is: to define variables of type int i is 1, then the execution of the conditional expression behind i <= 10, satisfying the following condition executes loop, after the code loop is finished, it performs the expression i ++ , add 1 to the variable i, and then go execute conditional expression, if it does not meet the out of the loop, if satisfied to continue.

while statement: while statement is used to represent the cycle, and for it a little bit different in the following format:

while(判断条件语句) {

    循环体语句;
}

while the format is relatively simple, is that it will go to judgment condition statement, the statement is true when it will go to perform loop after loop is finished will determine conditional statement again, if it will continue to meet, if it is not satisfied It will be out of the loop.

int x = 1;
while (x <= 10) {

    System.out.println("x = " +  x);
    x++;
}

As above, the loop will execute 10 times.

In the actual development of both the use of loops is very large.

Control statements: break, continue

break statement: break statement has been mentioned above, and it is mainly used to jump out of the statement, its use is limited scenarios, it can only be used in switch statements and loops in.

for (int x = 1;x <= 10 ;x++ ) {
    if (x == 4) {
    break;  //跳出循环
    }
}

continue statement: that is to continue the meaning of that experience continue when it will no longer be the statement following the execution, continue to the next cycle. It can only be used in the loop statement.

for (int x = 1;x <= 10 ;x++ ) {
    if (x == 4) {
        continue;   //终止本次循环,继续下次循环
    }
    System.out.println("x = " + x);
}

In the actual development of these two control statements used very much.

The moment summary

This is a major point, said three types of grammar java syntax, basic data types, operators, and a variety of sentence.

Eight basic data types: byte, short, int, long, float, double, boolean, char

Operators: arithmetic operators, assignment operators, comparison operators, logic operators

Sentence: select statement, loop statements, control statements

In fact, when learning grammar is the best way to compare our lives to learn the language, because language is a programming and computer dialogue, tells the computer what to do to live, a good article on here, then talk about the next java grammar, including functions, arrays, and other object-oriented grammar.
Here Insert Picture Description

I am concerned about the number of public, we progress together

Guess you like

Origin blog.csdn.net/static_zh/article/details/95053072