JAVA (3), java

Source: Internet
Author: User

JAVA (3), java

Interface considerations:

1. The interface cannot be instantiated.

2. All methods in the interface cannot have a subject (no {})

3. One class can implement multiple interfaces

4. The interface may contain variables <but the variables cannot be modified using private or protected>

Additional:

The variables in the interface are essentially static and final.

In java Development, frequently used variables are defined in interfaces and used as global variables.

<Access mode: interface name. variable name>

5. One interface cannot inherit other classes, but it can inherit other interfaces

 

 

 

Abstract classes can have implemented methods.

None of the methods in the interface can be implemented.

 

Special requirements for defining variables in Interfaces

 

Interface Syntax:

Class name implement interface {

Method;

Variable;

}

 

Summary:

An interface is a more abstract class.

The methods in the abstract class can have a method body.

All methods in the interface do not have a method body.

The interface embodies the design concept of polymorphism and high cohesion and low coupling in programming.

 

package com.test4;public class Test{       public static void main(String[] args){     } }interface Fish{    public void swimming(); }  interface Bird{   public void fly(); }class Monkey{   int name;   public void jump()   {      System.out.println("ABC");    } }class LittleMonkey extends Monkey implement Fish,Bird{   public void swimming(){    }   public void fly(){   } }

Java inheritance is a single inheritance (a class can only have one parent class at most)

Final-Concept

Final can modify variables or vertices.

 

Using final can avoid modifying the quilt class:

Class Aaa {// use final to modify the method, indicating that the method cannot be modified or overwritten by final public void sendMes () {System. out. println ("Send message ");}}

Final usage:

1. When you do not want a method of the parent class to overwrite the quilt class (override), use the final keyword to modify

2. If you do not want the value of a variable of the class to be modified, you can use final to modify <final public void xxx ()>

If a variable is final, the initial value must be assigned; otherwise, the variable cannot be compiled.

3. If you do not want the class to be inherited, use final modification <final class xxx>

 

 

Array

One-dimensional array

Necessity of array:

Package com. test1; public class Demo5_1 {public static void main (String [] args) {// define six variables // define a float arr [] = new float [6] array that can store six float types; // use the for loop assignment // assign arr [0] = 3 to each element of the array; arr [1] = 5; arr [2] = 3.4f; arr [3] = 2; arr [4] = 50; // calculates the total weight [traversing array] float all = 0; for (int I = 0; I <6; I ++) {all + = arr [I];} System. out. println ("total weight:" + all );}}

Use of object arrays:

Package com. test1; import java. io. *; public class Demo5_2 {public static void main (String [] args) {// defines an array of objects that can store four dogs Dog [] = new dog [4]; // assign an initial value to each dog // enter the information of each dog on the console [alt + j] shortcut key InputStreamReader isr = new InputStreamReader (System. in); BufferedReader br = new BufferedReader (isr); for (int I = 0; I <4; I ++) {dog [I] = new Dog (); System. out. println ("Enter the dog name"); // read the dog name String name = br from the console. readLine (); // assign the name to the object dogs [I]. setName (name); System. out. println (""); String s_weight = br. readLine (); float weight = Float. parseFloat (s_weight); // assign the name to the object dog [I]. setWeight (weight) ;}// calculate the average weight // calculate the total weight for (int I = 0; I <4; I ++) {allWeight ++ = dogs [I]. getWeight ();} // calculate the average weight float avgWeight/dogs. length; System. out. println ("Total weight =" + allWeight + "average =" + avgWeight) }}// defines a Dog class Dog {private String name; private float weight; public String getName () {return name;} public void setName (String name) {this. name = name;} public float getWeight () {return weight;} public void setWeight (float weight) {this. age = weight ;}}

Sorting type:

1. Internal sorting:

(Exchange sort, choice sort, and insert sort)

2. External sorting method:

(Merge and direct Merge Sorting)

Exchange sorting: Bubble sorting and quick sorting

 

Bubble Sorting:

Package com. test1; public class Demo5_3 {public static void main (String [] args) {int arr [] = {, 0,-}; // sort // the outer loop, it decides how many times to take for (int I = 0; I <arr. length-1; I ++) {// The inner loop, which is compared one by one. If the former is greater than the latter, the for (int j = 0; j <arr. length-1-i; j ++) {if (arr [j]> arr [j + 1]) {// transposition temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}}// output the final result for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I] + "");} the sorting method is encapsulated into the class: class Bubble {// sorting method public void sort (int arr []) {int temp = 0; for (int I = 0; I <arr. length-1; I ++) {for (arr [j]> arr [j + 1]) {temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}}}

Call: Bubble bubble = new Bubble ();

Bubble. sort (arr );

Select sorting method:

Class Select {// Select the sort public void sort (int arr []) {// The minimum for (int j = 0; j <arr. length-1; j ++) {int min = arr [j]; // minimum number of records subscript int minIndxe = j; for (int k = j + 1; k <arr. length; k ++) {if (min> arr [k]) {// modify min = arr [k]; minIndxe = k ;}}}

Plug-in sorting

Think of n elements to be sorted as an ordered table and an unordered table. At the beginning, the ordered table contains only one element, and the unordered table contains n-1 elements.

Class InsertSort {// Insert the sorting method public void sort (int arr []) {for (int I = 1; I <arr. length; I ++) {int insertVal = arr [I]; // The insertVal preparation compares int index = I-1 with the previous number; while (index> = 0 & insertVal <arr [index]) // move arr [index] Backward. arr [index + 1] = arr [index]; // move the index forward to index --;} // insert insertVal to the appropriate position. arr [index + 1] = insertVal ;}}

Multi-dimensional array

Definition:

Type array name [] [] = new type [size] [size]

Package com. test1; public class Demo5_5 {public static void main (String [] args) int a [] [] = new int [4] [6]; a [1] [2] = 1; a [2] [1] = 2; a [2] [3] = 3; // output the image for (int I = 0; I <4; I ++) {for (int j = 0; j <6; j ++) {System. out. print (a [j] + "");} // line feed System. out. println ();}}

Binary (source code, reverse code, and complement code ):

1. The highest bit of binary is the sign bit. 0 indicates a positive number, and 1 indicates a negative number.

2. The original codes, backcodes, and supplementary codes of positive numbers are the same.

3. Negative decimal number = the symbol bit remains unchanged, and other digits are reversed.

4. complement the negative number = his/her reverse code + 1

5. The anti-code and supplemental code of 0 are both 0.

6. java has no unsigned number

7. During computer computation, all computation is performed in the form of complement code.

 

Shift to the right: Status overflow, the symbol bit remains unchanged, and the symbol bit is used to fill the overflow high

Arithmetic shift left: The symbol bit remains unchanged, and the low value is 0.

 

 

 

Set:

 

Java Collection classes mainly include the following types:

1. List structure collection class

ArrayList, struct list, Vector, and Stack

2. Map Structure collection class

HashMap class, Hashtable class

3. Set structure collection class

HashSet class, TreeSet class

4. Set of Queue Structures

Queue Interface

 

Java set usage:

Package com. test1; import java. util. *; public class Demo7 {public static void main (String [] args) {// defines the ArrayList object ArrayList a1 = new ArrayList (); // displays the size of the System. out. println ("al size:" al. size ());}}

 

 

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.