Category: Java

Java – How to declare Hexadecimal literal ?

Problem statement You need to declare an Hexadecimal literal in Java and display the result on a console window. Solution Hexadecimal numbers can contain numbers from 0-9 as well as characters A-F where A-F represents 10-15. You can prefix the numbers with 0X to denote it as hexa decimal number as shown below. The output of the above program will be 10 21

How to Get the localhost IP address in Java ?

If you want to get the localhost IP address in java , you can use the InetAddress class which exposes the getLocalHost method and later use the getHostAddress method of the InetAddress. The getLocalHost method returns the host information of type InetAddress . The InetAddress type has the method getHostAddress which returns the IP address. How to Get the localhost IP address in Java ?…

How to search array elements in Java using Binary Search ?

Do you want to search for an element in a array in Java using Binary Search algorithm ? . If yes , its pretty much easy to do it using the binarySearch method exposed by the Arrays class. How to search array elements in Java using Binary Search ? Below is an example of searching an element using Binary search from array elements in Java.

How to sort an array in Java ?

If you want to sort an array in Java , you can use the Arrays.sort utility method to do it. How to sort an array in Java ? Below is a sample code snippet demonstrating the usage of the Arrays.sort utility method to sort an array in java.

Java – How to document your code ?

Problem Statement You need to document your java classes so that it helps in maintenance. Solution Use Javadoc and place comments before any method , field or class to document. Just enter /** to start writing the comments using javadoc. The subsequent lines needs to begin with * and end the comment section with */ Below is a sample code demonstrating the usage of the…

Java How to – Get Environment Variables

Problem Statement You need to programmatically get the values of the environment variables from your java program. Solution Use java.lang.System.getenv method to get the environment variables from your java program from Java JDK 1.5 and higher version. You can pass the “PATH” as parameter to the method to retreive the path from the environment variable. When no arguments are passed, it returns all the environment…

Java – How to round a BigDecimal value in Java ?

Problem Statement You need round the BigDecimal value in your Java program. Solution Use the setScale method that is defined in the BigDecimal to round the value of the BigDecimal. You can also set the parameters like BigDecimal.ROUND_HALF_UP etc to the method. Output of the above program is Abundantcode.com Java Tutorials 10102.16

Developing a Java program – Hello World

There are lot of IDE options for the developers to create a java application . In this article , we will use Jet brains IntelliJ IDEA Community edition of the IDE to develop the Java console application. Developing Java program in IntelliJ IDEA 1. Download and install Jet brains IntelliJ IDEA Community edition . Make sure you have the Java (JDK) installed on your PC…