Java concurrency model uses locks to implement mutually exclusive access to objects in a multi-threaded environment and locks are associated with every object in Java (of type 'Object'), not only with Threads.
wait, notify/notifyAll methods are used by threads to communicate with each other while trying to access a common object. Putting it differently, objects become a medium via which threads communicate with each other. For example: suppose there is a 'telephone' object, which at one point of time can be used by...
Wednesday, 26 December 2012
Thursday, 20 December 2012
Can a class (whether an inner or outer class) be declared static?
In order to understand the use of the static keyword in class declaration, we need to understand the class declaration itself. You can declare two kinds of classes: top-level classes and inner classes.
Top-level classes You declare a top-level class at the top level as a member of a package. Each top-level class corresponds to its own java file sporting the same name as the class name....
Tuesday, 18 December 2012
Annonymous Inner Classes In Java
A class that have no name is called anonymous inner class;
They can be created by using:
1. class
2. abstract class
3. interfaces
Example 1:
lets consider a abstract class
package abstractconcept;public abstract class MyAbstract { public String name; public int age; /**Abstract Method*/ public abstract int sum(int a,int b); /** * Concrete Method */ public int sub(int c,int d) { ...
Monday, 17 December 2012
Password At Command Prompt - console.readPassword()
import java.io.*;class Abc{public static void main(String args[]){System.out.println("hello");Console console=System.console();console.printf("Enter Password\n");char a[]=console.readPassword();for(int i=0;i<a.length;i++){console.printf("%c",a[i]);}...
Saturday, 15 December 2012
Marker Interface in Java: what, why, uses ?
What are Marker Interfaces in Java?An empty interface having no methods or fields/constants is called a marker interface or a tag interface. This of course means if the interface is extending other interfaces (directly or indirectly) then the super interfaces must not have any inheritable member (method or field/constant) as otherwise the definition of the marker interface (an entirely empty interface) would not be met. Since members of any interface are by default 'public' so all members will be inheritable and hence we...
Does 'static' cause Memory Leak in Java?
Does 'static' cause Memory Leak in Java?What's memory leak? In simple terms, it's unused but referenced (somehow because the programmer probably unintentionally forgot to remove the references once the use of the object was over) part of the memory. Before we start discussing if 'static' can cause memory leak in Java, let me assure you that whatever you've read about Garbage Collectors in Java, is still valid and it certainly takes care of most (almost all) of the memory allocation/reclaimation of Java objects. But, that...
Why String has been made immutable in Java?
Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files...
clone() how cloning works in Java?
What is the role of the clone() method in Java?protected Object clone() throws CloneNotSupportedException - this method is used to create a copy of an object of a class which implements Cloneableinterface. By default it does field-by-field copy as the Object class doesn't have any idea in advance about the members of the particular class whose objects call this method. So, if the class has only...
Jackson Tree Model Example
In Jackson, you can use “Tree Model” to represent JSON, and perform the read and write operations via “JsonNode“, it is similar to DOM tree for XML. See code snippet :
ObjectMapper mapper = new ObjectMapper();
BufferedReader fileReader = new BufferedReader(
new FileReader("c:\\user.json"));
JsonNode rootNode = mapper.readTree(fileReader);
/*** read value from key "name" ***/
JsonNode nameNode = rootNode.path("name");
System.out.println(nameNode.getTextValue());
This time, we show you how to read...
How To Convert Java Map To / From JSON (Jackson)
This time, we are doing the same, but replace user define object with Map.
Map is more suitable in a simple and short JSON processing, for complex JSON structure, you should always create an user defined object for easy maintainability.
In this tutorial, we show you how to use Map to construct the JSON data, write it to file. And also how to read JSON from file, and display each value via Map.
1. Write JSON to file
Jackson example to use Map to construct the entire JSON data structure,...
How To Convert Java Object To / From JSON (Jackson)
JSON (JavaScript Object Notation), is a simple and easy to read and write data exchange format. It’s popular and implemented in countless projects worldwide, for those don’t like XML, JSON is a very good alternative solution.
In this series of Java JSON tutorials, we focus on three popular third party Java libraries to process JSON data, which areJackson, Google Gson and JSON.simple
For object/json conversion, you need to know following two methods :
//1. Convert Java object to JSON format
ObjectMapper...