Java BASICS (4): java Basics

Source: Internet
Author: User

Java BASICS (4): java Basics
I. method 1. method definition

A method is also called a function. It is a piece of code that can independently complete a function. Methods can be considered as a whole.

Syntax:

Modifier return type method name (data type variable name, data type variable name ,...... [Form parameters (0 to n)]) {syntax for returning the result value in the code block area of the method: return returns the same type as the return value of the method ;}

Note:

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

2. A method with the same name and number of parameter types cannot exist simultaneously in a class. (The order of parameters can be different)

3. the first word of the method name should beStart with a lowercase letterAnd the following words start with an uppercase letter without a connector. For example, addTicket ();

Advantage: make the program moreBrief and clear. BenefitsMaintenance. Improved DevelopmentEfficiency. Improved codeReusability.

2. Modifier

Modifiers are used to define classes, methods, or variables. They are usually placed at the front of the statement.

1. Access modifiers (4 types)

Private(Privatization with the smallest scope): visible within the same category. Use objects: variables and methods.Note: The class (external class) cannot be modified ).

Default(Default, no write, default): visible in the same package without any modifiers. Use objects: classes, interfaces, variables, and methods.

Protected(Inheritance related): visible to the classes and all subclasses in the same package. Use objects: variables and methods.Note: The class (external class) cannot be modified).

Public(Public, maximum range): visible to all classes. Use objects: classes, interfaces, variables, and methods.

2. Non-access modifier

StaticModifier used to modify class methods and class variables.

  • Static variable

Static variable, no matter how many objects a class instantiates, itsOnly one copy of static variables. Often combined with finalDeclare Constants.

  • Static Method

Static declarations are independent of static methods of objects.Static methods cannot use non-static variables of classes..

FinalModifier, used to modify classes, methods, and variables. final modified classes cannot be inherited, and modified methods cannot be redefined by inherited classes,The modified variable is a constant.And cannot be modified.

  • Final variable

Note: variables can be explicitly initialized and can only be initialized once. Objects declared as final cannot be referenced to different objects. However, the data in the final object can be changed.That is to say, the reference of the final object cannot be changed, but the value in it can be changed.

  • Final Method

The final method in the class can be inherited by the quilt class, but cannot be modified by the quilt class. The main purpose of declaring the final method isPrevent the content of this method from being modified..

  • Final class

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

AbstractModifier used to create abstract classes and abstract methods.

  • AbstractAbstract class

Abstract classes cannot be used to instantiate objects. The sole purpose of declaring an abstract class is to expand the class in the future. A class cannot be modified simultaneously by abstract and final. If a class contains an abstract method, the class must be declared as an abstract class; otherwise, a compilation error occurs.

  • AbstractAbstract Method

An abstract method is a method without any implementation. The specific implementation of this method is provided by the subclass. Abstract methods cannot be declared as final or static. Any subclass that inherits the abstract class must implement all abstract methods of the parent class, unless this subclass is also an abstract class.

SynchronizedAndVolatileModifier, mainly used for Thread Programming.

  • SynchronizedMethods declared by keywords can only be accessed by one thread at a time. The synchronized modifier can be applied to four access modifiers.
  • VolatileEach time a modified member variable is accessed by a thread, the value of the member variable is forcibly re-read from the shared memory. When a member variable changes, the thread is forced to write the variable value back to the shared memory. In this way, two different threads always see the same value of a member variable at any time.
2. Call Methods

Java supports two methods to call a method, which can be selected based on whether the method returns a value.

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

 1 public class Test1 { 2     public static void main(String[] args) { 3         int sum = add(6, 6); 4         System.out.println(sum); 5     } 6  7     public static int add(int a, int b){ 8         int sum = a + b; 9         return sum;10     }11 }

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

1 System.out.println("move on");
3. Method overload)

Method overload: defines multiple methods in a class.Same nameBut each method must have a different list of parameters (Type, number, and order) Parameter type, number of parameters, Parameter order, as long as there is a difference.

If a class has multiple methods with the same name and no error is reported during compilation, it must be overloaded.

 

1 public class Overload {2/** 3 * method Overload, the number of parameters is different 4 * @ param a 5 * @ param B 6 */7 public static void add (int, int B) {8 9} 10 11 public static void add (int a, int B, int c) {12 13} 14 15 public static void add (int a, int B, int c, int d) {16 17} 18}
2. array 1. array Definition

Java arrays are used to store elements of the same type with a fixed size. It is equivalent to a variable container that stores multiple data

2. Create an array 1. dynamically create an array

Create an assignment immediately after the declaration. You can also declare the assignment first.

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

1 public class Test {2/** 3 * dynamically create an array 4 */5 @ Test 6 public void testList () {7 int [] list = new int [2]; // create an array 8 list [0] = 1; // store data 9 list [1] = 2; 10 11 System. out. println (list. length); 12} 13}
2. Static array Creation

When creating an array, store the known elements directly at one time.

Syntax format:

  • Data Type [] array variable name = new data type [] {multiple data separated by commas };
  • Data Type [] array variable name = {multiple data separated by commas };
1 public class Test1 {2/** 3 * Create an array statically 4 */5 @ Test 6 public void test1 () {7 int [] list1 = new int [] {1, 2}; 8 int [] list2 = {3, 4, 5}; 9 System. out. println (list1.length); 10 System. out. println (list2.length); 11} 12}
3. Multi-dimensional array

Multi-dimensional arrays can be considered as arrays. For example, a two-dimensional array is a special one-dimensional array, and each element of it is a one-dimensional array.

1 public class ArrayTest {2 3 public static void main (String [] args) {4 int [] a = {1, 2, 3 }; 5 int [] B = {4, 5, 6}; 6 int [] c = {7, 8, 9}; 7 8 int [] [] arr = {, b, c}; // create two arrays 9 10 // traverse two-dimensional arrays 11 for (int I = 0; I <arr. length; I ++) {12 int [] m = arr [I]; 13 for (int j = 0; j <m. length; j ++) {14 System. out. print (m [j] + ""); 15} 16 System. out. println (); 17/* output result: 1 2 318 4 5 619 7 8 9 */20} 21} 22}

 

 

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.