Showing posts with label Java Programming Tutorials. Show all posts
Showing posts with label Java Programming Tutorials. Show all posts

Wednesday 28 August 2013

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

We often needs to convert byte arrays to Hex String in Java, In order to print byte array contents in readable format. Since many cryptographic algorithm e.g. MD5 return hash value as byte array, In order to see and compare those byte array, you need to convert byte array to Hex String. As we have seen, while generating MD5 check sum 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 byte array in Java. Commons codec provides a utility class Hex, which contains a encodeHexString() method to create Hex String from byte array. It's one of the best option 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 issue with printing byte array as normal String, and 2 examples to convert byte array into Hexadecimal String in Java.
Read more »

Sunday 25 August 2013

Top Most Difference between valueOf and parseInt method in Java

Both valueOf and parseInt methods are used to convert String to Integer in Java, but there are subtle difference between them. If you look at code of valueOf() method, you will find that internally it calls parseInt() method to convert Integer to String, but it also maintains a pool of Integers from -128 to 127 and if requested integer is in pool, it returns object from pool. Which means two integer objects returned using valueOf() method can be same by equality operator. This caching of Immutable object, does help in reducing garbage and help garbage collector. Another difference between parseInt() and valueOf() method is there return type. valueOf() of java.lang.Integer returns an Integer object, while parseInt() method returns an int primitive. Though, after introducing Autoboxing in Java 1.5, this doesn't count as a difference, but it's worth knowing.
Read more »

Friday 23 August 2013

Top Most 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 third array. But being a Java developer, I first looked in JDK to find any method which concatenate 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 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 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 »

Thursday 22 August 2013

Top Most 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, 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 »

Tuesday 20 August 2013

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

There are multiple ways to generate MD5 hash in Java program. Not only Java API provides convenient method to generate 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 check-sum to see, if any file is altered or not. Though, MD5 was not considered a good cryptographic algorithm for security purpose due to several vulnerability found on it, it's still good enough or checking integrity of file. MD5 hashing algorithm generate 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 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 than you need to convert byte array to Hex String, as java.security.MessageDigest.digest() method returns MD5 hash as 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 MD5 hash or digest using Java, Spring and Apache commons code library.
Read more »

Monday 19 August 2013

Top Most 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 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 »

Friday 16 August 2013

Top Most 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 second step for any one who wants to learn Java programming. Of course, 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 »

Monday 12 August 2013

Top Most 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 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 »

Friday 2 August 2013

Top Most Fundamentals of Object Oriented Programming in Java

I remember when I was doing my engineering , OOPS concept was in first semester. Back then it looks like some alien things to me, because I have never done programming and even the smallest program we get to write e.g. checking whether number is palindrome or not , or printing Fibonacci series , we never really needed to use OOPS. When needed, I just memorize things and written down in exam :). That worked, I passed the exam but effectively I didn't learn OOPS or Object Oriented Programming on those days.

It's when, I started learning java (an almost truly object oriented programming language) and introduced to concept like abstract classes, interface, and method overriding; some thing started to going in my mind. This blog post is very basic in nature, but I really hope it could help beginners on understanding things like class, objects, abstraction , polymorphism, inheritance ,encapsulation , composition , cohesion , coupling , interface programming etc.
Read more »

Thursday 1 August 2013

Top Most How to write Unit Test in Java using JUnit4 in Eclipse and Netbeans

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 unit test and why should you write them. Unit test is to test smaller unit of code, e.g. methods.  Writing unit test to test individual unit of code is one of the best development practice and helps to find bug earlier in development cycle. Though there are other unit testing framework available in Java e.g. TestNG, JUnit has it’s 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 don’t write unit test due to ignorance and others due to laziness. Any way, it’s just start which take time, once you start writing unit tests, you will automatically start enjoying it. I have seen Java developers testing there code with main() method, but now they prefer to test them with JUnit testcases. I agree few initial tests are difficult because of knowledge and inertia and best way to approach is to start with simplest of JUnit tests. In this JUnit tutorial, I will show you how to write and execute JUnit test from Eclipse and Netbeans, two popular Java IDE. By the way, if you are looking for any good book on JUnit and unit testing, you should look Pragmatic Unit Testing in Java with JUnit, it's an amazing book and teaches a lot about both JUnit and unit testing in Java.
Read more »

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt