Java Course: Basics of 1.Java Grammar

Source: Internet
Author: User

1. Note When writing a program:

Case sensitive, remember to distinguish between case;

Capitalize the first letter of the class name, with multiple words each word should be capitalized (you can modify the class name by the Dark F2 key);

The first letter of the method should be lowercase, and multiple words, except the first letter lowercase, the other initials are capitalized;

The source file name is the same as the class name definition, and storing a separate class file is remembered by default, with the class name being saved as the filename, with the suffix. Java files;

Main method Entry: All Java programs are executed by the public static void main (String []args) method, which cannot be executed without adding the program. (static method calls can only be called by calling the method name or the class name directly with the call. Method name, non-statically defined method needs to be new out of an object i.e. class name Object name = new class name)

2. Identifiers: class names, variable names, and method names 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), a dollar ($), or an underscore (_) , and remember that you cannot use numbers as the initial letter.
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

3.Java You can use modifiers (to set access permissions on classes or methods and variables of a class) to modify methods and properties in a class:

* Accessible modifiers: default, public, protected, private

 

same package

Subclasses of different packages

non-subclasses of different packages

private

√

 

 

 

default

√

√

 

 

protected

√

√

√

 

Public

√

√

√

√

* Inaccessible modifiers: Static, final, abstract, synchronized, and volatile: the next two to understand.

public   member variable can be accessed by any method in the project, it is recommended to use as few as possible   
protected   classes that are not in the same package cannot be accessed, but subclasses can access   
private    can only be used in the same class   
static  * static variable keyword is used to declare static variables independent of the object , you must declare outside the method construction method and the statement block, and the local variable cannot be declared as a static variable  . Also known as a class variable, whose value is shared by all objects of the class, regardless of how many objects are instantiated, and will not vary depending on the object of the class. The location of the memory has been allocated, no matter how the change will not affect it. (Variables defined in subroutines (methods, construction methods, or block of statements, when they are executed, variables are destroyed; Accessible modifiers cannot be used for local variables; local variables have no default values, so local variables are declared and must be initialized before they can be used) called local variables. Variables defined at the beginning of a program (those under the class name) are called global variables. )

* Static method: The static keyword is used to declare an object-independent method. A static method cannot use a non-static variable (also called a member variable, a static variable called a class variable) of a class. The static method obtains the data from the parameter list and then computes the data.
Final * Member variables, the final variable can be explicitly initialized and can only be initialized once, the final object's reference cannot be changed, but the value inside can be changed.  Its value remains the same. Modifiers are typically used with the static modifier to create class constants. Static final String str= "1"; the constant that the final str is changed from the variable.

*final method: The final modified method, called the Ultimate method, is the last method in the class that can inherit from the quilt class, but cannot be modified by the quilt class. The final method cannot be redefined by the quilt class, i.e. it cannot be overwritten, and the primary purpose of declaring the final method is to prevent the content of the method from being modified.
The *final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class.

Abstract class cannot be used to instantiate an object, and the sole purpose of declaring an abstract class is to augment the class in the future.

A class cannot be both abstract and final decorated. If a class contains an abstract method, the class must be declared as an abstract class, or a compilation error will occur.

Abstract classes can contain both abstract and non-abstract methods.

* Abstract method is a method that does not have any implementation, the specific implementation of the method is provided by the subclass. Abstract methods cannot be declared final and static.
Transient when an object is persisted (such as writing to a database), the value of the member variable does not need to be saved

Any child class that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class.
If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods.
The declaration of an abstract method ends with a semicolon, for example: Public abstract sample ();
Volatile synchronizes multi-threaded access to the value of a member variable so that different threads always get the same value for that member variable.

4. Two common definitions of arrays: *type[] Variable name = new type[The number of elements in an array]; declaring an array type

*type[] Variable name = new type[]{comma-separated initialization value};

Problem:

1. Can a subclass have more than one parent class?

No, there is a single inheritance in Java

2.What is the substantive difference between a method call in Java and a public class method called by an inheritance relationship?

Inherit is their own, "property more", call or others. (Wish to add)

Java Course: Basics of 1.Java 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.