0 Basic Learning Javase (ii)--Basic grammar

Source: Internet
Author: User
Tags modifiers throw exception

Java Basic Syntax 2.1 Java BASIC syntax

Java is an object-oriented programming language, and everything can be treated as objects, and Java is the smallest thing in the class (class), classes have methods, classes can create objects, and there are some properties and so on. A Java program can be thought of as a collection of a series of objects that work together by invoking each other's methods. The following is a brief introduction to the concepts of classes, objects, methods, and instance variables.

    • Object : An object is an instance of a class that has state and behavior. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.
    • class : A class is a template that describes the behavior and state of a class of objects.
    • method : The method is the behavior, a class can have many methods. Logical operations, data modifications, and all actions are done in a method.
    • instance variables : Each object has a unique instance variable, and the state of the object is determined by the value of these instance variables.
2.2 First Java Program

Here is a simple Java program, which will print the string Hello world, the text part of the simple explanation of what each sentence is doing

  

 Public class Myfirstjavaprogram {      //   * *  First Java program.      * It will print the string Hello    World*    /publicstaticvoid Main (string [] args) {   // Define the main method, which is essential for the program to run /       /  print  HelloWorld//  fixed syntax for print output    
2.3use Notepad to write code and shipLine

Here's how to save, compile, and run the program:

    • Open Notepad, add the above code;
    • Save the file name as: Myfirstjavaprogram.java; Remember to save the path, such as on the desktop.
    • Open the CMD Command window, enter the location of the target file, assuming the desktop, i.e. enter C:, then CD desktop, go to the desktop
    • In the Command Line window, type Javac Myfirstjavaprogram.java press ENTER to compile the code. If the code has no errors, the cmd command prompt goes to the next line.
    • Then type Java Myfirstjavaprogram Press ENTER to run the program and print Hello world
c:\users\administrator\desktop> Javac Myfirstjavaprogram.java c:\users\administrator\desktop> java Myfirstjavaprogram Hello World
2.4 Notes to be written for the first time

The following points should be noted when writing Java programs:

    • case-sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.
    • 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, such as 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 filename : The name of the file that you write code to save, must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).
    • Main Method Entry : All Java programs are executed by the public static void Main (String args[]) method.
2.5 Java Identifiers

All of Java's components require names, which are like classes, variables, and methods mentioned above, so that the names we give them are called identifiers.

For the Java identifiers, there are a few things to note:

    • All identifiers should start with a letter (A-Z or a-Z), Dollar ($), or underscore (_), such as A11, but 1AA is not possible.
    • Can be a combination of any character after the first character
    • Keyword cannot be used as an identifier
    • Identifiers are case-sensitive
    • Examples of legal identifiers: Age, $salary, _value, __1_value
    • Examples of illegal identifiers: 123abc,-salary
2.6 Java modifier

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

    • Accessible modifiers: default, public, protected, private
    • Inaccessible Modifiers: final, abstract, STRICTFP
2.7 Java variables There are several types of variables in Java
    • Local variables
    • class variables (static variables)
    • Member variables (non-static variables)
2.8 Java Array

An array is an object stored on a heap and can hold multiple variables of the same type.

2.9 Java Enumeration

Java 5.0 introduces enumerations, and enumeration limit variables can only be pre-set values. Use enumerations to reduce bugs in your code.

For example, we design a program for the juice shop that will limit the juice to small cups, medium cups, and large cups. This means that it does not allow customers to spot fruit juices other than these three sizes.

This involves the invocation of the Enum method, which can be referred to http://blog.csdn.net/qq_27093465/article/details/52180865

class Freshjuice {   enum  freshjuicesize{SMALL, Meduim, LARGE}   freshjuicesize size;}  Public class freshjuicetest {   publicstaticvoid  main (String args[]) {       New  freshjuice ();       = Freshjuice. Freshjuicesize.meduim;}   }

2.8 Java Keyword

The following is a list of Java reserved words. These reserved words cannot be used for constants, variables, and names of any identifiers.

Key Words Description
Abstract Abstract methods, modifiers for abstract classes
Assert Whether the assertion condition satisfies
Boolean Boolean Data types
Break Jump out of a loop or label code snippet
Byte 8-bit Signed data types
Case A condition for a switch statement
Catch Match the try with catch exception information
Char 16-bit Unicode character data type
Class Defining classes
Const Not used
Continue Do not execute the remainder of the loop body
Default Default branch in the switch statement
Do Loop statement, the loop body is executed at least once
Double 64-bit double-precision floating-point number
Else The branch that executes when the IF condition is not established
Enum Enum type
Extends Represents a class that is a subclass of another class
Final Indicates that a value cannot be changed after initialization
Indicates that a method cannot be overridden, or that a class cannot have subclasses
Finally In order to complete the execution of the code design, mainly for the robustness and integrity of the program, regardless of whether there is no exception to execute code.
Float 32-bit single-precision floating-point number
For For Loop statement
Goto Not used
If Conditional statements
Implements Indicates that a class implements an interface
Import Import class
instanceof To test whether an object is an instance of a class
Int 32-bit integer number
Interface interface, an abstract type, with only the definition of methods and constants
Long 64-bit integer number
Native Represents methods implemented in non-Java code
New Assigning a new class instance
Package A series of related classes make up a package
Private Represents a private field, or method, etc., accessible only from within the class
Protected Indicates that a field can only be accessed through a class or its subclasses
Subclasses or other classes within the same package
Public Represents a shared property or method
Return Method return value
Short 16-digit number
Static Represents a class-level definition that is shared by all instances of the
Strictfp Floating-point comparisons use strict rules
Super Represents a base class
Switch SELECT statement
Synchronized A block of code that represents a single thread that can be accessed at the same time
This Represents the invocation of the current instance
or call another constructor
Throw Throw exception
Throws Defining exceptions that a method might throw
Transient Modify fields that do not serialize
Try Represents a code block to do exception handling or a finally mate to indicate whether throwing an exception executes the code in the finally
void Tag method does not return any values
Volatile Tag fields may be accessed by multiple threads at the same time without synchronizing
While While loop
2.9 Java Annotations

Similar to C/c++,java also supports single-line and multiline annotations. The characters in the note will be ignored by the Java compiler.

Single line://

Multiple lines:/* */

 Public class myfirstjavaprogram{   /*  This is the first Java program    * It will print Hello World    * This is an example     of a multiline comment *    /publicstaticvoid  main (String []args)       {//  This is an example of a single-line comment              /*/System.out.println (" Hello World ");     

2.9 Java Empty Line

Blank lines, or annotated lines, are ignored by the Java compiler.

2.10 Inheritance

In Java, a class can be derived from another class. If you are creating a class that already has a class that has the properties or methods you need, you can inherit the class from the newly created class.

With inherited methods, you can reuse the methods and properties of existing classes without rewriting the code. The inherited class is called the superclass (Super Class), and the derived class is called a subclass (subclass).

Simple example: If you create an animal class, it can eat, can drink water, then create a cat, you will find the cat is also an animal, can realize all the methods and properties of animal class.

2.11 Interface

In Java, interfaces can be understood as protocols that communicate with each other between objects. Interfaces play an important role in inheritance. An interface defines only the methods to be used for derivation, but the concrete implementation of the method depends entirely on the derived class.

0 Basic Learning Javase (ii)--Basic grammar

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.