Is there any constant in Java? how to declare it?

Recently, I’m been asked by my colleague (C++ programmer) regarding why java does not contains a constant keyword? Actually java do contains constant function , but it just appear as different keyword – final. A final variable in java is equal to C++ constant. The final keyword is declare before a data type, it make …

Read more

Why i choose Hibernate for my project?

Currently my company is using IBATIS and pure SQLs as database persistence mechanism. I like SQL query very much, especially in tuning, but i do not like to write all SQL statement in Java application, it’s easy to hit typo error and what a stupid and tedious job? Finally my company has a new project …

Read more

How to read file in Java – BufferedInputStream

Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStream classes. The readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader. You may interest to read this How to read file from …

Read more

How to read file in Java – BufferedReader

In this article, we will show you how to use java.io.BufferedReader to read content from a file Note Read this different ways read a file 1. Files.newBufferedReader (Java 8) In Java 8, there is a new method Files.newBufferedReader(Paths.get(“file”)) to return a BufferedReader filename.txt A B C D E FileExample1.java package com.mkyong; import java.io.BufferedReader; import java.io.IOException; …

Read more

Java HashMap example

HashMap is an object that stores both key=value as a pair. This HashMap permits null values and the null key, unsynchronized and no guarantees to the order of the map. ["key","value"] = ["java","mkyong.com"] 1. HashMap Basic 1.1 Add an Item Map map = new HashMap<String, String>(); map.put("PostgreSQL", "Free Open Source Enterprise Database"); 1.2 Get an …

Read more

Adapter Design Pattern

Adapter , Adapter , Adapter ~ Actually Adapter design pattern can consider as a simple conversion program / class. It usually used to make two incompatible interfaces or classes to work together. Please review below Adapter design pattern image draw by me, a bit ugly 🙂 Case Study Company A developed a program in Java …

Read more

Java Web Start (Jnlp) Tutorial

Here is a brief explanation about Java Web Start from SUN “Java Web Start is a mechanism for program delivery through a standard Web server. Typically initiated through the browser, these programs are deployed to the client and executed outside the scope of the browser. Once deployed, the programs do not need to be downloaded …

Read more

Open Browser in Java windows or Linux

A very useful Java code, to open a web browser from Java application in windows or Linux. package com.mkyong; public class StartBrowser { public static void main(String args[]) { String url = "http://www.google.com"; String os = System.getProperty("os.name").toLowerCase(); Runtime rt = Runtime.getRuntime(); try{ if (os.indexOf( "win" ) >= 0) { // this doesn’t support showing urls …

Read more

How to install java jdk on fedora core (linux)

How to install java jdk on fedora core? here i provide few steps to demonstrate how it’s work. Installation Setup 1 ) Please visit sun java website to download any java jdk version you like. http://java.sun.com/javase/downloads/index.jsp 2 ) Click download, select Linux platform, language and accept license and continue. 3 ) Select “Linux RPM in …

Read more

Java – Check if array contains duplicated value

This article shows a few ways to detect if an array contains a duplicated value. Java 8 Stream, compare the array size. TreeSet, compare the array size. Two for loops, classic algorithm, 0(n^2). Set, 0(n). Bitmap, boolean[] 0(n). All the above solutions are tested with the below program and generate the same output. (except 5. …

Read more