Java Basics 2-Java basic grammar

type of data

image

The basic components of the program 1.Java

  • Keywords: been given specific meaning of the word Java language, not as identifiers, such as private.
  • Identifier: a string of numbers, letters, and $ _ composed of a reference variable. And the first letter can not be a number.
  • Variables: during program execution, within a range of values ​​of the amount of change.
  • Constant: The value does not change during execution, into literals and custom constants, such as 3.14.
  • Expressions: Java is used in infix expression. The double a + (bc) / 2.

2. The basic data types:

Types of

Take up storage space

range

byte

1 byte

-128~127

short

2 bytes

-2^15~2^15-1

int

4 bytes

-2^31~2^31-1

long

8 bytes

-2^63~2^63-1

float

4 bytes

-3.403E ~ 3.403E38

double

8 bytes

-1.798E308 ~ 1.798E308


Type conversion:

  • Default Conversion: byte, short, char-> int-> long-> float-> double where byte, short, char can not be converted between each other, they are involved in computing first converted int type. Assignment expression with the variable operation if the calculated force, compilation failure occurs. Such as
  • image
  • Cast: variable name = target type (target type) (converted data); converted to binary considered. For example, to print byte a = 130, may be utilized to achieve cast.

3. Operator

Operator precedence table

image

(1) arithmetic operators (+, -, *, /,%, +, -)


         A: + Usage
             a: addition
             b: plus
             c: string connector (p + is overloaded)
         B: /% and the difference between
             a: / division between integers, the result is an integer.
                 Rounding to 0
                 If a decimal want, to * 1.0.
             b:%   
                 determines whether a number is odd
                 a% b = a - (a / b) * b
                 modulo operation is a very time-consuming operation, it is best not to use more critical performance when
          D: ++ and - usage of
             a: their role is to increment or decrement

             b: use
                 ** alone
                     in front of and behind the operation data the same effect.
                     a ++ or ++ a same effect.
                 ** operation involved in using
                     the front on operands: first increment or decrement, and then participate in the operation   
                     Later on operands: the first involved in the operation, and then increment or decrement
             c: underlying principle:
                 A = A ++;   
                 int TEMP = A;
                 A ++;
                 A = TEMP;
   

(2) assignment operator (=, + =, - =, * =, / =,% =, etc.)


         A: = assignment operator is called, is the most basic assignment operator
             int x = 10; 10 assigned to the int type variable x.
             a = b = 10;
             assignment operator is bonded right to left.
             Assignment expression has value, value = value equal to the right expression.
         B: Extended assignment operator characteristics
             implied auto-cast.
             Interview questions:
                 Short S = 1;
                 // S = S + 1;
                
                 Short S = 1;
                 S + = 1;
                 Will the code above which there is a problem?

(3) a comparison operator (==,! =,>,> =, <, <=, Instanceof)


         A: No matter how simple or complex operator ends the final result is a boolean type.
         B: Do not put == written =

(4) logical operators (&, |!, ^,, &&, ||)


         A: The logical operators for connecting the boolean expression
         B: Conclusion
             &: there is false false
             |: There is true true
             ^: the same is false, then a different true.
                 The couple's relationship.
             !: Non-true is false, false if non-true
            
             &&: & conclusions and is the same, but there is a short circuit effect. The left is false, the right is not executed.
             ||: Conclusion and | is the same, but there is a short circuit effect. The left is true, the right not to execute.
             Recommended &&, ||
   

(5) Bitwise Operators (&, |, ^, ~, <<, >>, >>>)


         A: Conclusion:
              &: are 1, it is a 1, otherwise 0
              |: simultaneously 0, it is 0 otherwise 1
             ^: the same is 0, the different is 1;
             A ^ B = B ^ A;
             A ^ a = 0;
             a ^ a = 0;
             ~: 1 ~ 0, 1 ~ 0;
            <<: high discarding removable, low 0s;
             left n units, corresponding to n ^ multiplied by 2;
           > >: low discarding removed, if the sign bit is 0, the high bit of 0, if the sign bit is 1, the high bit of 1;
             right by n units, the equivalent of dividing the n ^ 2;
          >>>: removing discarding the low, high fill 0;

        Note: how a mobile unit exceeds the word length m, the actual movement of
             a mod m (modulo computation mathematics)
         B: interview questions
             a: determining whether a number is odd ==. 1. 1 & a;
             B: for take more than 2 ^ n. & A (n-2 ^ -. 1);
             C: How to determine whether a number is n-A & ^ 2 (. 1-A) == 0
             D: exchange between two integers;
             E: There are a bunch of numbers, are paired appears, only a single, individual identify the number of
             f: evaluation 8 2 * 2 << 3 with the most efficient way;

(6) ternary operator


         Format A:
             Comparative Expression Expression 1: Expression 2;?
         B: execution process:
             First, calculation and comparison of the expression values, to see is true or false.
             If true, the expression 1 is the result.
             If it is false, the expression 2 is the result.
         C: Case:
             A: Comparison of two data equality
             b: acquiring the maximum of two data
             c: acquiring three data maximum value
                 int max = (a> b a :? B)?> C (a> ? B A: B): C;
                 int max = A> B (A> C A:?? C): (B> C B: C);?

Statement

  1. Declare statement

Create some type of variable and give it a name with an identifier, Java is a strongly typed language, the compiler checks the type of consistency. So the first time you use a variable must be declared. As int a, b;

  2. assignment

The c = 5, 5 that is assigned to a variable of type int constant C, the left side of the assignment statement must be a single variable, the right side of the expression may be any appropriate type of value can be obtained.

  3. Conditional statements

One block of code in accordance with the branch condition established, and if there are two kinds of sentence structure swich

The if statement

if(<boolean expression>)
{
  <block statements1>
}else
{
  <block statements2>
}

switch statement

switch(<boolean expression>)
{
  case value1:
       <block statements1>
       break; // switch statement can end
  case value2:
       <block statements2>
       break;
  default: // When all cases that do not match, on the implementation of the contents of the premises
       <block statements>
       break;
}
  • If the result of the relationship between the expression must be a boolean type
  • The value of the switch expression relationship can be: byte, short, int, char
    • After JDK5 can be enumerated, JDK7 can later be String
    • Behind the case can only be a constant, not a variable. default, break can be omitted, but not recommended
  • break and continue statements
    • break: an immediate exit from the loop
    • continue: immediately begin the next round cycle

  4. Loops

while statement:

while(<boolean expression>)
{
  <block statements>
}
Loop in the code segment called loop.

for statement:

In a more compact way to express the while loop
for(<initialize>; <boolean expression>; <increment>)
{
   <block statements>
}

  5. Jump control statements

    break interrupt

    • Usage scenarios: switch statement to select the structure, loop, other scenes meaningless, will complain
    • Action: single cycle out (if a multilayer, only out layer), out of the multilayer loop, with the tag statement is required

    continue continue

    • Usage scenarios: loop
    • Role: Exit this iteration, start the next iteration (cycle time)

    return return

    • One way to end the event of return, the method will not continue to the next execution

Array

  1. Concept:

  • An array of a plurality of the same data type is a set of storage elements

  2. Static Initialization:

  • Given value, the array system in Java must be initialized before use, it normally takes three steps (name and type declaration of the array, create an array, initialize the array elements)

  3. Dynamic initialization:

  • The default value is given only to the length of the system. The double [] a = new double [N]; // declare the array equals the left side, the right to create an array. At this time, a default initial value of 0.0.
  The compile-time array initialization. The int [] a = {1, 1, 2, 5, 8};

  4. The two-dimensional array:

  • Two-dimensional array is an array of one-dimensional array, such as double [] [] a = new double [M] [N];

method

  1. meaning:

    Is the completion of a particular function block, in Java, similar functions in other languages, are called methods.
  2. format:

               Modifier return type method name (parameter type parameter name 1, parameter type parameter name 2 ...) {
                               method body;
                              return value return;
                }

        Modifiers : Currently on the use of public static. And then other modifiers later explain in detail
         the type of the return value : the result is a function of the data type
        method name : that has a name, we call this convenient method.
        Parameter Type : data type parameter is a
        parameter name : variable name is
        parametric classification :
            Parameter method call transfer: arguments
            parameter: defining mode, defined parameter.
        Method body statement : function code block is complete
        return : the end of the method, and the calculated result back to the caller.
        Return value : the function is the result of a return to bring the caller.

     3. Nature:

         Method parameters are passed by value:

    • Method parameters and local variables of the same process variables, the only difference is the initial value of the variable parameter is provided by the caller. The method of treatment is the value of the parameter, rather than the parameter itself. The results generated in this way is to change the value of a parameter variable in a static method has no effect on the caller.

         Method name can be overloaded:

    • The so-called heavy-duty, refers to the same class, there is a method to allow more than one of the same name, as long as the number of their parameters, parameter types, different parameters order.
    • Method overloading has nothing to do with the return value type, just look at the method name and parameter list (virtual machine to distinguish between the different methods of the same name by the argument list when calling)

          The method can return only one value, but can contain multiple return statements:

    • A Java method can return only one value, its type is the type declared in the method signature, such as double, int, and so on. Any static method will always return a value, a parameter of the first return statement ie execution.

           The method can produce side effects:

    • The return value may be a void, which indicates that the method returns no value. After the set of methods of control after a statement is finished it will be returned to the caller. We call void type of static methods cause side effects (accepts input and generates output, modify the array or change the system state).

Guess you like

Origin www.cnblogs.com/EthanWong/p/12464871.html