Posts

Showing posts with the label Java Programming Tutorials

How to Convert and Print Byte array to Hex String in Java

We often need to convert byte arrays to Hex String in Java, In order to print byte array contents in a readable format. Since many cryptographic algorithms e.g. MD5 returns hash value as a byte array, In order to see and compare that byte array, you need to convert byte array to Hex String. As we have seen, while generating MD5 checksum of File , there are multiple ways to convert byte array to Hexadecimal String in Java . You can either write your own method, or you can use open source library e.g. Apache commons codec to create Hex String from a byte array in Java. Commons codec provides a utility class Hex , which contains an encodeHexString() method to create Hex String from a byte array . It's one of the best options to genera te Hex String if you a re already using this library to generate MD5 hash values. In this Java tutorial, we will see what is the issue with printing byte array as normal String, and 2 examples to convert a byte array into Hexadecimal String in Java. Re...

How to Reverse Array in Java - Int and String Array Example

This Java tips is about, how to reverse array in Java, mostly primitive types e.g. int , long , double and String arrays. Despite of Java�s rich Collection API, use of array is quite common, but standard JDK doesn�t has great utility classes for Java. It�s difficult to convert between Java Collection e.g. List , Set to primitive arrays. Java�s array utility class java.util.Arrays , though offers some of critical functionalities like comparing arrays in Java and support to print arrays , It lacks lot of common features, such as combining two arrays and reverse primitive or object array. Thankfully, Apache commons lang, an open source library from Apache software foundation offers one interesting class ArrayUtils, which can be used in conjunction with java.util.Arrays class to play with both primitive and object array in Java. This API offers convenient overloaded method to reverse different kinds of array in Java e.g. int, double, float, log or Object arrays. On a similar note,...

How to generate MD5 Hash in Java - String Byte Array digest Example

There are multiple ways to generate the MD5 hash in Java program. Not only Java API provides a convenient method for generating MD5 hash, you can also use popular open source frameworks like Spring and Apache commons Codec to generate MD5 digest in Java. MD5 is popular Message Digest Algorithm, which is most commonly used to check data integrity e.g. comparing MD5 checksum to see, if any file is altered or not. Though MD5 has not considered a good cryptographic algorithm for security purpose due to several vulnerabilities found on it, it's still good enough or checking the integrity of the file. MD5 hashing algorithm generates a 128 bit or 16-byte long hash value. MD5 hash values , also known as MD5 digest is mostly represented as 32 character Hex String. You can generate an MD5 hash from a byte array , or String directly using Java, Spring and Apache commons codec. Spring and Apache commons codec has identical API e.g. class name DigestUtil s is same and allows you to directl...

Bitwise and BitShift Operators in Java - AND, OR, XOR, Signed Left and Right shift Operator Examples

Bitwise and Bit Shift operators in Java are powerful set of operators which allows you to manipulate bits on integral types like int, long, short, bytes and boolean data types in Java. Bitwise and Bit shift operator are among the fastest operator in Java but still many Java programmers doesn't familiar with bitwise and bitshift operations, especially those who doesn't come from C programming backgrounds. If you have already learned C or C++ before starting with Java then understanding bitwise and bitshift operators are quite easy in Java, because its similar to bitwise operation in C. Some of th e tricky programming interview questions e .g. checking if a number is power of tw o or swapping two numbers without temporary variable or, can be easily solved using bitwise operators. This Java programming tutorial is quick recap of different bitwise operator available in Java and how to use them. This tutorial also discusses bit shift operator, both signed and unsigned with example...

Steps to Create JUnit Test in Eclipse and Netbeans IDE

Writing JUnit tests for Java classes in Eclipse and Netbeans IDE are super easy, and I will show you with that later in this JUnit tutorial. Before that, let�s revise what is a unit test and why should you write them. A unit test is to test smaller unit of code, e.g. methods. Writing a unit test to test individual unit of code is one of the best development practice and helps to find bug earlier in the development cycle. Though there is other unit testing framework available in Java e.g. TestNG , JUnit has its own place among Java developers. IM HO code review and unit tes ting are two most important practices for improving code quality and should always be followed during software development. Sad thing is that not every developer follows it; some programmer doesn't write unit test due to ignorance and others due to laziness. Anyway, it just starts which take time, once you start writing unit tests, you will automatically start enjoying it. I have seen Java developers testing ...

2 ways to combine Arrays in Java � Integer, String Array Copy Example

There are multiple ways to combine or join two arrays in Java, both for primitive like int array and Object e.g. String array. You can even write your own combine() method which can use System.arrayCopy() to copy both those array into the third array. But being a Java developer, I first looked in JDK to find any method which concatenates two arrays in Java. I looked at java.util.Arrays class, which I have used earlier to compare two arrays and print arrays in Java , but didn't find a direct way to combine two arrays. Then I looked into Apache Commons, ArrayUtils class, and bingo, it has several overloaded method to combine int , long , float , double or any Object array. Later I also found that Guava ,earlier known as Google collections also has a class ObjectArrays in com.google.common.collect the package, which can concatenate two arrays in Java. It's always good to add Apache commons and Guava , as supporting library in Java project, they have lots of supporting clas...

How to install JDK 7 on Windows 8 - Java Programming Tutorial

Installing JDK is first step in learning Java Programming. If you are using Windows 8 or Windows 7 Operating System, than installing JDK is quite easy as you just need to follow instruction given by Java SE Installation wizard. Only thing which requires some attention is, choosing correct JDK installer based upon, whether you are running with 32-bit or 64-bit Windows 8 or Windows 7 OS. JDK 7 is latest Java version but JDK 6 is still most popular in software and programming world. You can choose to install JDK 7 or JDK 6 based upon your course material. In this Java tutorial, we will learn how to install JDK 7 in Windows 8 operating system by following step by step guide. Another thing, which is part of JDK installation is setting PATH for Java in Windows 8 , this will enable to run javac and java command from any directory in Windows 8. See that link for step by step guide on setting PATH in Windows 8. Though, I will use Windows 8 operating system to install JDK 7, You can still ins...

How to set Java Path and Classpath in Windows 8 and Windows 7 - Tutorial

So, you just bought a new PC or Laptop with Windows 8 operating system, and wondering how to set PATH and Classpath on Windows 8; Or, you might have just upgraded your windows 7 laptop to professional edition of Windows 8 and looking to set JDK Path to compile Java programs. Not to worry, this is the second step for anyone who wants to learn Java programming. Of course, the first step is to install JDK. In this Java tutorial, we will see step by step guide to set Java PATH and CLASSPATH in Windows 8 operating system . By the way, if you are learning Java in Windows 7 operating system, You should still be able to set Java path and classpath in Windows 7 by following steps given here, because navigation steps for modifying environment variables on Windows 7 and Windows 8 are almost same. Read more �