Win a copy of Head First Android this week in the Android forum!

Madhukar Ojha

Ranch Hand
+ Follow
since Mar 21, 2007
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Madhukar Ojha

Hello Ranchers

I am new to log4j , trying to use it in my web app.
I executed sample program in desktop app. successfully.

Now i need to implement it in my web app.
I want to send my log messages to log file of my web app instead of server log file.

Please send me study material and examples.
When you call m1

Both methods argument can hold 'null'

but compiler chooses most precise if all of them are in same hirerchy

that is m1(String o)

because String extends Object.

But...... If you have a class MangoShake

Hi all Ranchers

Yesterday I passed SCJP 5 with 93%.
I would like to thank all dear ranch friends for their support.

I would like to share my experience about this exam.

1. I read thoroughly K&B and Java 2 by Ivor Horton (Wrox publication).
2. I practiced with Enthuware question bank and Whizlabs.

I think that these 2 books mentioned above give you good foundation of
the topics. Question bank provided by Enthuware and Whizlabs make concepts solid to score high. I am fully satisfied with materials provided
by them.
13 years ago

Originally posted by Ramesh Sahu:
class StringTest {

public static void main(String args[]) throws IOException{
System.out.println("s12"==((" s12 ".trim())));
System.out.println( "String" == "String".replace('t','t') );


You should always remember that
Besides String literals and compile time constant expressions
all String objects are created at run-time same as other objects.

When you say " s12 ".trim() then " s12 " object goes String pool
whereas string created on " s12 ".trim() invocation is created at
run-time hence distinct.

Hence "12"==((" s12 ".trim()))
returns false

When you invoke replace() , it returns same object reference if both
arguments are same.

Since both arguments are same hence replace method returns same reference
hence it returns true.
Congratulation Sa..................agar
13 years ago
Hi Rancher,

public static <E extends Number> List<? super E> process(List<E> nums)

1. parmeter must be of type List and its <type> must be of type Number
or sub type of Number.

2. Return type must be of type List or its sub type and its <type>
must be of type Number or its super type.(Object in this case.)

I hope this will help you.
Hi Rancher ,

add is valid for events like MouseEvent.


[ July 18, 2008: Message edited by: Madhukar Ojha ]
Hi Milan,

Compilation will fail at

Because horse is instantiated with wild-card ?.
? reference gives you view mode.

You can't add any stuff using this reference.
Hi Rancher ,

with constructors you can use private.
It is used for generally Singleton classes. This class should have
a static class which return the object of that class.

<pre name="code" class="core">
class Vict{
private static Vict v = new Vict();

private Vict(){

public static Vict getInstance(){
return v ;

class VictTest{
public static void main(String ... args){
Vict objVict = Vict.getInstance();

It is one use to declare constructor private.
I hope this will help you.
Hi Faber ,

You have to make only one change if you are running this program
on Windows.

<pre name="code" class="core">
Pattern p = Pattern.compile("\\d");
Hi Rancher ,
Only instance methods can be overridden and nothing else.

When you eclare static int i in class,
you are just redefining i .
It is also called shaodwing.
If you use i without any prefix , you will get i defined in your class.

Now if you want to use i defined in interface then
you have to write

Hi Rancher

Because referenc type of oa is Type Object[] .
Hence return type of object is of type is Object.

You are trying to hold the reference type of super class in sub type .

You can o like this

I hope it will help you.
Hi Faber

It seems that you have a lot of doubts in generics.
Please go through k&B Head First Java and SCJP 5 exam guide.

When you use
void show(SomeType<? extends SomeClassOrInterface> a)
you can not put any data in the object referenced by a .


When you use
void show(SomeType<? super SomeClassOrInterface> a)
you can put any object which is of type SomeClassOrInterface or its
sub ype in the object referenced by a.

I hope this will help you.
Hi Rajiv

Great Score

Well Done................
13 years ago
Hi Faber ,

You should remember that there is only one object on heap which is referred by three different type of references , 2 of them are type safe and one is of raw type.
One more thing you should keep in mind when you mix up type safe and raw type it is ok at compile type. But at runtime autual object matters. Hence it can throw Exception.

after line 45 b refers to bO, then after line 46, we cast b to bA. The scary thing happens at line 48 when we set the bA's element with an Apple, thus the bO's element is set too.

line 50 throws runtime exc becos we try to case an Apple to an Orange.

Correct me if i'm wrong.[/QB]

Hi Faber ,
Basket b = bO; //line 45
Now you have lost type safety .

Basket<Apple> bA = (Basket<Apple> b ;// 46
Now again you cast raw type b to type safe bA.

bA.setElement(new Apple());// 48
Compiler allows it only because it cares about reference type that is bA.
Since bA is raw type that is why compiler lets you do it.

Try to do it with reference with bo

bo.setElement(new Apple());// error
It is compile-time error because knows it very well that bo is of type Basket<Orange> and you are trying to add wrong thing in type safe collection.

Orange o = bO.getElement();
Now since compiler thinks that element should be Orange because of reference type so it allows it.

But at runtime JVM finds that it is Apple object and you are trying to assign it to different type Orange , that is why it is upset and throws Exception.