Sunday, 6 July 2014

Source code issues

Well apparently the gist loader I've been using for displaying gists in here is broken. I didn't have time to investigate, but I think I'm going to switch to Alex Gorbatchev's Syntax Highlighter.

I didn't manage to get it working yet. I'll keep trying it on this page.

public class A {
    public A() {
        System.out.println("Hello, syntax highlighter!");
    }
}

Saturday, 5 July 2014

Java constructors

I get this error from time to time and you may get it too:

"There is no default constructor available in com.example.A"

Here's a sample code / pattern (error is shown by my IDE on line 7):

public class A {
    public A(int a) {

    }
}

class B extends A {
}

The reason for that error is very clear if you read the Java language specification, the section(s) on constructors.

In Java, every class has a constructor. If you don't declare you own, a default (no-argument) constructor will be created implicitly for you. If you do declare your own constructor, then no no-argument constructor will be created implicitly - you'll have to create it manually if you want one. The default constructor has the access modifier of the class (so, for example, if your class is "public", then the implicitly created default constructor will also have the "public" access modifier etc.).

So both classes in the above code are guaranteed to have a constructor: A has an explicit one and B has an implicit no-argument constructor. Here's the essential thing that you are required to know to understand why the error takes place: in Java, all constructors start with a call to a superclass constructor. The exception is the Object class, which has an empty constructor. If you explicitly call a superclass constructor (using super([parameters])), then this requirement is fulfilled. If you don't, then the default constructor of the superclass is called implicitly.

However, if your superclass doesn't have a default constructor, like in A's case above, this will result in a compile-time error. You will therefore need to call a superclass constructor explicitly in B, which in turn requires you to create a constructor explicitly in B.

Yet another situation when knowing the language specification comes in handy. :)

Learning Java

What's the best resource for learning Java? I assume the one asking this question has some experience with the language. I probably asked this question several times since I first wrote my first line of Java a while ago.

Sure, "learning Java" is a bit broad. You can "learn", for example, how to add a tool tip to a JButton. StackOverflow is obviously the top choice for these very specific things. Then the Oracle tutorials are also good at introducing numerous topics and the API. But I am talking more about the language itself than its APIs. 

There has been one resource in particular that greatly improved my understanding and knowledge of the language in a way that nothing else came even close: THE. JAVA. LANGUAGE. SPECIFICATION. (along with the JVM specification)

There are some things about the language that you may not learn at university or from a random tutorial / article online, but all will be covered in the language specification. It's a document I constantly read to refresh my memory and understand things better and I think all Java developers should have a copy handy. 

Tuesday, 21 January 2014

Practicing SQL

I was looking for ways to practice SQL online and I found three options:

1. SQLZoo. It's nice because it offers indications and tutorials on several topics of interest.

2. SQLFiddle. Doesn't have tutorials, but it's ready for you to just type your queries. 

The above should be OK for a quick test or practice. However, if you'd like to work on real data, then I think nothing comes even close to

3. STACKEXCHANGE DATA. Yes, you have real data available from all Stack Exchange websites. That includes over 6.5m Stackoverflow questions to query, not including users tables and what not. So excited I'm gonna hit the Publish button now and head to it. 

Saturday, 4 January 2014

Making your life easier with the terminal

There are several tricks that could help anyone become faster and more productive when working with the terminal. I wrote about several ways

1. Create aliases

A have a folder called "Programming" that I want to access very often from the terminal. Now typing "cd /home/alex/Programming" isn't a big deal, but I managed to shorten that to just "cdp", which is far more comfortable and a lot faster. But aliasing isn't just for shortening commands. You can also set a default behaviour to other commands. For example, if you want ls to list hidden files by default, you could create an alias for "ls -a" to be just "ls".

Creating aliases is easy: edit your .bashrc file and add your aliases line by line like this

alias ls='ls -a'

2. Use variables

There was a time when I wanted to move several files from different folders to the same folder (which had quite a long path). Then I had to repeat this a few minutes later, this time using other files. Instead of doing mv <my_files> <long_path_to_folder> over and over again, I saved the long path to a (short :D ) variable and used that instead, thus saving time. There are definitely better uses for variables, but I appreciated their existence. To create a new variable, type

varname=value

and then, to access its value use $varname.

3. Symbolic links

If you ever downloaded, for example, Eclipse or IntelliJ IDEA, you had to run the programs by typing something like <path_to_executable>/exec, which can get annoying after a while, especially, as in the previous situation, when you have a long path. Now I start IDEA by just typing idea in the terminal. Here's how it all works.

When you type a command in the terminal, that command is searched for in dedicated folders and, if found, is executed. We need to let those dedicated folders know that when we type idea in the terminal, then <path_to_idea>/idea.sh has to be executed. Welcome to symbolic links!

A symbolic link will hold a reference to your file. Go to /user/bin and type ln -s <path_to_idea> idea. You need to be superuser to run this. Then log out and log back in and now you can start IDEA (or whatever you want) using the command you've just set. :)