Posts Tagged: Java

Javaist learning Python – Part 1

I always enjoyed stuff that had to do with programming languages and learning how to set your mind to new things/paradigms. One of my favourite courses in the University was Logic and Functional Programming – learning Prolog and LISP (Let’s Insert Some Parenthesis :D).

I played a little bit with Python for my Mater’s Thesis (Static vs Dynamic Typed Programming Languages) some years ago and I always wanted to go a little bit more in depth with it. So, now I considered it to be a great time to restart playing with Python.

The syntax I consider it to be pretty straightforward and there is quite a good documentation on the internet. But, somehow I felt I was missing the Java API docs format. For me it is more convenient to search for something and see what else I can do with that object. Also, the font size seems to be kind of unfriendly – small and a little bit cramped(I know one can resolve this with zoom on the page). I will probably get used to the documentation, but in the meantime I will crave for Java documentation.

Even though, I knew it is not a pure object oriented programming language, is it still funny to white something “outside an object”, but this did not bother me at all, on the contrary, I found it really easy to get started writing code.

Another thing I really like the command line interpretor, that can be run from the command line. Having this is really helpful for the people that have their first contacts with Python. One can actually test something before adding it to a program.

That’s all for now, but I will keep posting further impressions :).

Collections quick review

There are times in our developer lives when we just settle with a range of implementations without thinking of the reasons behind it, or without having any valid argument in order to use one implementation over another.

Some time ago I was having a discussion with a friend [which is a big data developer] about the importance of choosing the right collection. He gave an example on how using the wrong implementation a job took over 3 hours, after changing the implementation of the collection used the same job needed 5 minutes to complete.

Below can be seen a table with basic information about different implementations of collections

Implementation Ordered Sorted Syncronized Nulls Notes
HashMap One null key andno null values
Hashtable* No null keys and no null values
TreeMap Accepts nulls as keys and value
LinkedHashMap Accepts nulls as keys and value Faster iterations than HashMap but slower inserts and remove
HashSet Accepts null
TreeSet Accepts null
LinkedHashSet Accepts null
ArrayList Accepts null Fast iterations
Vector Accepts null
LinkedList Accepts null Fast iterations and deletion
PriorityQueue Accepts null

* There is not a typo: Hashtable is the special kid that does not have camel case

PS: This is just a quick review for more about how each works, I suggest visiting the API.

So what’s that difference again? Abstract class vs Interface

Everything was nice and pretty clear before beautiful Java 8 was born.  One could define:

(1) Abstract class as being a class declared abstract (usually, but not necessary, contains at least one abstract method).

(2) Abstract method as being one method that is declared, but for which no implementation was provided.

(3) Interface as a 100% abstract class.

Even though, not all the differences are explained in these statements, these would have been acceptable definitions.

And then Java 8 was brought into the world, and the new feature (considered by some the best new enhancement) : default and static methods for interfaces. Non-abstract methods defined directly in the interface. This implies that (3) became obsolete and the magic question arise:

What is the difference between an interface and abstract class? 

Abstract class Interface
Default methods any method that is not abstract can be considered defaultit is not marked with default modifier marked withdefault modifier
Static methods static modifier required see below examples static modifier required
Non-Constants variables can be defined all the variables are implicitly public static final
A Java class can extend just one class (abstract or concrete) implement multiple interfaces
A Java interface cannot extend/implement an abstract class can extend multiple interfaces
The first concrete class extending the abstract class must implement all abstract methods implementing the interface must implement all abstract methods
Implicit modifiers for variables No implicit modifiers public static final
Implicit modifier for methods No implicit modifiers the non-static & non-default methods are implicitly public abstract
When to use … ? when the code is shared amongst related classes when the code is shared among unrelated classes

So, let’s revisit the definition of an interface. I would like to go for this one:

Interfaces are abstract data types that provide a contract between its users and its providers.

Here you have a nice example to start discovering the differences between abstract classes and interfaces!

import static java.lang.System.out;
class Main {
 public static void main (String[] args) throws Exception { //Mother mother = new Mother(); DOES NOT COMPILE
 //AnInterface interface = new AnInterface(); DOES NOT COMPILE
 Mother child1 = new Child();
 AnInterface child2 = new Child();
 Child child3 = new Child();
 //calling static fields


 //caling static methods
 //calling default methods

 //child2.staticInterfaceMethod(); DOES NOT COMPILE
 //child3.staticInterfaceMethod(); DOES NOT COMPILE
 //Child.staticInterfaceMethod(); DOES NOT COMPILE
 //calling default methods



abstract class Mother {

 public static final String ABSTRACT_CLASS_NAME = "Mum name";

 public static void staticMotherMethod() { //the access modifier is not implied
 out.println("static Mother Method");

 public void defaultMotherMethod() { //the access modifier is not implied
 out.println("Mother default");

 public void defaultMotherMethod1() {
 out.println("Mother default 1"); //the access modifier id not implied

 public abstract void abstractMotherMethod() ;

interface AnInterface {

 String INTERFACE_NAME = "Interface name"; //implicit public static final

 default void defaultInterfaceMethod() { //implicit public
 out.println("default interface method");

 default void defaultInterfaceMethod1() {
 out.println("default interface 1");

 static void staticInterfaceMethod() { //implicit public
 out.println("static inteface method");

 void abstractInterfaceMethod(); //implicit public abstract

class Child extends Mother implements AnInterface {
 public void abstractInterfaceMethod() {
 out.println("Child abstractInterfaceMethod");

 public void abstractMotherMethod() {
 out.println("Child abstractMumMethod");

 public void defaultMotherMethod1() {
 out.println("Child default method");

 public void defaultInterfaceMethod() {
 out.println("Child interface method");


Hope you had fun! 🙂 Till the next post hope you enjoy Java!

Everybody likes promotions

…of course, I talk about the Java numeric promotions. Probably everyone knows what she/he should do to get promoted, but what about the Java numeric promotions?

A. What does promotion mean?

The numeric promotion represents the automatically made conversion from a smaller numeric type to a wider numeric type when a binary aritmetic operation is made. [So it seems that this does not require hard work for some :D]

Just a small recap about the types and their length can be seen in the below table:

Type length in bytes length in bits
byte 1  1 * 8 = ?
short 2 please guess
int 4 hard one
long 8  ∞ * ∞ (reversed)
float 4 32
double 8 be my guest
char 2 16

B. What is required to get a promotion?

Just operands of the same type can be used for a binary operation. In order to achieve that there are some clear rules about how promotions occur:

1. When applying the operator for operands of different types: the smaller numeric type is promoted to the larger number type

2. When applying the operator for an integer value and a floationg point : the integer value is promoted to the floating point value.

3. Byte, short, char are automatically promoted to int when using binary operator.

4. The result of the operation has the type of the operands. [Mind the fact at the time the operator is applied the operands have the same type]

C. Tiny games with promotions

1. Let’s apply the above mentioned rules for the follwing example:

byte byteA = 1;
byte byteB = 2;
//byte byteSum = byteA + byteB;
//short shortSum = byteA + byteB;
int intSum = byteA + byteB;

Applying rule number 3 it is clear that the commented out lines of code result in a not very elegant compilation error:

 error: possible loss of precision
 byte byteSum = byteA + byteB;
 required: byte
 found: int

2.  Apply the rules and say the type .

int intA = 1;
float floatB = 2.0f;
byte byteC = 3;
____ sum = intA % byteC + floatB;

Yep, first rule number 1 is applied for byteC and the result of intA % byteC would be an integer, then this result is promoted to float and the returned value is float.

3. Let’s try another game. Take the following code:

byte byteA = 1;
byte byteB = 2;
byteB += byteA;

What happens? If you said “Compilation failure”, please, think again. As I said the promotion is applied only for binary arithmetic operations and += is actually considered a compound assignment operation, but that is another story 🙂

People tend do enjoy promotions ;). I bet you get a promotion every day, so enjoy today!

And I fell in love with the date…Java 8 Date

Love is in the air year after year in the period when winter and spring fight for power.

In the week that Java8 turns one I am going to speak about the date I fell in love with…Java 8 Date.

Even though I have never considered the previous implementation of Dates as being a pain (just a little bit verbose :p) I am really appreciative and using it made me fall instanlty in love with the new Date.

What do I really like about Date? You may find listed below some(not all) reasons:

  • The possibility to use classes specify/use exactly what you need:

Why should I be forced to store the specific date (usually 1st January 1970) when I just need the hours?  was a question that arose probably in many situations. [I would really want to know how many GB of data occupies the 1st January 1970]

Java 8 resolved the issue with adding specific classes LocalDateTime, LocalDate and LocalTime classes for the “un-timezoned” feature and for each of these provides the zoned class, which stores also the timezone for each of them.

Also, the precision one needs to use can be specified

  • The easiness to get a reference to a new instance of a Date.

Seems like a dream not to use a Calendar class to get a new instance of a Date class:

LocalDateTime thisMoment =;

LocalDate java8LauchDate = LocalDate.of(18, Month.MARCH, 2014)

LocalDate forTheNostalgics = LocalDate.ofEpochDay(1);

  • The beauty of the Period and Duration

Until this moment we specified precise moment in time, but sometimes we need to use an interval rather than specific points in time.

Period oneYearTwoMonthsThreeDays = Period.of(1,2,3);

and Period’s little brother (used for intervals within a day):

Duration snoozeDuration = Duration.ofMinutes(10);

  • One can do maths![How can one not love that]

The addition/substraction of specific periods is just a bliss:

LocalDateTime now =;

  •  The usage of the “normal” number for the month.

Just imagine in a previous Java version my birthday would have been declared like. Ok, I am  a developer and used with 0-index, but I could not just set my brain to use 5 0 for my birthday[Now that you know when my birthday is, you can send me birthday cards or presents and I do not mind receiving them before of after my bithday…so it’s still not too late 🙂 ]

Happy birthday, Java8!

And for you: hope you all have dates as good as the Java 8 ones!