I try to write blogs whenever I get spare time

Just to let you know what I am trying to learn and what I am doing now.

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Puzzle with long division

This post is all about a simple division of data type “Long”. Just have a look at the following code:

public class LongDivision

{

public static void main(String[] args)

{

final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;

final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);

}

}

What would be printed in the console after executing this code?

I am helping you a bit. According toSun, Long has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). So both the two final variable’s value would fit perfectly, there should not be any overflow.

So, we should expect this code to print 1000. But, alas! to the great horror you would see this code to print 5!!!

What the heck with this dumb code?

Wait, here is the twist. The problem is that the computation of the constant MICROS_PER_DAY does overflow. Although the result of the computation fits in a long with room to spare, it doesn’t fit in an int.

Aren’t you thinking this is an absurd stuff? We declared the variable as long, why this poor long data needs to be fit in int?

The fact is that, when you use the int multiplication operation(*), the computation is performed entirely in int arithmetic, and only after the computation completes is the result promoted to a long. By then, it’s too late: The computation has already overflowed, returning a value that is too low by a factor of 200. The promotion from int to long is a widening primitive conversion [JLS 5.1.2].

So why is the computation performed in int arithmetic? Because all the factors that are multiplied together are int values. When you multiply two int values, you get another int value. Java does not have target typing, a language feature wherein the type of the variable in which a result is to be stored influences the type of the computation.

It’s easy to fix the program by using a long literal in place of an int as the first factor in each product. This forces all subsequent computations in the expression to be done with long arithmetic.

So the correct code would be like this:

public class LongDivision

{

public static void main(String[] args)

{

final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;

final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000;

System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);

}

}

NB: Courtesy by “Joshua Bloch”. This puzzle has been taken from his book: “Java™ Puzzlers: Traps, Pitfalls, and Corner Cases“.

Java support in Google-App-Engine

Google has recently added the long awaiting support for java in their Google-App-Engine. Previously, there was support for only python apps. Now you can easily deploy your java web-apps in the App Engine.

These are the main features added for Java (as described by Google):

  • A complete Java 6 runtime environment ( in a secure sandbox environment)
  • Based on common Java web technology standards, including servlets and WARs, JDO and JPA, java.net, JavaMail and JCache
  • Datastore Java API for storing and retrieving data. It provides robust scalable data storage for your web application.
  • The Memcache Java API. It is a distributed in-memory data cache in front of robust persistent storage for some tasks.
  • The Images Java API. For manipulating images.
  • The Google Accounts Java API. App Engine applications can authenticate users with Google Accounts.
  • Supports other languages that compile to the JVM or use JVM-based interpreters, such as JRuby, JavaScript (Rhino), and Scala
  • A plugin for the Eclipse IDE makes project creation, testing and deployment a snap. This plug-in is a bonus for GWT developers. It has reduced lots of manual works of configuration during deployment and compilation.

Getting Started With the GWT

The world is moving too fast now a day. People wants immediate response from the web pages rather than the total reloading of the page. That's why, web applications using AJAX, are becoming more and more popular day by day. Now suppose, you want to develop this kind of web applications, but you are not familiar with javascript or the AJAX technology and you know Object Oriented Programming, you have the basics of developing desktop applications, then GWT is the perfect tool you are looking for.
GWT (Google Web Toolkit) is a marvelous tool to develop web app with java programming. You write your application in java like a desktop app, and GWT will convert it to a web-app by compiling the java code to javascript code. You can have a look here for the official review of GWT.
For developing using GWT, you have to:

  • Install the Java SDK. You can get it from here.
  • Have Eclipse IDE (version 3.0 or later).
  • Download GWT from here.
  • Unzip the Google Web Toolkit package.
If the above four steps are finished, you are ready to give a run with GWT.

Now, Look at the folder you have just unzipped. You'll see there are two command line utility named "projectCreator" and "applicationCreator". These two files will help you to create a new GWT project. Follow this steps now:
  • First of all, we will create a project with eclipse support by running this script on the command-line:
    projectCreator.cmd -eclipse TestGWT -out samples\TestGWT
This command will create a Eclipse project named "TestGWT" under the "samples" folder.
  • Then, run this script:
applicationCreator.cmd -out samplesTestGWT -eclipse TestGWT -overwrite org.hillol.testgwt.client.TestGWT
This will generate all the files needed two start a GWT project.
  • To open your project in Eclipse, launch Eclipse and click the File -> Import menu. Choose "Existing Projects into Workspace" in the first screen of the wizard, and enter the directory in which you generated the .project file in the next screen of the wizard. When you are complete, you should see your GWT project loaded into your Eclipse workspace:

  • Now, run the project. You will see a page with a logo and a button. Click on the button, you will see a dialogue box popped up. This is the default simplest GWT project. Later I will show you how to create more complex applications and how to communicate with the server.