The basic syntax -java

Basic grammar

Java programs can be regarded as a collection of objects, and these objects by calling each other's work methods.

  • Object: an instance of a class, we have state and behavior. Specific to a certain thing.
  • Class: The class is a template that describes the behavior and state of a class object.
  • Methods: that behavior / action, there are many ways a class. Logical operation, data modification, etc.
  • Examples of variables: each object has a unique instance variables, the state of the object is determined by the instance variables.
  • The basic syntax
  • Identifier
  • Modifiers
  • variable
  • Array
  • enumerate
  • Keyword

Continue Hello World!

    / **
     * This is the Javadoc
     * @author Developer
  * / public class the HelloWorld { public static void main ( String [] args ) { / * * This is a multi-line comment * * / System . OUT . Println ( "the Hello World! " ); // print the character phrase; it is a single line comment } }


The basic syntax

 


  1. Pay attention to grammar

    • Case-sensitive, such as an identifier Hello and hello are not the same
    • Class name, the first letter capitalized, if the stitching together of multiple words, each of the first letter should be capitalized
    • Method names, method names should all start with a lowercase letter, if a plurality of words from the beginning of the second word capitalized 1
    • Source file name must be the same as the class name. File name suffix.java
    • Method main entrance, all the Java programs public static void main(String[] args){}start

Identifier

All components of the Java name is required. Class names, method names and variable names are all identifiers.

  • All identifiers should be based on capital and lowercase letters, the dollar sign ($) or an underscore (_) begins.
  • After the first letter may be the case any combination of letters, dollar signs, underscores, or numbers.
  • Keywords can not be used as identifiers

Exercise : Find the following correct and incorrect identifier

- - - -
MyCar .student $money checkPeople
wodeqiche Student _youqian $ _Zhaoren
1mycar daxueshen_g youqian + 2 zhao1geren

Modifiers

Java can be modified using the modifier class methods and properties

Access control modifiers: default public protected private
non-access control modifiers: final abstract static sunchronized

variable

  1. Local variables
  2. Class variables (static variables)
  3. Member variables (non-static variables)

Array

Arrays are stored on the heap of objects, you can save multiple variables of the same type.

enumerate

java 5.0 introduces enumeration, limit variables can only be a preset value.

Examples

 class Word{
    enum WordN{NIHAO, HELLO, HI} WordN size; public class WordTest{ public static void main(String[] args) { Word w = new Word(); w.size = word.WordN.HI; } } } 

 

Keyword

Keywords and reserved words can not be used to name

Types of Keyword Explanation
Access control private private
protected be protected
public public
Class, method and variable modifiers abstract Abstract statement
class class
extends Expansion inheritance
final The final value can not be changed
implements Implement (Interface)
interface interface
native Local, native method (non-Java implementation)
new New, created
static Static state
strictfp Strict, precise
synchronized Threads, synchronization
transient short
volatile Volatile
Program control statements break Out of the loop
case Selecting a defined value for the switch
continue carry on
default default
do carried out
else otherwise
for cycle
if in case
instanceof Examples
return return
switch Execution based on the value selected
while cycle
Error Handling assert Whether the assertion expression is true
catch Catch an Exception
finally There are no exceptions to perform
throw Throw an exception object
throws Declare an exception might be thrown
try Catch the exception
Package Dependencies import Introduced
package package
basic type boolean Boolean
byte Byte
char Character
double Double-precision floating point
float Single-precision floating point
int Integer
long Long integer
short Short integer
Variable reference super The parent class, the superclass
this This class
void No return value
Reserved Keywords goto Reserved keyword
const Reserve
null air

  1. Camel nomenclature 

Guess you like

Origin www.cnblogs.com/bomily0212/p/12082957.html