Notes on Apache Ant
A basic checklist before diving into Ant 1. Make sure that files inside /AntInstallDirectory/bin are on the PATH environment variable 2. Make sure that files inside /AntInstallDirectory/lib are part of the CLASSPATH environment variable. If you need instruction on how to set environment variables in Windows, go to this page, if you need instruction on [...]
Hello World java
To make sure that you have installed your development environment properly, break it in using the Hello World program. Launch your editor–notepad will do, gedit, TextEdit, TextMate etc. I wouldn’t advise using word processors like MS Word or OpenOffice, it might generate some other characters that will interfere with the compilation process. Here’s a simple [...]
Increment and decrement operator
Java has increment and decrement operator pretty much like C, C++ and C# and other languages. They do behave predictably the same too. You can copy the code above and save it to a file named IncDec.java, then compile and run it as $ javac IncDec.java $ java IncDec Just note the behavior of postfix [...]
Primitive Vs Reference type
For primitive types; char, byte, short, int, long, float, double and boolean, ALWAYS use “==” (double equal) operator to compare for equality, this does not go against common sense, in fact, this is the probably the default thinking of many programmers — I hope by now you’ve gotten over the “=” as a way to [...]
Java interfaces
An interface is a like a class — well not really — it’s an interface, it contains 100% abstract methods. There is an explanation on abstract methods in this post . When you define interfaces, you are not reusing any behavior, you are enforcing behavior, you are guaranteeing that whoever inherits this interface, will provide [...]
Java array basics
An array is a data structure that allows you access and manage many variables using a uniform name. Imagine it like a pigeon hole, with rows and columns, you can identify a specific location in a pigeon by addressing it using row and column coordinates; Now imagine how that can be a useful thing if [...]
Variable declarations and Pass by value
Variables are declared using this structure. [access keywords] [special keywords] Type varname;, and they are initialized using this structure varname = initial value;. It is common to see the declaration and initialization on the same line, thus giving us this structure [access keywords][special keywords] Type varname = initial value;
How to tell an object’s class type
If you need to know exactly what the object type of a class is during runtime, you can achieve that using the getClass() method of an object. The getClass() is method of java.lang.Object, which means that every java class has inherited that method. Another technique that you can use is the instanceof operator. When faced [...]
Object life cycle
Java objects go through certain states throughout it’s lifetime, it goes through a specific order of execution. Class is loaded - The default Java class loader kicks in, and loads your class definitions either from the local file system or the network, static initializers (if defined) are executed at this stage. Created – When another [...]
Overloading
Overloading is a process where you define a method more than once, on the same class. This is different from overriding because an override has to happen on a derived class; an overload will happen on the same class.
Hacking System.out.println()
To lessen your chances of getting CTS, you should type less. Have you ever wondered how many times we have typed System.out.println(); Here’s a quick hack so that we’ll type less of it.
Overriding
Overriding happens when you rewrite (re-implement is the code-speak) a method that you inherited from a base class. Rules on overriding The method signature of the overriding function (in the derived class) has to exactly the same as that of the signature being overriden (in the base class). If this rule is violated, you will [...]
Final methods and variables
The final keyword can be applied to both methods and variables, when it is applied to a method, the method can no longer be overridden, it can still be inherited but not overridden. This is a good way to exercise a more granular control on implementations, instead of locking in a whole class, you can [...]
Java constructors
Constructors are special method — Let me take that back, they are not methods, they are constructors, they just look a bit like methods.
Abstract classes
A class with at least one abstract method, is an abstract class (should be defined as an abstract class) – by this definition, a Java class becomes concrete when the following 2 conditions are met. There are no abstract methods defined inside the class The abstract modifier is not preceding the class keyword on the [...]
Final classes
Final classes are classes that cannot be inherited. It might be confusing at first, specially knowing that one of the benefits of object oriented programming is the ability to re-use functionality via inheritance, so why would we want to prevent inheritance? Consider this scenario; You are a consultant, you have been asked to write a [...]
An instance of a class is called an object
This phrase is very common in Java literature, hence we need to have a better understanding of what it means. Remember when we defined a class on Java class fundamentals, we talked about its parts, and how we’re supposed to define it, now we’ll talk about it and we’re going to include object in the [...]
Java classes, a deeper look
If you haven’t read the Java class fundamentals, read that first, then come back here, you will need an high level understanding of Java classes first. I left out some details during the introduction on Java class fundamentals, for good reason, it makes the article a bit easier to understand, so why another article on [...]
Accessibility
4 keywords that affect accessibility are public, protected, package (the default) and private. You probably have seen many times over that package is the default, or default is also the same as package in Java, what it means is just, that you if don’t do anything, don’t put any access keyword in front of either a variable, [...]
Java packages
Java packages provides a directory like or folder like structure that allows you to organize your Java classes. It also enables you to implement stricter access control at a class level, state and method level. When you create a non-trivial application, the number of classes could easily be in the hundreds, you will eventually want [...]
How to make Java objects talk to each other
One Java object can talk to another Java object by sending and responding to messages. So what are messages in terms of Java codes? The methods that you define inside your classes constitutes the messages, hence if you don’t define any method inside your classes (what good are they then?), then there is no invoke-able [...]
Inheritance
Inheritance happens in Java in 2 ways. At the class level via the extends keyword, and the other is the at the interface level, via the implements keyword.
Java class fundamentals
Java classes makes creation of Java objects possible, classes and objects are inseparable. I’m not really sure how I can make the distinction clear between classes and objects, I’m sure that by now there is a good chance you heard about a class being a “cookie cutter” or a “blue print” for objects. So let [...]
Java objects, what are they really?
I almost shied away from this subject because it’s very complex, and I’m not sure I can add value to the already voluminous definitions of Java objects on the Internet, but I have other intentions why I’d like to take a shot at this, someday these small chunks of info I’ve posted here will become [...]
Decisions again | using the switch statement
When the nature of the decision you need to make is a bit on the fuzzy side, it could be impractical to use the if-statement, for those kinds of decisions, you will use the switch-statement.
Decisions, decisions | using the if-statement
Some of things you will do when programming is choosing a set of steps (commands) to execute only when certain conditions are true, and if they are not true, you will do something else. In Java, you will either use the if-statement or the switch statement.
What kinds of data can you handle in Java
Primitive data types are defined at a language level, they’re just there, if you need one, just declare one — they’re built in. Java defines 8 of them. Here they are:
The rules for Java identifiers
Java identifiers includes variable names, method names (methods are functions in Java, there are no sub routines in Java, everything is a method), class names, interface names, package name and enum names – practically everything else that a programmer can define in the Java program. Its very straightforward to define identifiers, we just need to [...]
The while loop | while statement
Use this if you are uncertain as to how many times you need to perform a set of commands. Database programmers are quite fond of this, because the most common task in database programming is to read a set of records, you don’t know how many, process them one by one until you get to [...]
The for loop | for statement
Use this if you want to execute a command or set of commands for a fixed number of time. This is useful if you already know how many times you’d like to be inside the loop structure. The syntax for of the for-loop is as follows; for([initial value of some variable];[terminal value of the variable];[the [...]
Older Posts »