In this article, we will take a look on another interview question about
adding two numbers, but without using + or ++ operator. Interview starts with a
simple statement, Can you write a function to add two numbers (integers)
without using + or plus arithmetic operator in Java? If you are good in maths,
it wouldn’t take more than a second to say that, we can use subtraction or -
operator to add two numbers because a-(-b)== a+b. Well
that’s correct, but real question starts when interviewer quickly points out
that, you can not use any arithmetic operator including +,-,*,/++
or --. Programming and Coding questions are integral part of any Java
interview. You should always expect couple of questions like this, e.g. swapping
two numbers without using temp variable. If you have been giving interviews, then
you know that, it will eventually comes downs to bitwise operator in Java. Yes,
we can add two numbers by using bitwise and bitshift operators, which is not
arithmetic. Interviewer, will be happy by hearing bitwise operator, but he
would like to see the code. Well, if you know binary arithmetic or how to add
numbers in binary format, you may be familiar with fact than sum of two numbers
can be obtained by using XOR operation and carry, by using AND operation. This
is the fact, you must remember to solve this question or add two integers
without using any arithmetic operator e.g. plus, minus etc. Sometime
interviewer, may ask you to write both iterative and recursive solution of same
question, Since recursion is another confusing programming technique, it's favored more during interviews. In this Java tutorial, we will see both
recursive and iterative version of our add method, which calculate sum of two
numbers without using arithmetic operator, but using bitshift
and bitwise operators in Java.
Tuesday, June 18, 2013
Thursday, June 13, 2013
Spring HelloWorld Example in Java using Dependency Injection
In this Spring framework tutorial, we will learn how to write hello world
example. This should be the first tutorial to start learning Spring framework,
as it gets ball rolling and while coding and running this example, you learn a
lot about Spring framework, Spring XSD files, required JAR file and more
importantly how spring works.This Helloworld program in Spring framework is an
example of classical Java hello world program,
written using dependency Injection design pattern
by using Spring Framework's IOC container. Spring is so far one of the most
popular Java application framework, which promotes some best practices while
writing Java application e.g. dependency Injection. Spring provides an IOC
container to manage life-cycle of Spring beans and provides support to get beans
any time from IOC container. Apart from Spring's IOC container, it also provide
rich API to simply many of Java task e.g. JdbcTemplate help you
to write JDBC code without taking care of boiler plate things like closing
connection, statement, result-set etc. Similarly Spring framework also provides JmsTemplate to simply
JMS related task e.g. sending and receiving messages in Java. I first learned
about usefulness of Spring framework, while reading Expert Spring MVC and Web Flow,
it first few chapters, which explains about how dependency injection provides
improved testing, loose coupling and help in clean code. Those two chapters are
so far my best read on Spring, and I also suggest to take a look. By the way in
this Spring tutorial, we will see one of
the most simple example of Dependency Injection e.g. Hello Example. Message to
Hello class is provided by Spring framework using Dependency Injection.
Labels:
core java,
java beginners tutorial,
programming,
spring
Tuesday, June 11, 2013
How to Generate MD5 checksum for Files in Java
MD5 checksums are good to verify integrity of files and It's easy to
generate MD5 checksum in Java. Java provides couple of ways to generate MD5
checksum for any file, you can either use java.security.MessageDigest or any
open source library like Apache commons codec or Spring. All 3 ways we have
seen in our earlier article about generating MD5 hash for String
is also applicable to generate MD5
checksum for any file. Since most of md5() or md5Hex() method
takes byte[], you can simply read bytes from InputStream or pass to
these md5 methods. Apache commons codec from version 1.4 also provides an overloaded
method to accept InputStream, which makes
generating checksum very easy in Java. For those who are not familiar with
checksum, it's a fixed size datum generated from a block of data to detect any
accidental change in data. Which means once you create checksum for a file,
which is based on contents of file, any change on file e.g. adding white space,
deleting a character will result in different checksum. By comparing stored
checksum with current checksum, you can detect any change on File. It's good
practice to provide checksum of WAR or JAR files to support teams for
production release. In this Java tutorial we will learn how to create MD5
checksum for any file in Java.
Labels:
coding,
core java,
programming
Subscribe to:
Posts (Atom)
