Home > Java > javaTutorial > JAVA tutorial | basic syntax

JAVA tutorial | basic syntax

黄舟
Release: 2017-02-25 09:33:30
Original
1789 people have browsed it

Java basic syntax

A Java program can be thought of as a collection of objects, and these objects work together by calling each other's methods. The following briefly introduces the concepts of classes, objects, methods and instance variables.

  • ##Object: An object is an instance of a class and has state and behavior. For example, a dog is an object. Its status includes: color, name, and breed; its behaviors include: wagging its tail, barking, eating, etc.

  • Class: A class is a template that describes the behavior and status of a type of object.

  • Method: Method is behavior, and a class can have many methods. Logical operations, data modification, and all actions are completed in methods.

  • Instance variables: Each object has unique instance variables, and the state of the object is determined by the values ​​of these instance variables.

The first Java program

Let’s look at a simple Java program. Will print the string

Hello World .

public class HelloWorld {
    /* 第一个Java程序
     * 它将打印字符串 Hello World
     */
    public static void main(String []args) {
        System.out.println("Hello World"); // 打印 Hello World
    }
}
Copy after login



The following will introduce step by step how to save, compile and run this program:

  • ##Open Notepad and add the above code;

  • Save the file name as: HelloWorld.java;

  • Open the cmd command window , enter the location of the target file, assuming it is C:\

  • Type javac HelloWorld.java in the command line window and press the enter key to compile the code. If there are no errors in the code, the cmd command prompt will go to the next line. (Assuming the environment variables are all set).

  • Type java HelloWorld and press Enter to run the program

  • You will be in The window sees Hello World.

C : > javac HelloWorld.java
C : > java HelloWorld 
Hello World
Copy after login



##Basic Grammar


When writing Java programs, you should pay attention to the following points:

  • ##Case Sensitive: Java is case sensitive, which means that the identifiers Hello and hello are different.

  • Class name: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, for example, MyFirstJavaClass .

  • Method name: All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.

  • Source file name: The source file name must be the same as the class name. When saving the file, you should use the class name as the filename (remember Java is case-sensitive) and the filename suffix .java. (If the file name and class name are different, a compilation error will occur).

  • Main method entrance: All Java programs consist of public static void main(String []args)method Begin execution.



##Java identifier



All components of Java need names.

Class names, variable names, and method names are all called identifiers.


## Regarding Java identifiers, there are the following points to note:


    All identifiers should start with letters (A-Z or a-z), dollar signs ($), or underscores (_)Start
  • The first character can be followed by any combination of characters
  • key Words cannot be used as identifiers
  • Identifiers are case-sensitive
  • Legal Examples of identifiers: age, $salary, _value, __1_value
  • ## Examples of illegal identifiers: 123abc, -salary



#Java Modifier


Like other languages, Java can use modifiers to modify methods and properties in a class. There are two main types of modifiers:

(scope)



  • 访问控制修饰符 : default, public , protected, private

  • 非访问控制修饰符 : final, abstract, strictfp 




Java变量

Java中主要有如下几种类型的变量

  • 局部变量

  • 类变量(静态变量)

  • 成员变量(非静态变量)



Java数组


数组是储存在堆上的对象,可以保存多个同类型变量。在后面的章节中,我们将会学到如何声明、构造以及初始化一个数组。




Java枚举


Java 5.0引入了枚举,枚举限制变量只能是预先设定好的值。使用枚举可以减少代码中的bug。


例如,我们为果汁店设计一个程序,它将限制果汁为小杯、中杯、大杯。这就意味着它不允许顾客点除了这三种尺寸外的果汁。


注意:枚举可以单独声明或者声明在类里面。方法、变量、构造函数也可以在枚举中定义。

class FreshJuice {
   enum FreshJuiceSize{ SMALL, MEDIUM , LARGE }
   FreshJuiceSize size;
}
 
public class FreshJuiceTest {
   public static void main(String []args){
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice. FreshJuiceSize.MEDIUM  ;
   }
}
Copy after login




##Java keyword


Java reserved words are listed below. These reserved words cannot be used in the names of constants, variables, and any identifiers.

Keyword Description
abstract Abstract method, abstract class modifier
assert Assert whether the condition is met
boolean Boolean data type
break Break out of the loop or label code segment
byte 8-bit signed data type
case A condition of the switch statement
catch Combined with try to capture exception information
char 16-bit Unicode character data type
class Definition class
const Unused
continue Do not execute the remainder of the loop
default Default branch in switch statement
do Loop statement, the loop body will be executed at least once
double 64-bit double precision floating point number
else The branch executed when the condition is not true
enum Enumeration type
extends Indicates that one class is a subclass of another class
final Indicates that a value cannot be changed after initialization
Indicates that the method cannot be overridden, or a class cannot have subclasses
finally Designed to complete the executed code, mainly for the robustness and stability of the program Integrity, code is executed regardless of whether an exception occurs.
float 32-bit single precision floating point number
for for loop statement
goto Not used
if Conditional statement
implements Indicates that a class implements the interface
import Import class
instanceof Test whether an object is an instance of a class
int 32-bit integer
interface Interface, an abstract type with only definitions of methods and constants
long 64-bit integer
native Indicates that the method is implemented in non-java code
new Allocate a new class instance
package A series of related classes form a package
private indicates that private fields, methods, etc. can only be accessed from within the class
protected indicates that the field can only be accessed from within the class Access via class or its subclass
Subclasses or other classes in the same package
public represents shared properties or methods
return Method return value
short 16-digit number
static means in Class level definition, shared by all instances
strictfp Floating point comparison uses strict rules
super Indicates the base class
switch Select statement
synchronized Indicates only the same time A block of code that can be accessed by a thread
this represents a call to the current instance
Or call another constructor
throw throws exception
throws define method possible Thrown exception
transient Modify fields not to be serialized
try Indicates the code If the block needs to handle exceptions or cooperate with finally to indicate whether an exception is thrown, the code in finally will be executed
void The marked method does not return any value
volatile Marked fields may be accessed by multiple threads at the same time without synchronization
while while loop





#Java Notes


## Similar to C/C++ , Java also supports single-line and multi-line comments. The characters

in comments will be ignored by the Java compiler

.



#Java Empty Line


##Blank lines or lines with comments will be ignored by the Java compiler.



#Inheritance


#In Java, a class Can be derived from other classes. If you are creating a class and there is already a class that has the properties or methods you need, then you can inherit the newly created class from that class.


Using inherited methods, you can reuse methods and properties of existing classes , without having to rewrite these codes. The inherited class is called a super class, and the derived class is called a subclass.

interface



In Java, an interface can be understood as a protocol for communication between objects. Interfaces play a very important role in inheritance.


##

The interface only defines the methods to be used by the derived class, but the specific implementation of the method completely depends on the derived class.


#Java source program and compilation The difference in operation


## is shown in the figure below:


# The above is the content of JAVA entry tutorial | basic syntax. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template