Java the next day

Source: Internet
Author: User


/*/------------------------

Start the next day, feel uncomfortable//continue to play code

  Auto increment and decrement * *

 Put the code on first.

1  Packagestudy. Demo;2 /**3 * ++i4  * 5  * @authorAdministrator6  *7  */8  Public classAUTOINC {9 Ten      Public Static voidMain (string[] args) { One         inti = 1; A System.out.println (i); -SYSTEM.OUT.PRINTLN (+ +)i); First add a later operation -System.out.println (i++); Add one after the first operation the System.out.println (i); -System.out.println (--i); -System.out.println (i--); - System.out.println (i); +          -     } +  A}

And then paste the results of the operation, in contrast to understand--

1 12 23 24 35 26 27 1

++a equals a = a +1. --a opposite.

The previous code comments have, but again, + + or--in front of the first is to perform the operation, and then generate the value, for + + or--in the second of course, the opposite, is the value of the husband, and then perform the operation/

  

/*/------------------------

  The relational operator doesn't have to say much,

    The equivalence of test objects

The code is as follows. mostly = = and! =, these two are easy to confuse.

1  Packagestudy. Demo;2 3  Public classEquivalence {4      Public Static voidMain (string[] args) {5Integer N1 =NewInteger (47);6Integer N2 =NewInteger (47);7SYSTEM.OUT.PRINTLN (N1 = =n2);8SYSTEM.OUT.PRINTLN (N1! =n2);9         Ten     } One}

/*/Output

 

1 false 2 true

Believe it is a doubt, = = should be true,! = is false-this is wrong, the integer object is exactly the same, although the object content is the same, but the object's reference is really different. = = and! = is a reference to an object, so it is the output above//Read more.

Then we have an idea, if it is to compare the content of the object.

Of course we can use Equals () This is a special method for all objects, but this does not apply the ' basic type ' primitive type directly using = = and! =.

A code that is affixed with equals ().

1  Packagestudy. Demo;2 3  Public classEqualsmethod {4      Public Static voidMain (string[] args) {5Integer N1 =NewInteger (47);6Integer N2 =NewInteger (47);7 System.out.println (N1.equals (N2));8     }9}

/*/Output

 

1 true

We are always living in the imagined world, waking up and finding it is not so simple.

such as the following code.

1  Packagestudy. Demo;2 3 classvalue{4     inti;5 }6 7  Public classEQUALSMETHOD2 {8      Public Static voidMain (string[] args) {9Value V1 =NewValue ();TenValue v2 =NewValue (); Onev1.i = v2.i = 100; A System.out.println (V1.equals (v2)); -     } -}

Finally woke up and found not true, but false

/*/Output

 

1 false

This is because the default behavior of equals () is a comparison reference. So unless you override the Equals () method in your new class, it's not possible to show the behavior we want.

Most Java class libraries implement the Equals () method to compare the contents of an object, rather than a reference to a comparison object.

/*/------------------------

After reading, do an exercise,

Requirement: Create a class named Dog that contains two string fields: Name and says. In the main () method, create two dog objects, one named spot (it calls "ruff! "), another named scruffy (it calls" wurf! ”)。 They are then displayed with their names and calls.

The code is affixed.

1  Packagestudy. Demo;2 3 classdog{4 String name;5 String says;6 }7 8  Public classDogdemo {9      Public Static voidMain (string[] args) {TenDog spot =NewDog (); OneDog scruffy =NewDog (); ASpot.name = "ruff!"; -Scruffy.says = "wurf!"; -System.out.println ("Name:" + Spot.name + "\nsays:" +scruffy.says); the     } -}

/*/Output

1 name:ruff! 2 says:wurf!

First come here to do Nanyang Science and technology problem//

  

Java the next day

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.