Tampilkan postingan dengan label Java Programming Tutorials. Tampilkan semua postingan
Tampilkan postingan dengan label Java Programming Tutorials. Tampilkan semua postingan

Kamis, 28 Maret 2013

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 generate Hex String if you are 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.
Read more �

Jumat, 22 Maret 2013

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, Setto 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, you can also write your own utility method to reverse Array in Java, and this is even a good programming question. For production usage, I personally prefer tried and tested library methods instead of reinventing wheel. Apache commons lang fits the bill, as it offer other convenient API to complement JDK. In this Java tutorial, we will reverse int and String array in Java using ArrayUtils to show How to reverse primitive and object array in Java.
Read more �

Rabu, 20 Maret 2013

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 DigestUtils is same and allows you to directly generate MD5 hash as Hex String, while if you use Java then you need to convert byte array to Hex String, as java.security.MessageDigest.digest() method returns MD5 hash as a byte array. Earlier we have seen, How to encode and decode String in base64 encoding, and In this Java tutorial we will see, How to generate a MD5 hash or digest using Java, Spring and Apache commons code library.
Read more �

Selasa, 12 Maret 2013

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 the tricky programming interview questionse.g. checking if a number is power of two 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.
Read more �

Jumat, 01 Maret 2013

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. IMHO code review and unit testing 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 their code with main() method, but now they prefer to test them with JUnit test cases.
Read more �

Sabtu, 23 Februari 2013

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 classes, utility and method, which complements rich Java API. In this Java programming tutorial, we will see an example of these 2 ways to combine, join or concatenate two arrays in Java and will write a method in core Java to concatenate two int arrays in Java.
Read more �

Selasa, 19 Februari 2013

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 install JDK 6 or 7 in Windows 7 by following those steps. In order to choose correct JDK installer, we first need to check if Windows is running with 32-bit or 64-bit Windows 8. For that you can go to Control Panel-->System and Security and check System Type, if it shows 32-bit operating system means you need Windows x86 Java Installer, if it shows 64-bit operating system than you need Windows x64 Java Installer. Let's see steps involved to install JDK 6 or JDK 7 in Windows 8 operating system.
Read more �

Sabtu, 16 Februari 2013

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 �