Masood Alam

Greenhorn
+ Follow
since Jul 12, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Masood Alam

Greetings,

In the below code //Line 1 results in "true" and the //Line results in "false"

Could someone please explain... its seems to be similar to "String Pool"

/**************************CODE*************************************/

class TestIntegers{

public static void main(String[] args){

Byte b1= 5;
Byte b2 = 5;
Byte b3 = new Byte("10");
Byte b4 = new Byte("10");
System.out.println("First Style " + (b1 == b2)); //Line 1
System.out.println("Second Style " + (b3 == b4)); // Line 2

}

}
Hey if you are new to programming as such... then
Take a look at :
1)Introduction to Java programming: comprehensive version/Y. Daniel Liang.--6 th ed.

2)And of course Kathy Sierra
Thanks it works...

However I do still have the same doubt, as per the documentation

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

Does this mean we cannot change the list..then how will it change the array as per the line "(Changes to the returned list "write through" to the array.)"
The below code compiles fine, however I am getting runtime exception
at Line 12...

Iam trying to insert an element of type Integer into a List<Integer>

Why is an "UnsupportedOperationException" being generated...

/********************CODE********************************************/
import java.util.*;

public class TestAsList{

public static void main(String[] args){

Integer[] arr =new Integer[] {12,34,56,78,98,23};


List<Integer> aList = Arrays.asList(arr);

aList.add(new Integer(1232)); //Line 12

System.out.println("Printing aList :" + aList);

}

}

//*********************output***************************************/

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at TestAsList.main(TestAsList.java:12)
Here is another program, which is confusing...

At compile time c1(reference type) will see its method getObject which returns type A, however according to overriding at runtime the binding will happen with the object on right side ( new subcov() //Line 1) and it has the method getObject which returns type B

However when I run the code the output is "5"... //Line 2

Please explain....
/***************************CODE**************************************/
class A{
int x = 5;
}


class B extends A{
int x = 6;
}


class subcov extends cov2{

public B getObject(){
return new B();
}


}


class cov2{

public A getObject(){
return new A();
}

public static void main(String[] args)
{

cov2 c1 = new subcov(); //Line 1
System.out.println(c1.getObject().x); //Line 2


}
}
Hey thanks... it works.

Is there any resource where I can find more info on covariant types...
Hi All,

The following code is not compiling and the message that I am getting is that "incompatible types" ie at Line 1

"String str = se.doStuff(); //////Line 1"

If am right the object reference "se" should be invoking the method in Class "Second"

I now Iam missing something here, please explain

Thanks in advance

/******************* CODE *******************************/
class Second extends First{

public static void main(String[] args){

First se = new Second();

String str = se.doStuff(); //////Line 1

System.out.println("Printing from main :" + str);

}

public String doStuff(){
System.out.println("Derived");
return new String("Derived");

}

}//close class second


class First {

public Object doStuff(){

System.out.println("Base");
return new String("Base");
}//close doStuff

}//close class First