Wednesday, August 31, 2005

AJAX in Action

Although I know that this isn't *strictly* Java programming, but I felt I needed to share this article on Ajax - 'AJAX in Action'.

Once you have read the article above, which is concise and to the point, you might want to check out this article from Sun, that answered the next question that popped into my mind - great! Now, how about an example that links it with J2EE. Read it here.

Tip: For the newcomers, here's a really basic article on the topic.
Technorati Tags:
Blogs linking to this article

Package for a Java Bean

This blog entry is for answering the question posted by Medha in the blog entry "Java Questions".

Question: Why do we need to declare a specific package name for all the beans we intend to use in JSP pages?
Answer: Even though the bean specification doesn't require it, I recommend that you always declare a specific package name for all beans you intend to use in JSP pages, via the Java package statement.

If you don't, you have to use the JSP import directive to import the bean class (with the page directive) in every JSP page that uses it. The page implementation class may use a vendor-dependent package name; since Java assumes that a class name without a package qualifier refers to a class in the same package as the class that uses it (or to a package declared by an import statement), the compiler looks for the bean class in the wrong package unless you explicitly import it.

I hope this clears your doubt, Medha.
Technorati Tags:
Blogs linking to this article

Answering Java Questions

This blog entry is for answering the questions posted by Rahul in the last blog entry "Java Questions".

Question: Why should we override the equals() method of the Object class?
Answer: equals() is one of the methods of the class java.lang.Object and the default implementation is inherited by all the instances of all the classes. Important thing to understand with the default implementation of equals() is, it compares only the references and not the state of the object. Lets take an example.
public class Person
{
private int age;
private String name;

public Person(String n, int a)
{
age = a;
name = n;
}
}
Now you have following instances of Persons
Person p1 = new Person("Rahul", 23);
Person p2 = new Person("Rahul", 23);

p1.equals(p2) should return true, right ? But if you use the default implementation of equals() given by Object, the result will be false. This happens because Object's implementation of equals() compares only references, and as a result an object is always equal to "itself". Since p1 and p2 are references to different objects the equals would return false.

Now, lets add the equals method to the class Person as following:
public boolean equals(Object p)
{
if (p == null)
{
return false;
// This is called principal of non-nullity. Essentially a comparison
// with a null reference should return false.
}
if ( ! (p instanceof Person))
{
// if p is not a Person the comparison is false.
return false;
}
if (name.equals(p.name) && (age == p.age))
return true;
else
return false;
}
Now p1.equals(p2) would return true because we are not comparing references but the "state" of the instance also. It is very important to override equals() for classes whose instances would be used as keys in the hash tables, hash maps etc for exactly the reasons mentioned above.

Question: If we override the doGet() and doPost() method of a Servlet class, which one is called first, by the container?
Answer: Lets understand the basic concept of a servlet here. In very simple words, a servlet is a way to extend the server. doGet() and doPost() are methods specific to HttpServlets and are not called by the servlet container directly.
The method which the servlet container calls on the servlet is the service() method. It is the service() method in the HttpServlet which calls doGet() or doPost() based on the type of the incoming HTTP request. If the request is a GET request, doGet() would be called and on the other hand if it is a POST request, doPost() would be called.
Some clarification: Lets understand the basic HTTP concepts over here. When the browser requests a resource from the server it will send a HTTP GET request. Which litterally stands for "GET me xyz resource". So when you type a URL like:
http://www.abcd.com/index.html. The translation done by the browser is "GET index.html" and the server sends off the file index.html to the browser.

POST is a method by which the browser wants to post or send some data to the server.

So when you directly call a servlet using URL like:
http://www.myserver.com/servlet/HelloServlet the browser will use a GET request and eventually (after service() call from container) doGet() would be called.

I hope these clear your doubts, Rahul.
Technorati Tags:
Blogs linking to this article

Tuesday, August 30, 2005

Java Questions

This post is a Work-In-Progress and I am going to post here, commonly asked Java Questions and their Answers. I am requesting all members to atleast post their questions, as comments.

2nd Sept. 2005
Questions by Vijay Jadhav
(a) Write Java code to remove the trailing spaces in a string, without using the trim() method.
Answer provided by Pushpendra Bharambe
public class TrimString
{
public static void main(String[] args)
{
String str = "Hello World ";

System.out.println("Length of str = " + str.length());
while (str.endsWith(" "))
{
int k = str.lastIndexOf(" ");
str=str.substring(0, k);
}
System.out.println("Length of new str = " + str.length());
}
}
What if we wanted to remove all spaces from a Java String?
Answer provided by Vishal Dedaniya
public class RemAllSpaces
{
public static void main(String[] args)
{
String s=" ab bc cvnf v ", s1;
int x;
char[] c;
c = s.toCharArray();
for(int i = 0; i < c.length; i++)
{
if(c[i] != ' ')
{
x=0;
System.out.print(c[i]);
}
}
}
}
(b) Write Java code to insert a character at an index, in an array of characters.
Answer awaited.

31st Aug. 2005
Question by Medha Shewale
(a) Why do we need to declare a specific package name for all the beans we intend to use in JSP pages?
Answer to Medha's question above.

Questions by Rahul Kulkarni
(a) Why should we override the equals() method of the Object class?
(b) If we override the doGet() and doPost() method of a Servlet class, which one is called first, by the container?
Answers to Rahul's questions (a) and (b) above.

Update: GeekInterview is an Open Database where you can share interview questions, comment/answer any questions without any registration just by providing name and optional email address.
Technorati Tags:
Blogs linking to this article

Interfaces Vs Abstract Classes In Java

Interfaces Vs Abstract Classes In Java - Have you ever wondered why you should use interfaces instead of abstract classes, or vice versa? More specifically, when dealing with generalization, have you struggled with using one or the other? Anthony Meyer sheds some light on what can be a very confusing issue.

Anthony Meyer is a technical director and a Java developer at Flashline.com. His experience includes the design, development and implementation of large-scale, Java-based, Internet applications in the corporate Web development environment. He has also created and implemented corporate-focused reuse strategies in the financial industry.
Technorati Tags:
Blogs linking to this article

Mustang and Dolphin

Did you know that Java 2 Platform, Standard Edition (J2SE 6.0), code name Mustang, is due in the first half of 2006 and that Dolphin, the Java SE 7 release is scheduled to follow Mustang in late 2007?
Technorati Tags:
Blogs linking to this article

Monday, August 29, 2005

Java Language Keywords

It's surprising that sometimes even experienced Java programmers are not sure whether a particular word is a Java Language Keyword or not. The Java Tutorial has an updated list, that all can refer to.
Technorati Tags:
Blogs linking to this article

Hello World

My very first blog entry on PuneJava blog. Nothing much to write here today. I hope to contribute something worth reading here soon :-).

Here is something funny in Java. Try this program

public class FunnyOne
{
public static void main(String args[]) {
int i = 0;
System.out.println("i = " + i);
i = i++;
System.out.println("i = " + i);
}
}

Give it a try and take a look at the output. If the output is not what you expected and you know why, please comment this blog entry with the explaination.

Or I will try to explain this in my next blog entry.

Until then, Ciao !

Technorati Tags:
Blogs linking to this article

First Post

Some of you indicated that we start a PuneJava blog and so here it is. This blog can only evolve if you all contribute your time towards the same. Our member, Nikhil Palshikar pointed out that this blog could "collect and post articles that explained the fundamental aspects of Java and encouraged Java developers to read and understand these aspects. This in turn would take them far in their professional life". Kindly post your comments to this first post and make your suggestions. Angelina Peters, Sanjay Hande, Shailesh Barde and Abhijit were some of the first who were all for, for a PuneJava blog.
Update (30/8): Dileep Dharma, Nikhil Palshikar and Mayuresh Kadu have agreed to contribute articles on a regular basis. If anyone else is interested in contributing articles, Java related news items etc., please send me an email.
Technorati Tags:
Blogs linking to this article