Why do you want to learn java? In a word, when books are used, they hate less. If Java was not used at the scene, I would not have learned it. Now I have a deep understanding of it.
: The meaning of this sentence. When learning, don't think of immediate results. Maybe she will use it one day. At this time, you may be more open than others.
So don't worry about whether this thing is used or not. Learn it, cool (originally a teenager, and the word appears )..........,
The last two times I talked about something about Java. To this extent, do we want to do something else? Therefore, we need to understand the structure in Java.ProgramDesign part:
Process Control and Data operation structure: operators.
[Java assignment operator]
Why is this operator? Because it is too simple, it is easy to say. What does a value assignment mean? We use examples to describe:
/* This program test the oprator */public class oprtest {public static void main (string [] ARGs) {// asign value int inttest; system. out. print ("the value of inttest is:" + inttest );}}
The execution result of the above program is:
If an exception is thrown, we can see that the compilation process reports an error. Why? You can think about it for yourself.
Next weCodeMake the following changes:
/* This program test the oprator */public class oprtest {public static void main (string [] ARGs) {// asign value int inttest; inttest = 100; // initialize the variable value system. out. print ("the value of inttest is:" + inttest );}}
The execution result is:
We can see that the program has been successfully executed at this time. Why do you want to talk about this problem here? Because in C language, sometimes compilation is passed without assigning values to variables (the all alarm mode is not enabled), and
Sometimes it works. In Java, if the variable is not initialized and assigned a value, it cannot be compiled. At the same time, we can also see that I didn't assign an initial value to the variable when defining the variable, but started the statement to initialize the variable.
Note that the C language is called differently.
Assign a value to the runtime operator and use it in combination. We will introduce it below.
Four arithmetic operations in Java]
After knowing the number, we should receive four arithmetic operations: add, subtract, multiply, and divide. Everything is simple and natural, but it is not natural in the computer world. Let's take a look at the four arithmetic operations through examples:
/* This program test the oprator */public class oprtest {public static void main (string [] ARGs) {// asign value int inttest; inttest = 100; // initialize the variable value system. out. println ("the value of inttest is:" + inttest); // Add int sum; sum = inttest + 20121221; system. out. println ("sum = inttest + 20121221 =" + sum); // sub int DIF; DIF = sum-300; system. out. println ("DIF =" + DIF); // multiplication long Mul; Mul = sum * DIF; system. O Ut. println ("sum * DIF =" + Mul); // overflow, but can execute, so you need pay atention to this double doublemul; doublemul = 1.0 * sum * DIF; // here why we need to muliply with 1.0 ?? System. out. println ("1.0 * sum * DIF =" + doublemul); // division int div; DIV = DIF/789; system. out. println ("DIF/789 =" + Div); // normally 20121021/20121321 = 0.999985; how can the result be? Double doublediv; doublediv = 1.0 * DIF/789; system. out. println ("1.0 * DIF/789 =" + doublediv); // is the same wiht the statement DIV = DIF/SUM ;?? // Mod int MOD; mod = sum % 2; // is this equal to sum/2 ?? System. out. println ("sum % 2 =" + mod); system. out. println ("sum/2 =" + sum/2); // multiply plus and sub int postplus; int preplus; int postsub; int presub; system. out. println ("Mod =" + mod + ";" + "DIF =" + DIF); postplus = mod ++; system. out. println ("Mod ++ is:" + postplus); postplus = ++ postplus; system. out. println ("++ mod ++ is:" + postplus); postsub = DIF --; system. out. println ("DIF -- is:" + postsub); presub = -- postsub; system. out. println ("-- DIF -- is:" + presub );}}
The result of the above program running is:
The fourth arithmetic operation requires that the type conversion is performed during the calculation. For the/operator, if the operands are all integer, the result is integer. If the operands are floating-point, the result is
Is a floating point type. The ++ and -- operators are the same as those in C.
[Java Relational operators]
Java provides function-rich Relational operators: = ,! =, <, <=,>,> =
The effects of these operators are the same as those in the C language. I will not introduce them here, but it should be noted that when two floating point numbers are equal, you cannot simply use the = symbol for comparison.
Let's do a simple test.
// Equal to float fnum1 = 1.0f; float fnum2 = 0.99999999f; If (fnum1! = Fnum2) system. Out. println ("1.0! = 0.9999999 ");
What is the output of this program? The output result is as follows:
Here we can see that there is no output, so it means that when Java judges the if condition, it judges fnum1 as equal to fnum2.
[Java logical operators]
In Java, logical operators are also provided: And, or, and are not; and the symbols in C language are used, and '&' is used to represent; or '|' is used to represent; is not used! .
We will give an example of the specific and non-logic.
It is worth mentioning that there is a "Short Circuit" evaluation process in the process of or non-logical operation. When the result can be determined, the subsequent values will not be calculated. This sometimes affects the parameter transmission process.
For specific examples, we will wait until the method is discussed.
Java also provides a ternary operator, that is, ":?". The calculation rules of this operator are the same as those of the C language.
[Java bit operator]
Java provides bitwise operators similar to the C language. These bitwise operators are widely used and are usually used for "shielding" (how can they feel like shielding in a plc, but there is still a difference between the two ). Bitwise operators include:
By bit and, by bit or, by bit is different or, by bit is not.
The specific instance we do not give an example, there is a simple application is to find the byte type overflow value: 300 & 0xff; specific examples see: http://www.cnblogs.com/volcanol/archive/2012/12/18/2824169.html
Another interesting thing here is: & and | you can evaluate Boolean. Its calculation rules and usage are the same as those of logical operators. & corresponds to &, and | corresponds to |; let's look at an instance:
// Bit oprator Boolean btest1 = true; Boolean btest2 = false; If (btest1 & btest2) system. out. println ("True & True is true"); If (btest1 | btest2) system. out. println ("True | true is true ");
The execution result is as follows:
[Java shift operator]
In Java, the C language also provides the shift operator, which is also suitable for the calculation of integer numbers. Java provides two operators: Right Shift> and left shift <. Its specific usage and C language shift
The operators are the same.
We know that in C language, the right shift operator>, the rule for filling the highest bits is determined by the compiler, and there is no standard. To eliminate this ambiguity, Java specially designs a right shift operator ">>> "; and stipulates that
> The operator is used to fill the sign bit, that is, >>> does not change the positive and negative values of the value. >>> it specifies that the maximum bit is filled with 0.
[Constants in Java]
We know that the define mechanism is provided in the C language to define symbol constants, or symbol literal values; in C ++, the const mechanism is also provided to supplement the use of "symbol nominal value". Is there any
Is there a similar mechanism?
Obviously, the provision of such a mechanism will facilitate programming and reading; this mechanism is also provided in Java. How can this problem be achieved?
In Java, we can use the final keyword to define symbolic constants. Its syntax is as follows:
Final datatype variblename = constant literal value;
An example is provided to illustrate this problem:
Let's look at the next example:
A class constant can be defined in a class, just like the above definition, but is defined as private by default? Or public? Leave a question here and wait until we learn the modifier.
The following is the test code:
/* This program test the oprator */ Public Class Oprtest { Public Static Void Main (string [] ARGs ){ /** * ***** // Asign value int inttest; inttest = 100; // initialize the variable value system. out. println ("the value of inttest is:" + inttest); // Add int sum; sum = inttest + 20121221; system. out. println ("sum = inttest + 20121221 =" + sum); // sub int DIF; DIF = sum-300; system. out. println ("DIF =" + DIF); // multiplication long Mul; Mul = sum * DIF; system. out. println ("sum * DIF =" + Mul); // overflow, but can execute, so you need pay atention to this Double doublemul; doublemul = 1.0 * sum * DIF; // here why we need to muliply with 1.0 ?? System. out. println ("1.0 * sum * DIF =" + doublemul); // division int div; DIV = DIF/789; system. out. println ("DIF/789 =" + Div); // normally 20121021/20121321 = 0.999985; how can the result be? Double doublediv; doublediv = 1.0 * DIF/789; system. out. println ("1.0 * DIF/789 =" + doublediv); // is the same wiht the statement DIV = DIF/SUM ;?? // Mod int MOD; mod = sum % 2; // is this equal to sum/2 ?? System. out. println ("sum % 2 =" + mod); system. out. println ("sum/2 =" + sum/2); // multiply plus and sub int postplus; int preplus; int postsub; int presub; system. out. println ("Mod =" + mod + ";" + "DIF =" + DIF); postplus = mod ++; system. out. println ("Mod ++ is:" + postplus); postplus = ++ postplus; system. out. println ("++ mod ++ is:" + postplus); postsub = DIF --; system. out. println ("DIF -- is:" + postsub); presub = -- postsub; system. out. println ("-- DIF -- is:" + presub );*********/ /** * *************** // Equal to float fnum1 = 1.0f; float fnum2 = 0.999999f; If (fnum1! = Fnum2) system. Out. println ("1.0! = 0.9999999 ");********************** */ /** * ************** // Bit oprator Boolean btest1 = true; Boolean btest2 = false; If (btest1 & btest2) system. out. println ("True & True is true"); If (btest1 | btest2) system. out. println ("True | true is true ");*********** */ Final Int Finalinttest = 100; System. Out. println ( "Finalinttest is:" + Finalinttest );}}
It is found that Java is a type of C language. Many mechanisms and rules are very similar to C language, but you need to be careful about the differences between the two.