To paraphrase the above, I'm going to try to teach/learn how to drive a car, not be a mechanic.
Equality
I mentioned briefly before that there was an actual difference between using the ".equals()" method and the "==".
The == operator tests for equality in terms of the representation contained in the 2 variables you're testing. This works fine for primitive types since the variables contain the abstract values, but doing this for objects sucks, since the variables in that case hold references to the objects, not objects themselves. So even if 2 objects were exactly the same, == would come up false.
In the case of .equals(), it tests for the identity of the object, and Java just kind of assumes every object has this method. If not, a default is provided, which implements identity equality.
Null
There is a special reference value known as null (null reference). This reference points to no object, nothingness.
A reference variable should point to null when it has no value, and can be explicitly assigned to do so:
Using null is fun for conditionals and stuff, but the drawback is the amount of extra code and testing you need to do. Still, fun times.
This
Every instance method has this local variable automatically declared. "this" is a reference to the specific object the method has been called for. There are 3 main uses for this:
1. It allows a reference to the current object to be passed as a parameter to another method.
object.someMethod ( this );
2. It can be used when calling other methods, or accessing instance variables on he current object.
public void someMethod2 ( ) {
this.someMethod3 ( );
this.someMethod4 ( this.someVariable );
}
3. The third is to use one constructor to call another but really, this gets confusing fast, and I don't see a lot of use for it, at least for small-time programs, so let's gloss over it.
That's it for tonight.Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!
And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us.
I'm going to start learning programming soon too, I'll follow along with you, maybe at the end of the month you could do a quick summary on some basic things, or a basic program tutorial? Would be really cool to see.
ReplyDeleteOh shit, that's a good idea, actually. Thanks, I'll do summaries every now and then.
ReplyDeleteThis looks well too complicated for me! Great post though!
ReplyDeleteSome good info on this post. Everything coming along great man
ReplyDeleteGreat information, very useful.
ReplyDeleteThanks for sharing.
I will try to slowly learn about this while following your blog. It might work or not, but i will give it a go.
ReplyDelete