Java basic grammar
A Java program can be considered as a collection of objects, and these objects through method calls to each other to work together. Introduced under the concept of classes, objects, methods and instance variables below.
- Object: The object is an instance of a class, have state and behavior. For example, a dog is an object whose states are: color, name, breed; behavior: wagging its tail, called, eat and so on.
- Class: The class is a template that describes the behavior of a class of objects and status.
- Methods: The method is to conduct a class can have a number of ways. Logic operations, data modification, and all actions are completed in the method.
- Examples of variables: Each object has a unique instance variables, state of the object instance variables from these values decision.
The first Java program
Let's look at a simple Java program, it will print the string Hello World
Examples
/ * The first Java program
* Prints the string Hello World
* /
public static void main (String [] args) {
System.out.println ( "Hello World"); // print Hello World
}
}
Running instance »
The following will be gradually describes how to save, compile and run this program:
- Open Notepad, the above code added to it;
- Save the file name: HelloWorld.java;
- Open cmd command window, enter the location of the target file is located, assuming C: \
- In the command window, type javac HelloWorld.java press the enter key to compile code. If the code is not an error, cmd command prompt, enter the next line. (Assuming that the environment variables are set up).
- Then type java HelloWorld press the Enter key to run the program
You will see the Hello World in the window
C : > javac HelloWorld.java C : > java HelloWorld Hello World
Gif presentation:
The basic syntax
When writing a Java program, you should note the following:
- Case sensitive: Java is case-sensitive, which means that the identifier Hello and hello are different.
- Class Name: For all classes, the class name first letter should be capitalized. If the class name consists of several-word, then the first letter of each word should be capitalized, for example MyFirstJavaClass.
- Method name: All method names should be lowercase letter. If the method name contains several words, each word back initial capital letters.
- Source filename: Source file name must be the same as the class name. When you save the file, you should use the class name as the file name to save (remember Java is case-sensitive), the file name suffix as .java. (If the file name and class name are not the same will result in a compilation error).
- Main method entry: All Java programs by the public static void main (String [] args) method to begin.
Java identifier
All components of Java are required names. Class names, variable names and method names are called identifiers.
About Java identifiers, the following points should be noted:
- All identifiers should begin with a letter (AZ or az), dollar sign ($), or an underscore (_)
- After the first character can be any combination of characters
- Keywords can not be used as an identifier
- Identifiers are case sensitive
- Legal identifier, for example: age, $ salary, _value, __ 1_value
- Illegal identifier Example: 123abc, -salary
Java modifiers
Like other languages, Java can be modified using the modifier class methods and properties. There are two types of modifiers:
- Access control modifiers: default, public, protected, private
- Non-access control modifiers: final, abstract, strictfp
In the following sections we will discuss in-depth Java modifiers.
Java variables
Java, there are several types of variables are as follows- Local variables
- Class variables (static variables)
- Member variables (non-static variable)
Java arrays
Arrays are stored on the heap object, you can save multiple variables of the same type. In later chapters, we will learn how to declare and initialize an array structure.
Java enum
Java 5.0 introduced the enumeration, enumeration constraint variables can only be pre-set value. Using enumerations reduces code bug.
For example, we designed a program for the fruit juice shop, it will limit the juice of a small cup, medium cup, large cup. This means that it does not allow the customer in addition to the three point Dimensions juice.
Examples
class FreshJuice { enum FreshJuiceSize{ SMALL, MEDUIM, LARGE } FreshJuiceSize size; } public class FreshJuiceTest { public static void main(String []args){ FreshJuice juice = new FreshJuice(); juice.size = FreshJuice. FreshJuiceSize.MEDUIM ; } }
Note: enum declaration may be used alone or inside the class declaration. Methods, variables, constructors can also be defined in the enumeration.
Java keywords
The following lists the Java reserved words. These reserved words can not be used to name constants, variables, and any identifiers.
Keyword | description |
---|---|
abstract | Abstract methods, abstract classes modifiers |
assert | Assertion conditions are met |
boolean | Boolean data type |
break | Out of the loop or label snippet |
byte | 8-bit signed data types |
case | A conditional switch statement |
catch | With the exception of information capture and try |
char | 16-bit Unicode character data type |
class | Class definitions |
const | Unused |
continue | The remaining portion of the loop is not executed |
default | switch statement default branch |
do | Loop, the loop body is executed at least once |
double | 64-bit double-precision floating-point number |
else | When the branch condition is not satisfied if executed |
enum | Enumerated type |
extends | It represents a class is a subclass of another class |
final | It represents a value after initialization can not change the representation can not be rewritten, or a class can not have subclasses |
finally | , Mainly to the completion of code execution and design for robustness and integrity of the program, whether there are code execution exception occurs. |
float | 32-bit single-precision floating-point number |
for | for loop |
goto | Unused |
if | Conditional statements |
implements | It represents a class implements an interface |
import | Importing classes |
instanceof | Test whether an object is an instance of a class |
int | 32-bit integer |
interface | Define an interface, a type of abstract methods and constants only |
long | 64-bit integer |
native | Representation of non-java code |
new | Assign a new instance of the class |
package | A package consisting of a series of related classes |
private | Etc. represents private field or method can be accessed only from within the class |
protected | It indicates that the field can only access the class or subclass or subclass other classes in the same package |
public | Represents total property or method |
return | Method returns a value |
short | 16 digits |
static | Represented at the class level definition, shared by all instances |
strictfp | Floating-point comparison using strict rules |
super | It represents the base class |
switch | Select statement |
synchronized | Represent the same time can only be accessed by one thread block of code |
this | It represents the current instance of the call or call another constructor |
throw | Throw an exception |
throws | Definition method may throw an exception |
transient | Do not modify the sequence of the field |
try | It represents a code block to do and finally with exception handling or throw an exception indicating whether the code is executed finally |
void | Marking method does not return any value |
volatile | Flag field may be multiple threads access, do not sync |
while | while loop |
Java annotations
Similar to C / C ++, Java supports single-line and multi-line comments. Comment characters are ignored Java compiler.
public class HelloWorld { /* 这是第一个Java程序 *它将打印Hello World * 这是一个多行注释的示例 */ public static void main(String []args){ // 这是单行注释的示例 /* 这个也是单行注释的示例 */ System.out.println("Hello World"); } }
Java blank line
Blank lines, or annotated line, Java compiler will be ignored.
inherit
In Java, a class can be derived from the other classes. If you want to create a class, and already has a class property or method you need, then you can be a newly created class inherited class.
Use inherited methods, you can reuse the existing class methods and properties, without rewriting the code. Inherited class is called a superclass (super class), the derived class is called a subclass (subclass).
interface
In Java, understood as the interface protocol between objects communicate with each other. The interface plays an important role in the succession.
Interface defines a method to use is derived, but the specific implementation of the method depends entirely on the derived class.
The next section describes the Java programming classes and objects. After you will have a clearer understanding of Java classes and objects.