Feb 06 2013

The substring story

Category: Wild thoughtsseaqxx @ 12:45

In high school I started learning programming and practising it in Turbo Pascal. When it comes to strings, there is a method that is implemented in every language, and that is the method that extracts a substring form a given string, based on some parameters.

In Turbo Pascal the method is called Copy and here is the description:

COPY
Syntax: Copy(St, Start, Number)
The Copy function returns a substring copied out of St starting at position Start containing Number characters. Thus St is a string expression and Start and Number are positive integers. If Start > Length(St), the empty string is returned. If Start + Number > Length(St), only characters in the string St are returned. If Start is outside the range 1..255, a run-time error occurs.

So if you have the example string, Copy(“example”,2,4) will result in xamp. (In Turbo Pascal, indexing starts with 1)

The next programming language I was introduced to was C. In C there is a string.h files which contains all functions available for string processing, but not a substring method. So msot programmers use strncpy(click for api) like this

const char* input= “example”;
char *result = (char*) malloc(6);
strncpy(result, from+2, 4);

So the string value referenced by result will be in this case ampl.(In C, indexing starts with 0).

Next there was Php, which has a method called substr(click for api).

< ?php $result = substr(“example”,2,4); // returns “ampl

And in Java we have a method in the String class called substring.

public String substring(int beginIndex,int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex.

Thus doing the following in Java:

String str=”example”;
String result = str.substring(2,4);

Will create a new result string with the value of “am“.

In Javascript there is also a substring method which works like the one in Java.

Anyway, I just wrote this post to express my frustration, because yesterday I went full retard on a Java Recruting Test and and when asked what “example”.substring(2,4) produces, I wrote very sure of myself “ampl“. (WTF BRAIN!?) I am still puzzled by the answer I gave, and I am trying to figure out why my brain computed that answer using programming languages I haven’t used in a while, instead of Java, which I use every day. Of course I realised my mistake when I got into bed, and couldn’t sleep for two hours because my brains was screaming “Stupid, stupid, stupid! How could I be so stupid???”

This is one of the worst brain-farts I had in a while. Oh well, somewhere in this town there is somebody correcting that test and probably thinking I am the worst Java programmer ever. At least I still have a job. :D

Tags: ,


Oct 13 2012

Learning Spring, part III

Category: IT&Cseaqxx @ 18:15

I have a new hot question for you:

We have two classes AccountServiceImpl and ClientServiceImpl. Any of these classes inherits from each other, meaning one is a subclass of the other. What will happen if we define a pointcut like this:

"execution( * *..AccountServiceImpl.update(..)) 
       && execution( * *..ClientServiceImpl.update(..)) "

And here are your options:

  1. The pointcut matches any public update methods of the two classes whatever the arguments.
  2. The pointcut matches any update methods of the 2 classes whatever the arguments and the method visibility.
  3. The pointcut matches any update methods of the two classes, with one ore more arguments and whatever the method visibility.
  4. No join point is defined.
  5. Something else will happen.

Continue reading “Learning Spring, part III”

Tags: , ,


Sep 15 2012

Learning Spring

Category: English posts,IT&Cseaqxx @ 23:47

A few months ago the company where I am currently employed sent me to Belgrad to train me in Spring Core. The training lasted 4 days and at the end I was supposed to take the Spring Core certification exam and pass it.

I delayed that for a while, but because now I have some spare time I decided it was time for me to do this thing. So I went over the slides and the spring code samples again. But after doing some mock tests I concluded I will most likely fail the certification, because the official materials were not enough so I started reading Spring in Action. Still I noticed that in the tests there was still stuff that I hadn’t covered. So I started reading Spring Reference. And because I have some problems in retaining information just by reading it, I stared to test the recommended examples. And this is where I hit some walls.

But before telling you what is not clear for me I shall tell you what technologies I’m using for development: Maven 3.0.0, Jdk 1.7, Intellij Idea 11.1.3 and Spring 3.1.2 (I know the certification is for 3.0, but as the Spring reference manual has 840 pages, I might as well read about the new and useful stuff added in 3.1)

The first problem I had was with the compound property names. I tried using them. Idea does not recognize them and my test fails.

Continue reading “Learning Spring”

Tags: , ,


Jun 19 2012

when you have an itch…

Category: English posts,IT&Cseaqxx @ 10:34

… you gotta scratch it. And I had a serious itch cause by my Windows. Even if Windows has evolved so much, even if Windows 7 is the peak of greatness among all the Windowses before it, it still does not know how to manage 4 core processors. So I was seriously pissed about the fact that Windows 7 froze from time to time, making my super-laptop look like my first computer, an AMD K6, 500 MHz and 256 MB SRAM. Which was definitely not cool, considering the fact that my laptop’s configuration looks like this: Intel Quad Core i5-450M and 6 GB DDR3. The only reason I kept windows so far on my laptop was that my processor has a capability known as turbo boost, meaning that can over-clock itself when needed. But this won’t be needed that much if the tasks were efficiently distributed between the 4 cores, which were not.

So two days ago, I just snapped, decided to give up the possibility of playing games for a while and went on a search for the perfect Linux for my laptop. I am fond of Gentoo as you know, but compiling a full OS was not an option because it is a time-consuming operation and also because all that compiling would set my laptop on fire and it’s already hot in Romania right now (32 Celsius degrees). So I was left to choose between Archlinux and Mint. I did not even consider Ubuntu, it’s a Linux that looks and acts like Windows, the thing I was trying to get rid of. I had Archlinux before and I know in order to get the final result some time must be wasted on its configuration, so I went for Mint.

In less than 30 minutes I had a fully functional and compact Linux, oh well … fully for a normal user, not for me, a curios developer. So after this I went on and started installing the development software. The first one I wanted to install was the jdk. Mint uses open-jdk which Idea and STS refuse to go along with, so I went on a quest for installing the Oracle version. If on other Linux systems this was a piece of cake on Mint, it was not so, because Mint has all these symlinks pointing to open-jdk binaries, and even if you do everything right, set the JAVA_Home variable and add it to the path, when you will execute “java -version” in the console, the binary that will be executed will still be the one of the in the open-jdk. The only way to change this is to go to /usr/bin, see where the specific symlinks point to and change that. After that I installed Idea and STS and everything was flawless.

Then I wanted to add a second monitor and this is where all blew up in my face. But not because there was something wrong with Mint, but because one of the cables was not plugged in correctly in my monitor and the system did not see it. I did not even consider that the problem might come from a cable and went on and tried to install nVidia drivers in order to convince the system to see my external monitor. After the first restart I was left without an interface, because the nVidia drivers were not stable, ofcourse. So I went old-school and installed lynx, a text based browser, and searched for a solution for my problem. I did so and tried different options for an hour, when finally it worked and I had my graphic interface back, but the external monitor still was invisible to Mint, so I considered the possibility of the monitor not actually being plugged in the laptop. I check the cables and … surprise. It detected it right away.

Conclusion: if you want to install a Linux on your laptop, I truly recommend Mint, it is small, smart, fast and it knows how to work the special buttons on your laptop, without any additional settings. And is also easy to install, if you are not an old-school developer who considers problems being caused by the software first :D , that is. :)

Tags: , , , ,


May 05 2012

What does a passionate developer do…

Category: English posts,IT&Cseaqxx @ 22:35

… when is stuck in a hotel room because of the rain, bored out of his mind and with no mood to work? Well when that happens to me I start to reed my java feed reader and depending on what I find there I might write my own articles. So… yes, this article is today’s consequence of boredom.

1. What is the flaw with the Stack class?
Actually, there are two of them all being caused by the fact that java.util.Stack extends java.util.Vector:
I. Extending Vector methods insertElementAt and removeElementAt can be called and they actually work, so the stack definition is not respected (that part with only the last inserted element being accessible, the LIFO principle)
II. Extending Vector, Stack is also synchronized which makes it slow and when synchronization is not necessary this  is quite inefficient. This is not exactly a flaw, it’s more of a personal observation observation.

Then again in the api it is written that “It extends class Vector with five operations that allow a vector to be treated as a stack “, so I guess these are not flaws, the Stack class just works as intended. (Recommendation: use ArrayDeque)

2. Can an interface extend multiple interfaces?
There is no right answer to this question, because it depends of the point of view of the interviewer.
I. Yes, because you can define an interface like this:
public interface MultipleIntf extends List, Serializable {
}
II. No, extending means actually inheriting all functionality of the super-entity and  perhaps adding new functionality, in the case of interfaces there is nothing to inherit and no functionality to add. Except for the obligation to implement all abstract methods that will be enforced on the implementing class.

3. What is lazy loading?
Lazy loading is a name to describe the process of not loading something (object/class) until needing it. This question will surely take you to a ClassLoader discussion, so it is better to know and understand the Java Class Loading mechanism.
So:
- the java source files are compiled into executable code for the JVM, called bytecode, stored into *.class files.
- at start-up JVM has no loaded classes. When the first class is loaded, the classes on which its execution depends are searched and loaded too. So if I have a class which has imports statement for ArrayList and Serializable, the JVM will load my class, then it will search and load ArrayList.class and Serializable.class. Let’s imagine we have a big application with a lot of class files and one of them is missing. The application will run just fine, until we try to access a functionality implemented by that class, when the JVM will let us know that the class was not found by throwing a java.lang.ClassNotFoundException.
And this my darlings is lazy loading. A class is not loaded until used and there would be no point in doing that for efficiency reasons. Right? Anyway, if you want to have a deep understanding of the Java Class Loader, I recommend this article.

Tags: , , , ,


Jan 26 2012

Java stuff that you should know:What are the differences between C++ and Java?

Category: English posts,IT&C,Wild thoughtsseaqxx @ 16:48

Java vs. C++ Because somebody brought up this subject here I decided that this is the question I should answer next. I am a developer from the old generation,at least this is how we call it here in Romania. Turbo Pascal was my first, (red cheeks :D) then I learned C, C++, Php, Java and .Net. With Java and C++ I have had the longest and most stable relationships. So I think I am able to answer this one, though I have to mention that nowadays you could be asked to compare Java to other languages too.

Again this can considered to be a difficult question mostly because there are a lot of differences between these two languages. That’s why you want to start your answer with the differences that remove any doubt that you know both languages pretty well, and usually this means getting technical. This is the how I would answer this question:

  • Java is multi-threaded, C++ is not
  • In Java primitive and reference data types are always passed by value, C++ uses also references and pointers.
  • Java has no pointers, C++ does
  • Java has implicit memory management through garbage collector, in C++ memory management is explicit
  • Java does not allow operator overloading,  C++ does
  • Java does not need destructors, C++ does
  • Java does not have type struct and union, C++ does
  • In Java, types cannot be overridden, in C++ we can do that.
  • Java does not provide multiple inheritance using classes (or virtual inheritance), C++ does
  • In Java, arrays have fixed sizes and attempt to read from an index bigger that size -1, will cause an exception. In C++ attempt to read from an index bigger that size-1 might even succeed and the value returned is the current value stored int that memory block.
  • Java programming model is totally compatible with OOP programming. C++ also supports functional programming, procedural and template programming.
  • Java is a WORA/WORE – write once run anywhere/everywhere (any compatible JRE ) , C++ is a WOCA – write once compile anywhere
  • Java has built-in support for comment documentation
  • Java has no native support for unsigned arithmetic, C++ does. (Has anybody used that in C++, ever?)
  • In Java there is no need for scope resolution operator (::)

Of course there’s more, but this should be enough at an interview. Fell free to add anything you might consider important. As always I am eager to learn new stuff.

Tags: ,


Jan 19 2012

Java stuff that you should know: difference between abstract classes and interfaces

Category: English posts,IT&C,Wild thoughtsseaqxx @ 17:14

Many times after a Java Interview I have asked myself if my answers were correct, if the person interviewing me realized that I understand a certain concept and if I was able to express my knowledge in order to achieve that. So I will start writing on my blog how I would answer some common Java Interview questions.

So, in Java what are the differences between an abstract class and an interface?
This is a question with one big answer, not only because there are that many differences between abstract classes and interfaces, but because there are so many ways to say it. So, here is what I would say about interfaces:

  1. Interfaces are equivalent to protocols. They basically represent an agreed-upon behavior (a contract) to facilitate interaction between unrelated objects.
  2. Interfaces are a capsule for a group of methods with empty bodies or prototypes (abstract methods ) and implicitly final static members (constants).
  3. Interfaces have all members public impilicitly.
  4. Interfaces are declared like this:
    public interface TestIntf {}
  5. Interfaces can only extend other interfaces and keyword implements does not apply to them.
  6. Interfaces can extend more interfaces.
  7. If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
  8. Interfaces are considered to be slower because they require extra indirection to find the corresponding method in the class implementing them.
  9. Interfaces support only public and default access modifiers.
  10. Interfaces can be declared abstract, so the Java compiler would not complain if your declaration would look like below, but the abstract modifier is redundant as interfaces are implicitly abstract.
    public abstract interface TestIntf {}

Here is what I would say about abstract classes:

  1. Abstract Classes are the middle ground between interfaces and classes. They can have both abstract and non-abstract methods and constant and non-constant fields.
  2. Abstract Classes can have constructor, but it can never be called directly. It can only be called using super in the extending class or is automatically called by the jvm.
  3. Abstract Classes support all access modifiers.
  4. Abstract classes can be declared like this:
    [access_modifier] abstract class TestAC {  [ implementation]    }
  5. Abstract Classes can implement any number of interfaces, can extend only one class (which can be abstract or not – the condition is not to be final) and implicitly extend class Object.
  6. Abstract Classes can have only non-abstract methods (but what would be the point?). Abstract Classes which have only abstract methods and constant fields are called fully abstract.

And what would I say about both of them:

  1. Java supports multiple inheritance only through interfaces.(Some say it is limited. I say it is practical.) Why? Because extending multiple classes may lead to problems when they have methods with identical signatures. (Remember C?)
  2. The use of interfaces or abstract classes is determined by the project requirements and design.
  3. Using abstract classes affects the flexibility of your code, because class extension rips away class individuality.

If you have anything else to add, or something here needs correction, your effort will be kindly appreciated.

Tags: , ,