Archive for category Java

Difference in App and Web Server

Here is a very good article describing difference between application server and web server.

In another article, we can see what differentiates a full fledged Java EE application from a Java Web application.

Leave a comment

Method Overloading Ambiguity Problem

While going through an article about method overriding in OOP, I stumbled upon an ambiguity problem in overriding.

Here is the java code I wrote to test it:

public class OverLoadingTest {

static void testOverload(int i,double d) {
System.out.println("testOverload 1: int, double");
}

static void testOverload(double d,int i) {
System.out.println("testOverload 2: double,int");
}

public static void main(String[] args) {

testOverload(2,3.5);
testOverload(2.5,3);
testOverload(2,3); // Compilation error:  The method testOverload(int, double) is ambiguous for the type OverLoadingTest
  }
}

In the third call testOverload(2,3), there will be a compilation error, because:

First argument 2 is int, so java compiler will decide to call the method testOverload(int,double) because its first argument is int, so it is better match than .testOverload(double,int)

Second argument 3 is also int, so java compiler will decide to call the method testOverload(double,int) because its second argument is int, so it is better match than testOverload(int,double)

As compiler will not be able to decide the right overloaded version for call, compilation error occurs.

Happy Coding !

Leave a comment

toLowerCase() is Locale Based

I have found this very interesting and informative post for Java developers. Many Java developers may not know that toLowerCase() and toUpperCase() methods of java.lang.String class are Locale based. Have a look here at javadocs for these methods.

toLowerCase() internally calls toLowerCase(Locale locale). Same is the case for toUpperCase().

Leave a comment

Reading rss feed using java (JSP Taglib)

We can read RSS (Real Simple Syndication) feeds and publish them on our web page using JSP(Java Server Pages) tag library. Sun provides a rss taglib that can be used in JSP. It is extremely simple to use. You just need to have basic knowledge of JSP and HTML.

So, how to do that? First you need to download the tag library file (rssutils.tld, rssutils.jar) from Sun’s website. Here is the url to download it. Put the files rssutils.tld and rssutils.jar in WEB-INF/lib directory of your web application.
After that, choose a blog or website who publishes its rss feeds to users, and get the url of its rss feed. In below example, I have used the rss feed url of this blog. Please find the attached file here and save it with .jsp extension (rssTest.jsp)
You can use tag also to read content in a loop.
For a small tutorial on rss taglib, go here

To know what RSS is, go here

,

2 Comments

enum as a class

Here I have compared enum with a class. In java, enums are like classes. What I have done here is, if we have to write a class that works like en enum, how can we do it. Though it is not of practicle use,what I have tried is to make the concept clear about enums.By comparing enum with a class, we can understand how enums work.

Here DayEnum is a enum and DayClass is class for that enum.

enum DayEnum {
SUNDAY, MONDAY
}

class DayClass {
String value;
public DayClass (String str)
{
value=str;
}

static DayClass SUNDAY, MONDAY;
static
{
SUNDAY = new DayClass("SUNDAY");
MONDAY = new DayClass("MONDAY");
}
}

public class Test
{
public static void main(String args[])
{
DayClass dayClass = DayClass.SUNDAY;
DayEnum dayEnum = DayEnum.SUNDAY;
System.out.print("dayClass "+dayClass.value+" \n dayEnum "+dayEnum);
}
}

Here SUNDAY, MONDAY are similar to static objects of Day if Day were a class.

We use enum as:
DayEnum day = DayEnum.SUNDAY;

enums are like constants. To simulate that, I have used value in DayClass. So every object of DayClass has an integer value associated with it.

Leave a comment

Java Good Practices

Here is an article on java good practices.Have a look!

1 Comment

Private Fields of Reference Type

Hi, today I stumbled upon a strange problem while wriring some java code.
There is a class with a private field of reference type, say an ArrayList. Now,what is the use of private fields? They are used so that they can not be modified from outside the class like public fields. General practice is to provide getter methods in the class to return the values of private fields.It goes fine with the primitive type variables and Strings(because Strings are immutable). But when the field is of reference type, we can still change its value.Have a look at the code:

import java.util.*;

class Aclass
{

private ArrayList list;
private String s= "My string";

public Aclass()
{
list= new ArrayList();
list.add("Hello");
}

public ArrayList getList()
{
return list;
}

public String getString()
{
return s;
}
}

public class TestPrivate
{

public static void main(String args[])
{
Aclass ob= new a();
ArrayList alist= ob.getList();
System.out.println(l2);
alist.add("Hi");
ArrayList anotherList= ob.getList();
System.out.println(anotherList);

String s2= ob.getString();
System.out.println(s2);
s2="changed";
String s3= ob.getString();
System.out.println(s3);
}
}

When you run this code,the output is:
[Hello]
[Hello, Hi]
My string
My string

You can see that content of list has been changed, but for string, they are not changed.So you can change the list object even if it is private.It is because the getList() method returns the reference to the same list object, so we can modify the list even if it is private.Now if you think that it is some loophole or bug of java,the you are wrong.It is just a simple basic concept of reference type variables.

Now, what should you do to avoid this thing. You have to modify the getList() method so that it returns the clone of list, not the actual list object.Here is the solution:

public ArrayList getList()
{
return (ArrayList) list.clone();
}

clone() will return the clone of object, not the actual list. Return type of clone() is Object, so we have to typecast it to ArrayList. Any modification done from the reference returned by this method will not change the actual one.For more information on clone() method,
go here.

, ,

1 Comment