Java 0 Basic Starter Series Day5 operators in Java

Source: Internet
Author: User

Operators, as the name implies, are symbols for operations, such as the simplest +-*/, which can be used to perform mathematical operations, with the simplest of chestnuts:

It is known that the rectangle has a length of 3cm and a height of 4cm, and the area of rectangle is obtained.

OK, let's start with a new project named rectangle.

Then right-click the SRC folder and create a new package named Pers.frank.rectangle.

Here to add a description of the package naming rules, I believe many people know that when the package named with the WWW domain name, plus the project name, but in fact, that is generally used for the company project naming, for different projects, have different naming rules.

  Indi : An individual project, a project that is initiated by an individual but not done on its own, may be public or private, and the copyright is primarily the originator.

The package is named "Indi. Initiator name." Project name. Module name ... ".

Pers : Individual project, which refers to individual initiation, completion alone, shareable projects, the copyright is mainly personal.

The package is named "Pers. Personal name." Project name. Module name ... ".

  Priv : A privately owned project, which is an individual-initiated, self-fulfilling, privately-used project.

The package is named "priv. Personal name." Project name. Module name ... ".

Onem : As with "Indi", it is recommended to use "Indi".

Team: A project that is initiated by a team and developed by the team, which is owned by the team.

The package is named"team." The name of the project name. Module name ....

com : Company project, copyright by the project initiated by the company owned.

The package is named "com. Company name." Project name. Module name ... ".

Package is mainly used to complete different functions of the class categories, placed in different directories (packages), convenient class management. We only need one class for this project, so we don't need to use a level four package name. The previous tutorial said a bit fast, the package name is set to HelloWorld, is not in accordance with the rules, here explain. (This is a wrong demonstration, the secret system embarrassing ...) )

Then create a new class in the package and right-click to name Recrangle.

The code is as follows:

 Package Pers.frank.rectangle;  Public class Rectangle {    publicstaticvoid  main (string[] args) {        int length = 3;         int width = 4;         int area = (length + width) * 2;        System.out.println ("area:" + area);}    }

Because the length and width are integers, you can use the int type. Next, let's run a bit.

  

This gives us the results we want.

At this time perhaps you have to ask, this result mental arithmetic can calculate, why still have to go to the trouble to write code? I said just to give one of the simplest chestnuts, if the length of the width of 1.234567 and 2.345678 respectively, you can still mental arithmetic it? You might say, just press the calculator. What if it is the largest of the 10 rectangles? What if it is the 1234th of the Fibonacci series? Can you still press it with your computer? Thus, the more complex the problem, the more it can reflect the superiority of programming. When you're still pushing the calculator, I have to come out and swim in the Summoner Canyon.

Maybe there's something you don't understand about this piece of code. Don't worry, let me say one sentence to read.

First line, package pers.frank.rectangle; Here is a description of the current class belongs to this package, if not written, it can be run, but in this case, the class will be divided into the default package. It is not the same as when the package name and the package name are not compiled. Specifically, you can view the http://blog.csdn.net/com_stu_zhang/article/details/25112591.

In the second line, public class Rectangle, which is declaring a class, publicly means that access to the class is publically accessible, the class keyword indicates that this is a class, and Rectangle is the class name. The content in the back curly braces is the definition of the class.

The third line, public static void main (string[] args), is the declaration of the main entry function. Each program must have an entry function, that is, the first executed function, the compiler will find and load the class containing the entry function, and then load the other related classes, because it is the first executed function, there is no class instantiation of the object, so you need to use the static decoration, the argument list in parentheses, String[] means that the parameter is an array of strings, args is the parameter name, here is not a detailed introduction, now do not understand it's okay, just need to know that the entry function long so on the line.

The 45th line defines two int variables, length and width, and initializes them separately.

Line six defines an int variable area and assigns the value of the expression (length + width) * 2 to it.

The seventh line is the output statement, the specific use of the output after the detailed introduction, here just need to know that it can be used as well.

In this way, it is not a bit more understanding of our code.

What are the other operators besides these most basic symbols?

  1, self-increment, decrement operator.

In Java, as in C + +, there are self-increment decrement operators, the effect is to increase the number of variables by 1, or reduce by 1. Look directly at the code:

 Package pers.frank.test; Import Java.lang.System;  Public class Test {    publicstaticvoid  main (string[] args) {        int n = ten;        N+ +;        SYSTEM.OUT.PRINTLN (n);        N--;        N--;        SYSTEM.OUT.PRINTLN (n);    }}

You'll get 11 and 92 numbers, which are n self-increment and then subtract two times from the result. What, isn't it simple? Don't be too happy to look at this code, do you know what the result is?

 Packagepers.frank.test;ImportJava.lang.System; Public classTest { Public Static voidMain (string[] args) {intn = 10; intm = ten * n++; inti = m++ *--N; intj = ++i + ++m-n--;System.out.printf ("N:%d, m:%d, i:%d, j:%d", n,m,i,j); }}

You may have a head of grass and mud horse in your heart after you finish reading it. Don't worry, it's not hard. The self-increment character is different from the variable after it is placed before the variable. What's the difference? If it is placed before the variable, it means self-increment/decrement, and then use this value to participate in the operation, if it is placed after the variable, then the value of the expression is evaluated first, then the variable self-increment/decrement.

So, m=10*n++; this sentence runs when the value of N is 10, the expression is evaluated first, then the m=100, and then n is self-increment 1,n=11;

As for the next few operations, because it is related to the priority, so here for the time being not introduced, and so on after the priority level to do the explanation.

  2. Relational operators and logical operators

Relational operators include: = =,! =, >, <, respectively equals, not equal, greater than, and less than. This I would like not to introduce too much, mainly for the logic of judgment, in the back of the control process will be more detailed chestnut. For newcomers, it is important not to confuse the assignment = = with the relational operator = =. The logical operators are,&&, | |,! They are called and, or, not. or heel and both are binocular operators, that is, concatenate two expressions, Exp1 && EXP2, only if the values of two expressions are true, the value of the total expression is true, otherwise false. For example: 1>0 && 2<3 because the results of two sub-expressions are true, the value of the total expression is also true. And: 10>9 && 9<8; Because 9<8 is not true here, it is false, so the value of the total expression is false. For the OR operator, only two expressions are required, and any one expression is true, and the value of the total expression is real. Example: 10>11 | | 1<2; The total expression is true because the latter expression is true. For the no operator, this is a single-mesh operator that reverses the result of the expression and turns it into false and false.

Here is a table, you should see it.

If you really do not understand, it does not matter, the subsequent judgment process will have code to help you understand.

  3. Operator level

Operators are prioritized, just as the subtraction also has a first-and minus-multiplication order. Here is a table that lists the precedence of these operators, the smaller the priority value, the higher the precedence operation.

  

Maybe now there is not much concept for this, it's okay, we can better understand the code behind.

Now let's explain the expression of the previous WTF.

i=m++ *--n; expression from left to right operation, according to the priority, first calculate the self-subtraction operation, the value of n becomes 10, and then the multiplication, the value of I is 100*10=1000 again assigns the value to i;m again self-increment, the value is 101;

j = ++i + ++m-n--; In the same vein, I self-increment obtains the 1001,m self-increment obtains 102, calculates: 1001+102-10=1093;n again self-reduction obtains 9;

So, there's a little bit more about this priority,

 4. Enumeration type

  Finally, by the way, the enumeration type is defined by the enum keyword, and sometimes the value of the variable is in a finite set, which is usually a set of our own custom, such as a set of several colors, when we define an enumeration type, we declare that it has several elements, When we use this enumeration, we can only use its elements, no elements we are unable to use, the system will error.

For example: enum Color = {RED, GREEN, YELLOW}; Color MyColor = color.red;

Using enumerations has the following benefits:

    • You can reduce errors caused by digital transpose or typing errors.

    • It's easy to change the values later.

    • Make your code more readable, which means that the probability of errors in your code is reduced.

    • Ensure forward compatibility. Use enumerations to reduce the probability of code errors when someone changes a value corresponding to a member name in the future.

In fact, to really elaborate, the enumeration is not simple, you can customize the function in the enumeration, you can implement the interface, these and other later encountered in the code to do a detailed introduction.

At this point, the tutorial ends. You are welcome to continue to follow the next tutorial. If there are any errors found, please contact me in time, I will promptly correct the changes, but also hope that we have more suggestions, more exchanges.

Java 0 Basic Starter Series Day5 operators in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.