How to change JDK in Linux

Linux Mint or Ubuntu may come with the OpenJDK Java compiler and runtime. If you need to work the Sun version of the Java development kit, you will need to download it. You can do this using the Synaptic app or just open a Terminal window, press ALT-F2, then type gnome-terminal
sudo apt-get install sun-java6-jdk
Ideally, the [...]

How to accept input from keyboard

If you need to write interactive text-based application in Java, you will need a way to accept input from the keyboard; sure you can get it from the command line, but at some point, you will need to prompt the user for an input and store that input somewhere in your code for later processing. [...]

Multiplication table using 2 for loops in Java

I saw this search term coming up over and over again in my wp-stats, I would guess that this is a school assignment–for those beginning to get into grips with Java language, this is how to generate the Multiplication table using 2 for loops; you could use another kind of loop, but using ‘for’ is [...]

Starting database programming in Java

Database programming is probably one of the most common tasks that you will encounter in your programming career–in fact, during job interviews, it is an expectation that you know how to deal with the most common databases in use–like for example, MySQL, Oracle, MS SQL Server and many others.
Java database programming can get hairy [...]

How to peek quickly inside the .class files using javap

If you need just need to quickly see the name of the methods of a specific Java class or you want to take a look at a disassembled code, you can use javap. If your JDK is properly installed and you have setup your PATH and CLASSPATH properly, then you can already use javap–it’s executable [...]

Installing Tomcat 6 on Snow Leopard

Snow Leopard comes with Java 6, so Tomcat installation wasn’t much of a problem.
The procedure involved just downloading the Tomcat 6 binaries from the Tomcat download site, I downloaded the tar.gz portion of the Core packages. The next step steps were to copy the Tomcat package to the home folder (you can put it anywhere [...]

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 how to [...]

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 [...]

Declaring an array filled with Objects

class ArrayEx tries to create an array of java.lang.Object(s), but it won’t. All it will do is to create an array of 4 elements, but each of the element will contain null. Unlike declaring and initializing Java primitives where the array elements will be populated automatically with the default primitive values (int, short, boolean, float [...]

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 and prefix [...]

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 dealing [...]

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 object invokes [...]

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 get a [...]

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 definition.

An abstract [...]

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 piece [...]

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 Java [...]

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 to [...]

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 me [...]

Older Posts »