Concrete usage of objects, arrays, and string classes for Java

Source: Internet
Author: User

The behavior of the object:

1. Memory allocation ( the difference between stacks and heaps )

Stack: Holds the value of the local variable (the value used to hold the base data type; A reference to the instance that holds the class)

Yes: used to store dynamically generated data, such as new objects

2. Call method

method returns a value, a method does not return a value, a method throws an exception to the caller of the method

1  Public classdate{2          Public intday,month,year;3 4          Public intGetDay () {//return integral type5System.out.println ("Inside the Getday method");6             returnDay ;7     }8      Public voidPrintDate () {//does not return a value9System.out.println ("Inside the PrintDate method");TenSystem.out.println (month+ "/" +day+ "/" +Year ); One     } A}

3. Method signature
Includes: 1) access modifier public allows the method to be called from anywhere,private except within the class cannot be called,protected , and the default access modifier Applies to inheritance and packages, respectively.

2) optional modifiers static, final, abstract, native, synchronized, native

3) return value (the method signature must include the type of the return value)

4) Method Name (the method signature must appear after the return value, using the Hump naming method)

5) formal parameter list (after the method name must be a form parameter list enclosed in parentheses)

6) List of exceptions thrown

Note: Static members that are decorated with static are shared (allocating 1 spaces) for all objects of the class, and only static members can be called directly in a static method, and non-static members cannot be called directly; in member methods, static and non-static members are called directly.

4. Formal parameters and actual parameters

Formal parameter lists: used to declare data types that are passed to a method

Actual parameters: Data passed to the formal parameter

5. Method overloading

Meaning: A class has two or more methods with the same name but with different parameter lists

Requirements: Different parameter types, different number of parameters, different parameter list

Example: 1) public float Computepay (double d,int x);

Public float Computepay (double D);

2) public void setday (int x,int y,long z);

public boolean setday (long a,int b,int c);

6. Constructor (construction method)

Purpose: Called when an object is instantiated, initializes a member variable in the object

Property: The name is the same as the class name; no return value type

Note: 1) By default, there is a non-parametric construction method in the class, 2) when you write out the method of the system by default, the parameter construction method is no longer generated

Format:

 1  public  class       radio{ 2  public  int   channel;  3  public  Radio () {}//  default constructor  4  public  Radio (int  c) {//  with parameter constructor  5  channel=C;  6   7} 

By this (), the constructor calls the other constructors, which must be the first executable statement in the constructor;

Array:

7. Array:

Meaning: A collection of elements stored in a contiguous block of memory, each element being the same data type, distinguished by an index, the index of the first element is 0;

Declare and allocate space: double[] temps=new double[30];

Element initialization when declaring and allocating space: int score=new int[]{1,2,3 ...}; or int[] score={1,2,3 ...};

8. Array length

1) arrayindexoutofboundsexception: array subscript out of bounds

2) An array is an object that is a reference data type

3) The Length property represents the size of the array:

1      Public voidRegister () {2Scanner input =NewScanner (system.in);3         4          for(inti = 0; i < users.length; i++){5             if(Users[i] = =NULL){6Users[i] =NewUser ();7System.out.print ("User name:");8Users[i].username =Input.next ();9System.out.print ("Password:");TenUsers[i].password =Input.next (); One                  Break; A             } -}

4) foreach: For array composition or set traversal

1      Public void showallusers () {2          for (User user:users) {3             if NULL )4                System.out.println (user); 5         }6     }

9. Referencing arrays

A reference array for an employee type is declared as follows: Employee [] array name;
Object array: Single element type [] array name =new type [length] (user[] users=new user[100;])

10. A method summary for the string class (JDK_API_1_6_ZH_CN. CHM document)

Concrete usage of objects, arrays, and string classes for Java

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.