Category Archives: Java

How to Find the Real URL Behind a Redirect in Java

Say you have a URL that redirects to another (e.g., bit.ly). How do you find the real URL behind the redirect? You can do this in Java using the HttpURLConnection class. Like this: String url = "http://bit.ly/23414"; HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setInstanceFollowRedirects(false); con.connect(); String realURL = con.getHeaderField(3).toString(); You basically create a connection, disable […]

Basic Regex Patterns in Java/Perl

Below you’ll find the basic Regex patterns you can use to match, edit and replace strings. I am using the Java/Perl Regex flavor, so the patterns might be slightly different if you are using another programming language or platform. Java Implementation Here’s a simple Java sample that uses the Regex library. It will try to […]

Dining Philosophers Concurrency Problem in Java

Dining Philosophers is a problem about concurrent programming and synchronization, first proposed in 1965 by Dijkstra. Basically you have X philosophers sitting around a table, and there are X forks available (one between each philosopher). The philosophers do two things: think and eat (alternating between both). However, in order to eat the philosopher must grab […]

Chronological Order of Popular Programming Languages

Below you’ll find a chronological order of some popular and/or important programming languages. 1840 – Analytical Engine Code The Analytical Engine was a theoretical (i.e., never built) mechanical general-purpose computer, created by British mathematician Charles Babbage. Ada Lovelace came across the idea, and created some code for the Analytical Engine. That’s why she’s considered the […]

CodeChef.com Easy Problem: Transform the Expression

He’s an interesting albeit relatively easy problem from CodeChef.com: ———- Reverse Polish Notation (RPN) is a mathematical notation where every operator follows all of its operands. For instance, to add three and four, one would write “3 4 +” rather than “3 + 4”. If there are multiple operations, the operator is given immediately after […]