Dark Horse Programmer--java Basic Syntax (i)---keywords, identifiers, annotations, constants and variables, operators

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

This post mainly summarizes the basic Java syntax, including keywords, identifiers, annotations, constants and variables, and operators.

First, the key word

Keywords are words that are given special meaning by the Java language. All the letters in the keyword are lowercase.

Keywords in Java mainly include the following parts:

1. Keywords for defining data types

  

Application Data Type: Class (Class) interface (interface)

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

void (definition function u does not return value type)

2, to define the data type is worth the keyword

  

Two values of type Boolean, True (True), False (False)

Null indicates that the application type points to an empty object.

3. Keywords for defining Process Control

  

Mainly used to make judgments, cycle control and other key words, each of them are very familiar with the introduction of not one by one.

Judgment: If else switch case default

Loops: For and do

Continue loop: Continue

End loop or select structure: Break

Returns: Return

4. Keywords used to define access rights modifiers

  

Private (privately) protected (protected) public

5. Keywords for defining classes, functions, variable modifiers

  

Abstract final (final) static (static) synchronized (synchronous)

6. Keywords used to define the relationship between classes and classes

  

Extends (inheriting class) implements (implementing interface)

7, to define the establishment of instances and reference instances, to determine the key words of the instance

  

New (build instance) this (current reference) Super (parent class reference) instanceof (Judging object type)

8. Keywords for exception handling

  

Try (check exception) catch (Catch Exception) finally (final execution)

Throw throws: More than two means throwing exceptions, but using different locations

9, for the package of keywords

    

Package (Create packages) import (Import package)

10. Other modifier keywords

  

Native (local)

STRICTFP (Strict float point, precision float)

Transient (variable modifier, which declares an instance variable, is not serialized when the object is serialized)

Volatile (type modifier, used to decorate variables that are accessed and modified by different threads)

ASSERT (assertion, which can be thought of as an advanced form of exception handling, captures the assumptions we make in our code)

Note: Main is not a keyword, but it is a name that is recognized by the virtual machine.

Second, identifiers

  Identifiers are some of the names defined in the program. It is made up of 26 English letters, numbers: 0-9, Symbols: _, $, and numbers cannot begin with identifiers.

Naming rules for identifiers in Java

1. Package Name: All letters are lowercase when multiple words are composed. such as: Xxxyyyzzz.

2. Class name Interface name: When multiple words are composed, the first letter of all words is capitalized. such as: Xxxyyyzzz.

3. Variable name and function name: When multiple words are composed, the first letter is lowercase and the second word begins with the first letter of each word. such as: Xxxyyyzzz.

4, constant name: All letters are capitalized. For multiple words, each word is connected with an underscore. such as: xxx_yyy_zzz

Third, comments

  The annotation is used to explain the procedure, and the annotation exists in order to improve the readability of the program. We write the program not only to show themselves, but also to others to see, so good annotation habits can greatly improve the readability of the code, the work of great benefit.

There are three main types of annotations in Java:

1, single-line comment, format is//comment text

2, multi-line comments, format is/* Comment text */

3, document comments, format is/** note text */

There are several important points of knowledge that we need to master when commenting:

1. For single-line and multiline comments, the annotated text will not be interpreted by the JVM (Java Virtual machine). So, even if you add more comments, the compiled class file takes up the hard disk bytes.

2, for the document comments, is a Java-specific note, where the content of the note can be provided by the JDK tool Javadoc, to generate a set of Web files as a description of the program's documentation.

3, the comment is a programmer must have the good programming habit. Beginners must develop the habit of writing a program: write the comments before writing the code.

4, the idea of their own through the comments first organized, and then the code to reflect, because the code is just a form of reflection of thought.

Iv. Constants

  Constants are values that cannot be changed, and constants in Java include:

  1, integer constant: all integers.

2, Decimal constants: All decimals

3. Boolean constant: True,false

4. Character constant: A numeric letter or symbol is identified by a single quotation mark ("'), such as: ' A '.

5. String constants: One or more characters are identified by double quotation marks (""), such as "HelloWorld", "a", "" "(empty string).

6, NULL constant: Only one value is: null.

V. variables

  A variable is a storage area in memory that has its own name (variable name) and type (data type), and the region's data can change continuously within the same type. The characteristic of variables is to store uncertain data, that is, to open up a space in memory.

The Java language data types include 8 basic types and 3 reference types.

  

  The Java language is a strongly typed language that defines specific data types for each data and allocates different sizes of memory space in memory.

Byte 1 bytes

Short 2 bytes

int 4 bytes

A long 8 bytes

Float 4 bytes

Double 8 bytes

Char 2 bytes

Boolean 1 bytes

Note: integer default type: type int, decimal default type: Double type.

The type conversion of variables is a common problem in our development process, which is divided into two types, automatic type conversion (also called implicit conversion) and coercion type conversion.

The following is an example of an automatic type conversion:

1 classCastdemo2  {3       Public Static voidMain (String[]args)4     {5          intX=3;6          byteB=5;7X=x+b;//A variable of type int is 4 bytes, and when a variable of type byte is added to it, the variable of type byte is automatically converted to the 4-byte int type, and then the addition operation is performed. 8 System.out.println (x);9      }Ten}

The result here is output 8.

The following is an instance of a forced type conversion:

1 class  2{3public     staticvoid 4     {  5         System.out.println ((char) (' a ' + 1));   ' A ' +1 implements an automatic type conversion with the result of a value of type int, which is cast by (char) to a value of type char.  6    }7 }

The result of the output is B:

  

In the expression, the data type can be automatically converted, the law of automatic conversion is as follows:

1. All the values of byte, short, and Char will be promoted to the int type.

2, if an operand is a long type, the result is long.

3, if an operand is a float type, the result is a float type.

4, if an operand is a double type, the result of the calculation is double type.

Vi. operators

  

The operators above are those that we often use in programming.

Here is a question that often arises during an interview.

Is the following code wrong? If there is a mistake, indicate the error. If not, please indicate the result of the operation.

 1  class   Castdemo  2  3  public  static  void   main (string[] args)  4   { 5  short  temp=3; 6  temp=temp+3;  7   System.out.println (temp);  8   9  }

The above code compiles with an error:

  

The reason is simple: we mentioned in the above statement that in the short type of variable temp in and integer constant 3 operation is a type promotion, the result becomes an int type, at this time the result of the int type is assigned to the short type of variable temp compilation error.

Here is still very simple, then change the topic:

Changed from Temp=temp+3 to Temp+=3; Will the results be the same?

 1  class   Castdemo  2  3  public  static  void   main (string[] args)  4   { 5  short  temp=3; 6  temp+=3;  7   System.out.println (temp);  8   9  }

Let's run a look at the results:

  

Compile without error, the result output is 6,

The reason for this is that when executing a s+=4 statement, the compiler, at compile time, enforces type conversions by default, which translates data of type int into data of type short.

  logical operators

About && &, | | The same points and different points as |

&&, | | and & | The result is the same, but the process of the operation is different. &&: When the left is false, the right side does not participate in the operation, when the left is true, the right side does not participate in the operation, this can improve efficiency. and |, & no matter what the results on the left, the right side is involved in the operation. Use of && | | More efficient than using & and | In addition, & and | can also participate in bitwise operations.

The most classic problem in a bitwise operation is that it does not use a third-party variable to realize the exchange of two variables. The code is as follows:

1 classOperatordemo2 {3      Public Static voidMain (string[] args)4     {5                 //define two variables to swap with each other6          intA = 3,b = 5;7 8System.out.println ("a =" + A + ", B =" +b);9                //Continuous XOR operation for interchangeTenA = a ^b; Oneb = a ^b; AA = a ^b; -  -System.out.println ("a =" + A + ", B =" +b); the      } -}

The output is:

  

Here the first Java basic syntax is summed up, although simple, but there are a lot of small points worth pondering, especially the data conversion this piece, if you do not understand the principle, some code and results appear to be inexplicable, but once we really mastered, look at the past at a glance, simple and very interesting.

Keep trying to refuel! For tomorrow's better self.

Dark Horse Programmer--java Basic Syntax (i)---keywords, identifiers, annotations, constants and variables, operators

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.