[Java programming ideology-learning notes] Chapter 2nd everything is an object and java programming ideology

Source: Internet
Author: User

[Java programming ideology-learning notes] Chapter 2nd everything is an object and java programming ideology
2.1 create a new data type: Class

After understanding the object-oriented theory in chapter 1, we know that each object must belong to one type. How can we create a new data type in Java? The procedure is as follows:

Class Circle {// attributes // method}

As shown above, the class is defined using the keyword classCircle, Enclose the entire class with curly braces. Of course, this class cannot do anything, and it also needs to define some attributes and methods.

2.2 attributes and Methods

An Object-Oriented feature is to combine data and methods. For example, to abstract a circle, we can see that the circle has its attribute radius. It also has a method, such as telling us its length and area. Not only is it a circle, but any other object has its attributes and methods. In view of such characteristics, object-oriented objects combine attributes and methods. The procedure is as follows:

Class Circle {double radius; // radius Circle () {}// constructor double getArea () {return 3.14 * radius;} // obtain the Area double getPerimeter () {2*3.14 * radius;} // obtain the perimeter}

WhereCircle (){}It is the constructor of the Circle class. Although it does not do anything, it is used to initialize the object when the object is created. The constructor name must be the same as the class name.

It is worth mentioning that all attributes of a class have default values.RadiusIs 0.0. In addition, the parameters and return values of attributes and methods can be not only basic data types, but also reference data types, that is, objects and arrays, as shown below:

Class MyClass {boolean bo; char c; byte B; short s; int I; long lo; float f; double d; String str; void getdefafielfieldvalue () {print (bo ); // false print (c); // '\ 0' print (B); // 0 print (s); // 0 print (I ); // 0 print (lo); // 0 print (f); // 0.0 print (d); // 0.0 print (str ); // null} String fun (String [] s) {// the return value and parameters can be reference data type return s [1];}
2.3 create an object

With the class, you need to create an object. Creating an object is simple.NewYou can just create it. For example, we want to createCircleObject

new Circle();

Brackets are used here, which makes it easy for us to think of methods. Therefore, when creating an object, we must call a method, that is, the constructor. You can also input parameters to the constructor, provided that the class must define a constructor with parameters.

2.4 manipulate objects with reference

How can I manipulate an object after it is created? For example, we have created an object such as a TV set which is placed in a heap. However, we usually use a remote control to control it. The remote control is equivalent to referencing variables, it is placed in the stack. This reference variable can not only control a TV set, but also be controlled when we create another identical TV set. Such as Solution

It means that remote control A (reference variable of the TV) can control TV a (a TV Object ). Remote control A can also change the object it controls and switch to control the TV set B or C, as shown below:

As shown above, remote control A can control both TV a and TV B. The Java program is described as follows:

Television TV = new Television (); // TV Atv. on (); // turn on the TV set Atv. off (); // turn off TV = new Television (); // reference variable TV to control TV Btv. on (); TV. off ();

It can be seen that a TV can control A or B by referencing A variable.

In addition to using the remote control to indirectly manipulate objects, you can also directly press the buttons on the TV, that is, directly manipulate anonymous objects, as shown below:

New Television (). on (); // turn on the TV Anew Television (). on (); // turn on the TV B

However, because the object is anonymous, it cannot be found after it is used once, so the two TVs of the above program can no longer be closed.

2.5 never need to destroy objects

After using the object, you have to destroy the object. In Java, the object is automatically destroyed through the garbage collector. Will the object be destroyed immediately when it exceeds its scope? No, even if the entire program exits, the object will not be destroyed immediately, and the garbage collector will always destroy the object at the appropriate time.

The creation and destruction of objects involve JVM memory allocation and recovery mechanisms. If you want to elaborate in detail, it will be beyond the scope of this article and will not be discussed in this article, so I will skip this article.

2.6 first Java program

Finally, you can write the first Java program. Here we will show the classic Hello, world program.

public class HelloWorld {    public static void main(String[] args) {        System.out.println("Hello, world");    }}

Explain it one by one,PublicThe access controller.HelloWorldIs a public class that can be accessed outside the package. In addition, a Java file can only have onePublicClass, which can contain any number of non-PublicClass, the file name must bePublicThe class name is the same. The second row refersMainMethod, which must be usedPublicAndStaticIfPublicChange to another modifier, or removeStaticThe result is that, although compiled, an error is returned during running.String [] argsYesMainMethod parameter, which indicates receivingStringArray. For example, if I enter the "java HelloWorld hello world" command to run the programStringThe first element of the array is hello, and the second element is world. The third line prints the result,SystemIs a class defined by the Java class library,OutIt is an attribute or a member of the referenced data type. The third line is: ClassSystemMember callsPrintlnThe method prints "Hello, world ".

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.