Some fragment of thinking in Java

Source: Internet
Author: User

1.String s;

System.out.println ("s=");

You'll get a compile time error because s isn ' t actually attached to anything, a safer practice, then are always Initiali Ze a reference when you creacte it:

String s = new "asdf";

2. When you create a array of objcets, you are really creating a array of references, and each of the those references is AU Tomatically initialzied to a spacial value with its own key word:null, when Java sees NULL, it recognizes the Refere nCE in question isn ' t pointing to a object, you must assigin a object to each reference before it, and if you tr Y to use a reference that's is still null, the problem would be reported at run time, Thus, typical array errors was Preven Ted in Java.

3. A test for inheritance-determine whether you can state the ' is-a ' relationship about the classes and has it make Sense

"A circle is a shape"

Another test is "is-like a"

"The heat pump is like a airconditioner"//air conditioner can only control for cooling and then the heat pump can both heat and cool;

4. Static key word.

It's a piece of storage for a particular field, it's not on the object and only initialized once, you can refer to it th Rough its class name, something you can ' t does with a non-static number.

5.Initialization & Cleanup

Java automatically calls that constructor if an object was created, before users can even get their hands on it, so Initi Alization is guaranteed.

The new expression does return a reference to the newly created object, and the constructor itself has no return value.

6. Overloaded Methods

Each methods must take a nuique list of argument types

The number of the arguments is different.

The type of each arguments is different.

The order of arguments is different. You don ' t normally want-take this approach since it produces difficult-to-maintain code;

Overloading with Primitives

If you hava a data type which is smaller than the argument in the method, which data type is promoted, Char produces a sligh tly different effects, since if it doesn ' t find a exact char match, it's promoted to int.

The mehods take narrower primitive values, if your argument are wider, then you must perform a narrowing conversion with CA St, otherwise the compiler would issue an error message.

7. The This Keyword

This can is only used inside a non-static method.

If you're calling a method of your class from within another method of the Your class, you don ' t need to use this.

Public CALSS Apricot {

void pick () {/* */}

void Pit () {/* */}

}

The This keyword are used only for those spacial cases on which you need to explicitly use the reference to the current obj Ect

Pulic class Leaf {

int i = 0;

Leaf increament () {

i++;

return this;

}

void print () {

System.out.println ("i=" + i);

}

public static void Main (string[] args) {

leaf x = new Leaf ();

X.increament (). Increament (). Inceament (). print ();

}

}//output:i = 3

To pass oneself to the foreign method it must with this.

When you call one constructor from another to avoid duplicating code, you can make such a call by using the-the This keyword,w Hile you can call one constructor using this, you cannot call. In Addtion, the constructor call must is the first thing you do, or you'll get a compiler error message.

Some fragment of thinking in 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.