First line of Java code

Source: Internet
Author: User
Tags arithmetic logical operators modifiers

One, the first line of Java code
 Package Com.hello.main;  Public class Main {        publicstaticvoid  main (string[] args) {        System.out.println ( "Hello Word");}    }

Package: Bag, you can understand as bag, purse, girlfriend's small bag. A package is used in Java to store different code. Why do you have this stuff? Think about, when you go to school, Schoolbag is what to do, used to put books, girlfriend's small bag to do what, let go machine cosmetics. The package in Java is used to put Java source files. If you don't mind, please think about the function of the schoolbag.

Public: Publicly available, which represents the access modifier in Java. What do you mean, access modifiers? Everywhere in life, there is no oil at home, you object to let you go to buy oil, supermarket is about 2 kilometers away from your home. You should not choose to open 11th, you just happened to have a shared bike downstairs, you opened a car. Riding the car to the supermarket, buy things out of the law found that the car was gone, was someone else rode away. Why? This car is not yours, his ownership is not you, everyone can use, is public. And you can open the other one and ride home. This is called public. Access modifiers are used in Java to represent the accessibility of this class or method property.

Class: Don't think much, this is the simplest thing in reality. Birds of a feather, flock together. You can think about the primary school exercises, which of the following objects do not belong to animals, A, husky, B, Tiger, C, cat, D, banana. Which would you choose? Why?

Static: Stationary, dynamic and static, Yin and yang. Since ancient times, have been fastidious about a balance. Java is also, static means that this method can not be instantiated is not a dynamic object access, so you can understand, eat this thing only you personally to eat, you let others to eat, the result is others eat, you are hungry.

void: Empty, the family pay attention to trophyless, empty is the color, color is empty. Empty, nothing. You can understand that the use of the Void keyword in C #, Java means that a method does not have any return value. This means that the method is clean after execution.

System.out.println (): The output method of the system. Talk about this in depth later.

Second, comments in Java

1. Documentation Note:/***/, which is used to generate documents that describe what this class, method is used to do, and who developed it. In reality, it's a manual.

2, single-line comment://, represents a single line of comments, one line at a time, used to briefly describe the function of a called method or block of code, when translating classical Chinese, the vernacular of a classical Chinese word, is the line of comments

3, block comment:/**/, for multi-line comments, quickly commented out multiple lines of code. A pie will fall off here.

     

 Package Com.hello.main; /**  *   @author  * * Publicclass  Main        {/     * * Here is a multiline comment     *       */     Public Static void Main (string[] args) {        //  output content        System.out.println ("Hello Word");}    } 
Third, data types in Java

Data type, you can understand the different size of the box, in Java Why do you have this thing? Imagine life without clinker bags, boxes and other containers. In Java, different data types are used to load different content data.

Number type:

1, byte byte, size 1, can understand the chicken peck rice, each peck a grain of rice. Range -127~128

2 short integer, size is 2 bytes, equivalent to the capacity of a spoon. Range, -32768~32767

3, int integer, size 4 bytes, equivalent to a bowl, range -2147483648~2147483647,

4, long length integer, the size of 8 bytes, the equivalent of a Guo, the range of -9223372036854775808~9223372036854775807, to L or L end;

Memory Method: You eat in the small yard at home, next to a chick is pecking you dropped on the ground of the rice (byte), you crazy with a spoon (short) in the bowl (int) to send the meat to the mouth, the rice is glued to the face, the left one to the right, mom saw you are smiling, kindly say, do not hurry pot ( Long) and.

Floating point type:

1, float,4 bytes, the equivalent of a jar of honey, the number of effective 6-7 digits. end with F or F

2, double,8 bytes, equivalent to oil barrels, the effective number of 15 bits. End with D or D,

Both float and double represent decimals, and the difference is how many bits can be retained after the decimal point.

Memory Method: Imagine the oil barrels and the honey jar, pour the oil and honey out, which residue more.

Character type:

1, char type, which represents a character with a size of 2 bytes, representing a Unicode encoding. such as storing a

Boolean type:

1, Boolean: Boolean type, True or false, generally your daughter-in-law asked you to hide money, the answer is the Boolean type.

   String is not a basic data type

     Common Interview Questions:

1. Please describe the basic data types in Java, and introduce the usage rules separately.

    /*** Data Type*/     Public Static voidDataType () {//Numeric Type        byteb =1;  ShortS =10; intI =100; LongL =100l; //floating Point Type        floatF =12.1235f; DoubleD =123.123423d; //Character Type        Charc = ' A '; //Boolean type        BooleanBl=true; }
Iv. variables

Declaring variables: In fact, it is to prepare a box of different capacity to load things. For example, int A;char C and so on, the variable is the value, the reality is a container for different things, such as you eat the potato chips box, perhaps you will be used to act as a pen holder. The contents of the container change at this time.

Initialize variables: The box has, but not loaded, when you use these boxes to find that the box is empty, so the box is defined, it is necessary to put things in, Java does not allow you are declared but not assigned variables. Imagine the potato chips you bought, opened and found to be empty. Generally not so, you buy back must be filled with potato chips.

Five, constant

Constants: The amount of non-changeable, that is, the container will not change after loading content, such as crystal bottles, before the factory filled with water and other objects, you can no longer replace the content inside (in reality, ideal), Java uses the final keyword to modify the immutable amount.

Final: The ultimate, immutable, meaning that you follow your father's surname. There is a hole in the back.

Vi. operators

Primary schools have studied, + 、-、 x, ÷,% for remainder

   Note: Just like in mathematics, integers cannot be 0,java except for 0, which is an exception, and the floating-point number is infinitely large or Nan (not-a-number) except for 0;

    /**      * Arithmetic     * /public     staticvoid  Math ()        { int a =10;         int b =2;        System.out.println (a/b);        System.out.println (a%b);        SYSTEM.OUT.PRINTLN (A*b);        System.out.println (a-b);        System.out.println (a+b);    }

VII. Type conversion

1, large type to small type, must be forced to carry out, you met a Jinshan, but you have only a small pocket, you can only part of the rest of the throw away, Java range of large types into small type will lose precision.

2, small type to large type, automatic conversion, the honey into the oil barrels, when the arithmetic, Java will be onboth sides of the operand processing, as long as the type is different, and there is a large type, then the data are converted to large type and then the operation, so the result is also large type.

    /**      * Type conversion     * /public     staticvoid  changetype () {         // automatic type conversion        byte b =10;         int a =b;         // forced type conversion, loss        of precision  Short s = (short) A;    }
Eight, assignment operators

The equals sign is not called the equals sign in the computer's world, but is called the assignment operator, which assigns the result of the right operation to the left variable, in reality, making the cake, mixing the egg and flour, and then putting it into the cake mold. For example: int a=10;a=a+3.4 will throw an exception, from the type conversion you can know why.

   Abbreviated form: int a =10; a+=2.3; no exception, why? Because the sentence is actually such a a= (int) (a+2.3);

Self-increase and self-reduction: ++a,a++, the expression of self-increase, is actually a=a+1, the same--a,a--, is self-reduction, that is, a=a-1;

Difference: If a single self-increment, self-subtraction statement, there is no difference. However, there is a difference in the nesting operation .

            Gaga, minus the first, the self-increment self-decrement element, in the participation operation,

Gaga, minus minus in the first part of the operation, in the self-increment and self-subtraction operation

Interview questions:

1, please calculate the following results.

int a =b=c=10;

A=++b;

c=a--;

b=++a+c--;

2, int a =10;a+=1.0; a=a+10.0; Please say the value of a;

IX. relational operators and logical operators

Relational operators, popular speaking is compared, as the saying goes, there is no harm compared. In reality, every day we are comparing, shopping, when selecting objects, and so on, Java provides six ways to compare.

1, = =: often used to compare the value is equal, the comparison value is equal. can only be used for comparison of value types.

2,! =: Judging two values do not want to wait, such as 3! = 9;

3, <, >, <=, >= and mathematics are more consistent.

Logical operators: Certain conditions are met to perform certain operations.

&&: And, it can be understood to meet two conditions at the same time, for example, through mobile phone Internet, need to meet, mobile phone networking, and mobile phone without damage.

|| : Or, when any one of the two conditions is met, it returns true, for example, on the Internet through the mobile phone, when there is no 4G can be through the WiFi internet, no wifi when the Internet can be 4G.

! : No, returns true if the condition is not met. For example, in the case of no network to play online games through the mobile phone, must not play.

Features: The logical operator has a short-circuit effect.

1, && When the first condition is false, the second condition is not judged. Direct return False

2, | |, when the first condition is true, does not operate on the second condition and returns true directly.

 Public Static voidBooltype () {intA=Ten, b= One; System. out. println (a>9&&b< A);//trueSystem. out. println (a<Ten|| B>Ten);//trueSystem. out. println (! ( A>0));//false}

    ternary operator: X<y?x:y, is it true? Really I like you, fake I hate you.

& Like, | or, ~, and logical operators, the difference is that there is no short-circuit effect. Will certainly calculate two worth of results.

   Features: A number operation two times the result is he itself. If the 12&12&13 result is 12

First line of Java code

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.