Java Fundamentals (iv)

Source: Internet
Author: User
Tags array length modifiers volatile

First, Method 1, the definition of the method

A method is also called a function, which is a piece of code that can perform a function independently. Method can be regarded as a whole.

Grammar:

Modifier  Method name (data type  variable name, data type variable name,...... [Formal parameters (0 to N)]) {    return   Returns the same value as the method return type;}

Attention:

1. The return type and method name of the method must be defined.

2. The same name and number of parameter types cannot exist in a class at the same time. (parameter order can be different)

3, the first word of the name of the method should start with a lowercase letter , the following word is written in uppercase letters, do not use a connector. e.g. Addticket ();

Pros: Make the program shorter and clearer . Facilitates maintenance . Improve the efficiency of development. Improved reusability of code.

2. Modifier

Modifiers are used to define a class, method, or variable, usually at the front end of a statement.

1. Access modifier (4 kinds)

Private (privatization, minimum scope): Visible within the same class. Working with objects: variables, methods. Note: Classes (external classes) cannot be decorated.

default (that is, default, nothing is written, default): Visible within the same package, no modifiers are used. Use objects: classes, interfaces, variables, methods.

protected (Inheritance dependent): Visible to classes and all subclasses within the same package. Working with objects: variables, methods. Note: Classes (external classes) cannot be decorated.

Public (common, most scoped): Visible to all classes. Use objects: classes, interfaces, variables, methods.

2. Non-access modifiers

The static modifier, which is used to modify class methods and class variables.

    • Static variable

Static variable, no matter how many objects a class instantiates, its static variable has only one copy . Often and final are combined to declare constants .

    • static method

The static declaration is independent of the object's Statics method. static methods cannot use non-static variables of a class .

final modifiers, which are used to modify classes, methods, and variables, the final decorated class cannot be inherited, the decorated method cannot be redefined by the inheriting class, the modified variable is constant , and is not modifiable.

    • Final variable

Note: Variables can be explicitly initialized and initialized only once. A reference to an object that is declared final cannot point to a different object. But the data in the final object can be changed. This means that the final object reference cannot be changed, but the value inside can be changed.

    • Final method

The final method in the class can inherit from the quilt class, but cannot be modified by the quilt class. The primary purpose of declaring the final method is to prevent the content of the method from being modified .

    • Final class

The final class cannot be inherited, and no class can inherit any of the attributes of the final class.

An abstract modifier that is used to create abstract classes and abstract methods.

    • Abstract Abstraction class

An 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 Abstraction method

An abstract method is a method that does not have any implementation, and the specific implementation of the method is provided by the subclass. Abstract methods cannot be declared final and static. 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.

synchronized and volatile modifiers, which are used primarily for threading programming.

    • The method of synchronized keyword declaration can only be accessed by one thread at a time. The synchronized modifier can be applied to four access modifiers.
    • A volatile -Modified member variable forces the value of the member variable to be re-read from shared memory each time it is accessed by the thread. When a member variable changes, the thread is forced to write the value of the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.
2. Invocation of the method

Java supports two ways of invoking a method, depending on whether the method returns a value to choose from.

1. When a method returns a value, the method call is usually treated as a value.

1  Public classTest1 {2      Public Static voidMain (string[] args) {3         intsum = Add (6, 6);4 System.out.println (sum);5     }6 7      Public Static intAddintAintb) {8         intsum = a +b;9         returnsum;Ten     } One}

2. If the method return value is void, the method invocation must be a statement.

1 System.out.println ("Move on");
3. Overloading of methods (overload)

Method Overloading : Refers to a class that defines multiple methods with the same name , but requires that each method have a different parameter list ( type , number , order ) parameter type , number of parameters, parameter order, as long as there is a difference.

  If there is more than one name in a class, compile without error , then it must be overloaded .

1  Public classOverload {2     /**3 * Method overloaded with different number of parameters4      * @parama5      * @paramb6      */7      Public Static voidAddintAintb) {8 9     }Ten  One      Public Static voidAddintAintBintc) { A  -     } -  the      Public Static voidAddintAintBintCintd) { -  -     } -}
Second, array1, the definition of the array

The Java array is used to store the same type elements of a fixed size. equivalent to a variable container that holds multiple data

2. Creating an array1. Creating Arrays dynamically

assign values immediately after the declaration is created    , you can also declare the re-assignment first

syntax format : data Type [] array variable name = new data type [ array length ];

1  Public classTest {2     /**3 * Dynamic creation of arrays4     */5 @Test6     Public voidtestlist () {7         int[] List =New int[2]; // Create an array  8List[0] = 1; // store data  9LIST[1] = 2;Ten  One System.out.println (list.length); A     } -}
2. Creating arrays statically

  in the when an array is created put The known elements are put in directly at once.

Syntax format :

    • Data Type [] array variable name = new data type [] { multiple comma-delimited data };
    • Data Type [] array variable name = { multiple comma-delimited data };
1  Public classTest1 {2     /**3 * Create arrays statically4     */5 @Test6      Public voidtest1 () {7         int[] List1 =New int[]{1, 2};8         int[] List2 = {3, 4, 5};9 System.out.println (list1.length);Ten System.out.println (list2.length); One     } A}
3. Multidimensional arrays

Multidimensional arrays can be seen as arrays of arrays, such as two-dimensional arrays, which are a special one-dimensional array, each of which is a one-dimensional array.

1  Public classArraytest {2 3      Public Static voidMain (string[] args) {4         int[] A = {1, 2, 3};5         int[] B = {4, 5, 6};6         int[] C = {7, 8, 9};7 8         int[] arr = {A, b, c};//creating a two-bit array9 Ten         //traversing a two-dimensional array One          for(inti = 0; i < arr.length; i++) { A             int[] m =Arr[i]; -              for(intj = 0; J < M.length; J + +) { -System.out.print (M[j] + ""); the             } - System.out.println (); -      /*Output Results: 1 2 3 - 4 5 6 + 7 8 9*/ -         } +     } A}

Java Fundamentals (iv)

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.