All posts in category : java

DNS

Java – Get nameservers of a website

In this tutorial, we will show you how to get the nameservers of a website using Java + dig command. 1. Using Dig Command 1.1 On Linux, we can use dig command to query a DNS lookup of a website, for example : $ dig any mkyong.com   //… mkyong.com.Continue Reading

java_logo_100

Find out your Java heap memory size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm. Heap sizes Initial heap size of 1/64 of physical memory up to 1Gbyte Maximum heapContinue Reading

java_logo_100

Java – Append values into an Object[] array

In this example, we will show you how to append values in Object[] and int[] array. Object[] obj = new Object[] { "a", "b", "c" }; ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj)); newObj.add("new value"); newObj.add("new value 2"); 1. Object[] Array Example Example to append values with ArrayList : TestApp.java package com.mkyong.test;Continue Reading

java_logo_100

Java – Convert int[] to Integer[] example

Examples show you how to convert between int[] and its’ wrapper class Integer[]. 1. Convert int[] to Integer[] public static Integer[] toObject(int[] intArray) {   Integer[] result = new Integer[intArray.length]; for (int i = 0; i < intArray.length; i++) { result[i] = Integer.valueOf(intArray[i]); } return result; } 2. Convert Integer[]Continue Reading

apache_logo

The type DefaultHttpClient is deprecated

Eclipse IDE prompts warning on new DefaultHttpClient, mark this class as deprecated. package com.mkyong.web.controller;   import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;   public class WebCrawler {   public static void main(String[] args) throws Exception {   HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://mkyong.com");Continue Reading

google-Bot

Java – Check if web request is from Google crawler

If a web request is coming from Google crawler or Google bot, the requested “user agent” should look similar like this : Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) or (rarely used): Googlebot/2.1 (+http://www.google.com/bot.html) Source : Google crawlers 1. Java Example In Java, you can get the “user agent” from HttpServletRequest. Example :Continue Reading

java_logo_100

How To Get HTTP Request Header In Java

This example shows you how to get the HTTP request headers in Java. To get the HTTP request headers, you need this class HttpServletRequest : 1. HttpServletRequest Examples Example 1.1 – Loop over the request header’s name and print out its value. import javax.servlet.http.HttpServletRequest;   //… private HttpServletRequest request;  Continue Reading

java_logo_100

Java – Write directly to memory

You have been told a lot that you can’t manage memory in Java. Well, it has changed since HotSpot release of the Java VM. There is a way to directly allocate and deallocate memory as well as write and read it… Of course we are talking about the JVM memoryContinue Reading

java-custom-annotation

Java Custom Annotations Example

In this tutorial, we will show you how to create two custom annotations – @Test and @TestInfo, to simulate a simple unit test framework. P.S This unit test example is inspired by this official Java annotation article. 1. @Test Annotation This @interface tells Java this is a custom annotation. Later,Continue Reading

java-ip-to-decimal

Java – Convert IP address to Decimal Number

In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vice versa. For examples : 255.255.255.255 <-> 4294967295 192.168.1.2 <-> 3232235778 1. IP Address to Decimal We show you two ways to convert an IP address to a decimal number NormalContinue Reading

lshiftLeft

Java and “& 0xFF” example

Before you understand what is & 0xFF, make sure you know following stuffs : Bitwise AND operator, link. Converts hex to/from binary, and decimal to/from binary. In short, & 0xFF is used to make sure you always get the last 8 bits. Let’s see an example to convert an IPContinue Reading

Calendar

Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar. 1. Java Date Examples Few examples to work with Date APIs. Example 1.1 – Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); String date = sdf.format(new Date()); System.out.println(date); //15/10/2013 Example 1.2 – Convert String to Date. SimpleDateFormat sdfContinue Reading

java_logo_100

Where to download Java JDK source code ?

The JDK source code is inside the src.zip, this article shows you how to get it on Windows, Ubuntu (Linux) and Mac OSX. 1. Windows In Windows, visit the Oracle website and download the JDK (not JRE). Install the JDK and the src.zip is inside the JDK installed folder, forContinue Reading

java_logo_100

Java – How to calculate leap year

A leap year is a year containing one additional day (366 days a year). Review the leap year algorithm : if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year P.S AlgorithmContinue Reading

java_logo_100

Java – Time elapsed in days, hours, minutes, seconds

Two Java examples show you how to print the elapsed time in days, hours, minutes and seconds format. 1. Standard JDK Date APIs You need to calculate the elapsed time manually. DateTimeUtils.java package com.mkyong.dateUtils;   import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;   public class DateTimeUtils {   public static voidContinue Reading

Page 1 of 1712345...10...Last »