Skip to main content

Posts

Showing posts from February, 2012

The Remainder or Modulus Operator in Java

Java has one important arithmetical operator you may not be familiar with,  % , also known as the modulus or remainder operator. The  %  operator returns the remainder of two numbers. For instance  10 % 3  is 1 because 10 divided by 3 leaves a remainder of 1. You can use  %  just as you might use any other more common operator like  +  or  - . class Remainder { public static void main (String args[]) { int i = 10; int j = 3; System.out.println("i is " + i); System.out.println("j is " + j); int k = i % j; System.out.println("i%j is " + k); } } Here's the output: % javac Remainder.java % java Remainder i is 10 j is 3 i%j is 1 Perhaps surprisingly the remainder operator can be used with floating point values as well. It's surprising because you don't normally think of real number division as producing remainders. However there are rare times when it's useful to ask exactly how many times does 1.5 go i

Tips to improve performance of database-driven Java applications

Here are four tips, not really super cool or something you never heard and I rather say fundamentals but in practice many programmers  just missed these, you may also called this database performance tips but I prefer to keep them as Java because I mostly used this when I access database from Java application. Java database performance tips 1: Reduce the number of calls you make to the database server. Believe it or not if you see performance in seconds than in most cases culprit is database access code. since connecting to database requires connections to be prepared, network round trip and processing on database side, its best to avoid database call if you can work with cached value. even if your application has quite dynamic data having a short time cache can save many database round trip which can boost your java application performance by almost 20-50% based on how many calls got reduced. In order to find out database calls just put logging for each db call in DAO layer, even