Java beginner's summary

Source: Internet
Author: User

1. Comparison of Objects

  • The "=" operator is used to compare two objectsMemory Address ValueEqual or not
  • The equals () method is used to compareContentConsistent?

For example:

 1 public class text3 { 2  3     public static void main(String[] args) { 4         String str1 = new String("java"); 5         String str2 = new String("java"); 6         String str3 = str2; 7          8         if(str1 == str2) 9         {10             System.out.println("str1 == str2");11         }12         else13         {14             System.out.println("str1 != str2");15         }16         if(str2 == str3)17         {18             System.out.println("str2 == str3");19         }20         else21         {22             System.out.println("str2 != str3");23         }    24 25     }26 27 }

The running result is:

str1 != str2str2 == str3

Why is the content of str1 identical to that of str2? These two objects point to different memory spaces, so their memory addresses are different. The memory address value is compared with "=.

To compare the content, use "equals ".

 1 public class text3 { 2  3     public static void main(String[] args) { 4         String str1 = new String("java"); 5         String str2 = new String("java"); 6         String str3 = str2; 7          8         if(str1.equals(str2))    9         {10             System.out.println("str1 == str2");11         }12         else13         {14             System.out.println("str1 != str2");15         }16         if(str2.equals(str3))17         {18             System.out.println("str2 == str3");19         }20         else21         {22             System.out.println("str2 != str3");23         }    24 25     }26 27 }

The running result is:

str1 == str2str2 == str3

2. What are the differences between the two methods for declaring String objects?

String str1 = new String ("java ");

String str2 = "java ";

For example:

 1 public class text3 { 2  3     public static void main(String[] args) { 4         String str1 = "java"; 5         String str2 = new String("java"); 6         String str3 = "java"; 7                  8         System.out.println("str1 == str2 ? -->"+(str1 == str2)); 9         System.out.println("str1 == str3 ? -->"+(str1 == str3));10         System.out.println("str3 == str2 ? -->"+(str3 == str2));11         12     }13 }

Running result:

str1 == str2 ? -->falsestr1 == str3 ? -->truestr3 == str2 ? -->false

If str1 is equal to str3, it means that str1 and str3 pointSame memory space.

Once declared, the content of the String object cannot be changed easily.

If you want to change the value of a String object, the first step is to disconnect the original String reference before opening up a new memory space.

If the new keyword is used to open up the memory space of the String object, two Memory Spaces are actually opened.

3. Generation of class objects

Format:

Class Name object name = new Class Name ();

To create an object of a certain type, you must use the followingTwo stepsTo achieve:

  1. Declare the variable pointing to "object created by class"
  2. Use new to create a new object and assign it to the variable created earlier

Example: Create an object of the Person class

Person p; // declare a Person Class Object pp = new Person (); // use the new keyword to instantiate the Person object p

You can also declare the variable as follows:

Person p = new Person (); // declare the Person object p and instantiate this object directly

The object can only be used after instantiation, And the keyword of the instantiated object is new.

4. Access a variable or method in the object

Access property: Object Name. Attribute name access method: Object Name. Method Name ()

For example, you want to access the name and age attributes in the Person class.

P. name; // access the name attribute p. age in the Person class; // access the age attribute in the Person class

5. Class property encapsulation (private)

To set or retrieve attribute values, you can only useSetXxx (),GetXxx ()This is a clear and standard provision.

Attributes or methods in the encapsulation class:

Encapsulation property: private property type property name encapsulation method: private method return type method name (parameter)

Add someSetXxx (),GetXxx ()This method is mainly used to set and obtain private attributes in the class to solve encapsulation problems, such as the following:

 1 class Person 2         { 3             private String name; 4             private int age; 5             void talk() 6             { 7                 System.out.println("wo shi:  "+name+", jinnian: "+age+"sui"); 8             } 9             10             public void setName(String str)11             {12                 name = str;13             }14             15             public void setAge(int a)16             {17                 if(a > 0)18                     age = a;19             }20             21             public String getName()22             {23                 return name;24             }25             26             public int getAge()27             {28                 return age;29             }30         }31         32 public class text17 {33 34     public static void main(String[] args) {35         36         Person p = new Person();37         p.setName("zhang san");38         p.setAge(-25);39         p.talk();40 41     }42     43 }

Running result:

wo shi:  zhang san, jinnian: 0sui

6. Internal class

Define the format of the internal class:

Name of the class external class {// name of the internal class member of the external class }}

External classes cannot find the attributes declared in internal classes, while internal classes can access the attributes of external classes.

The internal class declared with static becomes an external class, but the internal class declared with static cannot access non-static external class attributes.(Java basics tutorial P193)

7. class inheritance

Class inheritance format:

Class Parent class // define parent class {} class subclass extends parent class // use the extends keyword to implement class inheritance {}

In java, only single inheritance is allowed, and multiple inheritance is not allowed. That is to say, a subclass can only have one parent class, but java allowsMulti-layer inheritance.

Multi-layer inheritanceFor example:

class A{}class  B  extends  A{}class  C  extends  B{}

When the subclass object is instantiated, it first calls the non-argument constructor in the parent class by default, and then calls the corresponding constructor in the class.

 

SuperThe main function is to complete the sub-class to call the content of the parent class, that is, to call the attributes or methods of the parent class..

Using super to call the constructor in the parent class can only be placed in the first line of the program.

Super call attributes or methods:

Super. attributes in the parent class; super. Method () in the parent class ();

8. Comparison of this and super

This:

  • 1. indicates the current object
  • 2. Call methods or properties in this class
  • 3. When calling the constructor in this class, place it in the first line of the program

Super:

  • 1. Subclass calls the method or attribute of the parent class
  • 2. Place the constructor in the parent class in the first line of the program.

From the above table, we found that the construction method called with super or this needs to be placed in the first line. So,Super and this cannot call the constructor operation at the same time.

9.Abstract class definition rules

  • Abstract classes and abstract methods must be modified with abstract keywords.
  • Abstract classes cannot be instantiated, that is, they cannot use the new keyword to generate objects.
  • Abstract methods can be declared without implementation.
  • Classes that contain abstract methods must be declared as abstract classes. subclasses of abstract classes must be rewritten to all abstract methods before being instantiated. Otherwise, this subclass is still an abstract class.

10.Final keywords

When declaring classes, attributes, and methods in Java, you can use the keyword final to modify them.

  • Classes marked by final cannot be inherited.
  • The final flag method cannot be subscribe.
  • Final variables (member variables or local variables) are constants and can only be assigned once.

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.