[Java Programming Ideas-Learning notes] Chapter 2nd everything is an object

Source: Internet
Author: User

2.1 Creating a new data type: Class

After mastering the object-oriented theory in the first chapter, we know that each object must belong to a type, so how does Java create a new data type? As shown in the following program:

class Circle {    //  attribute     //  method }

As shown above, class Circleis defined using the keyword class, and the entire class is included with curly braces {}. Of course, this class cannot do anything, it also needs to define some properties and methods.

2.2 Properties and methods

An object-oriented feature is the combination of data and methods. For example, to a circle abstract, the visible circle has its attribute radius, it also has the method, like tells us its circumference and the area. Not only the circle, but any other object has its properties and methods, in view of such characteristics, object-oriented to the properties and methods together. As shown in the following program:

class Circle {    double radius;   radius    Circle () {}//  constructor    doublereturn 3.14 * radius * Radius }//  get area    double Getperimeter () {2 * 3.14 * RADIUS;} // Get Perimeter }

where Circle () {} is a constructor for class circle, it does nothing, but it is used to initialize an object when it is created, and the name of the constructor must be the same as the class name.

It is worth mentioning that the properties of the class have default values, where the default value of radius is 0.0. In addition, properties and method parameters and return values can be not only basic data types, but also reference data types, which are objects and arrays, as shown below

classMyClass {BooleanBo; CharC; byteb;  Shorts; inti; LongLo; floatF; DoubleD;        String str; voidGetdefaultfieldvalue () {print (BO);//falseprint (c);//' + 'Print (b);//0print (s);//0print (i);//0Print (LO);//0Print (f);//0.0Print (d);//0.0print (str);//NULL} String Fun (string[] s) {//return values and parameters can be reference data types        returnS[1]; }}
2.3 Creating objects

Once you have the class, you will create the object. Creating an object is simple, just create it with new , like we're going to create a Circle object

New Circle ();

The parentheses are used here, which makes it easy to think of methods, so creating an object must call a method, the constructor. We can also pass arguments to the constructor, but only if the class has to define a constructor with parameters.

2.4 Manipulating objects with references

How do you manipulate objects after you create them and use them? Manipulating objects by reference, for example, we create a television set such as the object, it is placed in the heap, but usually we always use the remote control it, the remote is equivalent to the reference variable, it is placed on the stack, this reference variable can not only control a TV, when we create another same TV, This reference variable can also control it. As Solution

Indicates that the remote control a (the reference variable of the TV) can be controlled by TV A (a TV object). Remote control A can also change the object it controls, instead controlling the TV B or C, as shown below

As shown above, the remote controller A can control the TV A, can also change the control direction, control TV B. Use the Java program to illustrate the following

New Television (); // TV atv.on (); // turn on TV atv.off (); // Close New Television (); // reference variable TV to control TV b Tv.on (); Tv.off ();

Visible, a TV reference variable can control the TV a or B.

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

New Television (). on (); // turn on TV a New Television (). on (); // turn on the TV b

However, because the object is anonymous, it cannot be found after one time, so the two TVs in the program cannot be closed anymore.

2.5 never need to destroy objects

After the object is used, the object must be destroyed, and in Java it is automatically destroyed by the garbage collector. Will the object be destroyed as soon as it goes out of scope? No, even if the entire program exits, the object is not destroyed immediately, and the garbage collector always destroys the object at the appropriate time.

The creation and destruction of objects involves the mechanism of memory allocation and recycling of the JVM, which, if elaborated, will be beyond the scope of this article and is no longer covered by this article.

2.6 First Java Program

Can finally write the first Java program, here to show the classic Hello, world program

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        System.out.println ("Hello, World");}    }

To explain it line by row,Public Refers to the access control character, which meansHelloWorldis a public class, it can be accessed outside the package, and a Java file can have only onePublic Class that can contain any number of non-Public class, the file name must andPublic The name of the class is the same. The second line refers to theMainmethod, it must be usedPublic AndStatic Retouching, if the PublicChange to a different modifier, or removeStatic, the result is that although the compilation was passed, the run times were wrong.string[] argsIsMainThe parameter of the method refers to the receivingString Array, such as I entered the command "Java HelloWorld Hello World" to let the program run, thenStringThe first element of the array is Hello, and the second element is world. The third line prints the result,Systemis a class defined by a Java class library, outis a property of it, or a member, that is a reference data type. What the third line does is that the classSystemThe member calledprintlnMethod Prints "Hello, World".

[Java Programming Ideas-Learning notes] Chapter 2nd everything is an object

Related Article

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.