Showing posts with label -C-O-N-C-E-P-T- Series. Show all posts
Showing posts with label -C-O-N-C-E-P-T- Series. Show all posts

Sunday, March 13, 2011

Serializing / De-serializing bean object using XMLEncoder / XMLDecoder

The java.beans package provides very useful classes [XMLEncoder / XMLDecoder] to save an object state [persist] into an XML file [Serialize] and easily read it back [De-serialize]. It's very helpful while debugging the state of objects especially in the production environments.


No need to provide any examples as the below are some useful links that already contains the same:

Thursday, February 5, 2009

-C-O-N-C-E-P-T- Series : Java

Difference between using "" and toString() method for converting object value to String:

Case I - Using "":

-----------------------------------------------------------------
BigDecimal dec = new BigDecimal("1234567");
String str = "" + dec; // converting dec to String
System.out.println("str: "+str);

o/p:
str: 1234567
-----------------------------------------------------------------

-----------------------------------------------------------------
BigDecimal dec = null;
String str = "" + dec; // converting dec to String
System.out.println("str: "+str);

o/p:
str: null
-----------------------------------------------------------------


Case II - Using toString() method:

-----------------------------------------------------------------
BigDecimal dec = new BigDecimal("1234567");
String str = null;

if( dec!=null ) {
str = dec.toString(); // converting dec to String
}

System.out.println("str: "+str);

o/p:
str: 1234567
-----------------------------------------------------------------

-----------------------------------------------------------------
BigDecimal dec = null;
String str = null;

if( dec!=null ) {

str = dec.toString(); // converting dec to String
}

System.out.println("str: "+str);

o/p:
str: null
-----------------------------------------------------------------

In first case, we don't require null check, but doesn't has readability.
In second case, we require null check, but has readability.

Each one has its own ease of use.

Thursday, July 3, 2008

-C-O-N-C-E-P-T- Series : Java

Synchronizing a static method and its effect in the multi-threading environment:

Since static code can modify only the static data, you will protect a static data with synchronized keyword. We know that static data and methods have only one copy for all the objects of that class. Therefore, you only need one lock per class to synchronize static methods. Object locks are not required for static methods. Static methods use a class lock for synchronization. This special lock is also an object lock.

Consider following java code:


Fig1


Fig2

Both the figure above are self explanatory, but I would like to elaborate more on Fig2. When we synchronize an instance method, the thread accessing that method acquires "Object Lock" on the object such that no other thread can access that method unless it completes its execution. Now for "synchronized static" method, it acquires "Class Lock" on that class. This class lock also implicitly has object lock, it's because as non-static methods can modify static variables, so we need to also put a lock on synchronized instance variables & this is internally done by the class lock.
Hence if one thread is executing a static synchronized method of a class then it holds a lock on all other synchronized methods of that class and in effect no other thread can call any of the synchronized static methods of that class until this current thread releases the lock.

This concept is really very interesting to learn :)

Stay tuned... :)

Thursday, June 26, 2008

-C-O-N-C-E-P-T- Series : Protocols for interfacing interactive web applications with web servers

In continuation with my previous post [here]

Following are different protocols for interfacing interactive web applications with web servers: SSI, CGI, SCGI, FastCGI, PHP, Java Servlet, JavaServer Pages, ASP, ASP .NET

(1) FastCGI
Web servers using this protocol: Nginx, Apache HTTP server (partial), Lighttpd (partial), etc.

(2) Java Servlet & JavaServer Pages
Web servers using this protocol: Apache Tomcat, WebLogic, WebSphere, etc.

(3) ASP & ASP .NET
Web server using this protocol: Microsoft IIS

Wednesday, June 25, 2008

-C-O-N-C-E-P-T- Series : Web / HTTP Server, Sevlet Container and Application Server

Static Web / HTTP server: It is responsible for handling HTTP requests and sending back static files as HTTP responses
Examples: Apache, lighttpd, nginx, Microsoft IIS, etc.

Servlet container: It handles HTTP requests but also add layers to process those requests, wrap them around Java objects that implement well defined interfaces and implement the servlet container architecture so that a java developer can easily use the API to respond to the requests, manage sessions, cookies, receive GET & POST params, and many more.
Examples: Apache Tomcat, etc.

Application server: It's more than the web server and servlet container. You can find lot of information here
Examples: JBoss, WebLogic, WebSphere, etc.

Tuesday, June 24, 2008

-C-O-N-C-E-P-T- Series : Java

Object's Shallow Copy and Deep Copy in Java:


Understanding concept:
Suppose I have following two objects:



Shallow copy: "obj2" refers to the memory block pointed by "obj1" as shown below.



Deep Copy: Here the data is actually copied over from "obj1" to "obj2" as shown below.



Java code:



(You can click on image to enlarge it)

Output:



Any comments are always welcome :)

Thursday, June 19, 2008

-C-O-N-C-E-P-T- Series : Java

Enhanced for loop:

Code snippet:




Output of the program is:
4
0
0
3
0




Here, "i" is directly assigned the each value from array in each iteration.
Iteration 1: Value of i = 1 (arr[0])
Iteration 2: Value of i = 2 (arr[1])
Iteration 3: Value of i = 3 (arr[2])
Iteration 4: Value of i = 4 (arr[3])



The above diagram is self-explanatory. Really interesting concept & worth learning :)

Friday, March 14, 2008

-C-O-N-C-E-P-T- Series : Javascript eval() Function

The eval() function executes a string as through it were a JavaScript statement.

Syntax: eval(string)

where string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Description

The argument of the eval() function must be a string that can also be executed as a valid JavaScript statement. If the string represents an expression, eval() evaluates the expression. If the string is a numeric sring character, eval() returns the number. If the argument represents one or more JavaScript statements, eval() performs the statements.

Example

When executed, this script






displays this on the screen:

eval(string1) = 50

eval(executable) produces

Please feel free to correct me at any point :)

Wednesday, March 12, 2008

-C-O-N-C-E-P-T- Series : Making simultaneous AJAX calls to the same servlet

AJAX calls are "asynchronous" in nature. So what does this mean? You can have multiple requests to same (or different) servlets with each request processed asynchronously. Right, so are you speaking anything new?? Yes I am :) When you make simultaneous AJAX calls to the same servlet, the last call overlaps the response text with the previous, so as a result you get only the last requet's response text i.e. HTTP requests are overlaped. I faced this this problem in one of our Project, so I thought to share some views on it.

Examples:

Traditional code:


function ajaxRequest() {
...
myRequest.onreadystatechange = callBack; // assign callback function
...
}

function callBack() {
if (myRequest.readyState == 4) {
if (myRequest.status == 200) {
// Update DOM
}
}
}


Rearranging the above code:

Now we make some rearrangement of the AJAX code so that multiple HTTP requests can go on simultaneously. To do this, the XMLHTTP request object must be instantiated inside the Javascript function that calls the server. Furthermore, all code related to the request must be inside this function. The callback function must be an anonymous inner function within the ajaxRequest() function, and the code which updates the page after the callback must also be within the anonymous inner function.


function ajaxRequest() {
...
myRequest.onreadystatechange = function()
{
if (myRequest.readyState == 4)
{
if (myRequest.status == 200)
{
// update DOM with results
}
}
} // end anonymous inner function (callback)
...
}


Doing this I was able to handle multiple AJAX calls simultaneously.

Please feel free to correct me at any point :)

Tuesday, February 26, 2008

-C-O-N-C-E-P-T- Series : Java

<CONCEPT>

Casting String to Boolean in Java:

We can achieve this using the Boolean class' static method:
Boolean.parseBoolean(String s) - returns a boolean value

Sample code:


String str = "true";
if( Boolean.parseBoolean(str) ){
    System.out.println("Value is true");
} else{
    System.out.println("Value is false");
}


Casting Boolean to String in Java:

We can achieve this using the String class' static method:
String.valueOf(boolean b) - returns the string representation of the boolean argument

Sample code:


boolean blnFlag = true;
String strVal = String.valueOf(blnFlag);

</CONCEPT>

-C-O-N-C-E-P-T- Series

Hello Friends,

It have been a very long time since I've posted my last blog. I have came up with a new idea of posting blogs on any kind of CONCEPT related to programming(any language), design patterns or any technical stuff. Please feel free to correct me at appropriate points.

Thanks.

Keep visiting... :)


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________