sannuth kashikar

Greenhorn
+ Follow
since Sep 16, 2008
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 sannuth kashikar

What is the use of java.lang.Process class.How useful is this ?
15 years ago
Hi ,

What is the difference between process and thread in java.How to have different processess in a java code.



Thank you.


15 years ago
Hi,
Its important to know where wildcard with extends or super to be used.
Wildcard with extends
code:
interface Collection<E> {
...
public boolean addAll(Collection<? extends E> c);
...
}

and
// # 1
List<Number> nums = new ArrayList<Number>();
List<Integer> ints = Arrays.asList(1, 2);
List<Double> dbls = Arrays.asList(2.78, 3.14);
nums.addAll(ints);
nums.addAll(dbls);

The first call is permitted because nums has type List<Number>, which is a subtype of Collection<Number>, and ints has type List<Integer>, which is a subtype of Collection<? extends Number>. The second call is similarly permitted. In both calls, E is taken to be Number.So we use ? extends E

We can also use wildcards when declaring variables. #1 changed by adding a wildcard to the second line:

List<Integer> ints = Arrays.asList(1,2);
List<? extends Number> nums = ints;// ok
nums.add(3.14); //compile-time error because you cannot add
//a double to List<? extends Number>, since
//it might be a list of some other subtype of number
assert ints.toString().equals("[1, 2, 3.14]"); //here is why compile-time error

If a structure contains elements with a type of the form ? extends E, we can get elements out of the structure, but we cannot put elements into the structure.

Wildcards with super

Here is a method that copies into a destination list all of the elements from a source list, from the convenience class Collections:

public static <T> void copy(List<? super T> dst, List<? extends T> src) {
for (int i = 0; i < src.size(); i++) {
dst.set(i, src.get(i));
}
}

? super T means that the destination list may have elements of any type that is a supertype of T, just as the source list may have elements of any type that is a subtype of T.

Here is a sample call.

List<Object> objs = Arrays.<Object>asList(2, 3.14, "four");
List<Integer> ints = Arrays.asList(5, 6);
Collections.copy(objs, ints);
assert objs.toString().equals("[5, 6, four]");

As with any generic method, the type parameter may be inferred or may be given explicitly. In this case, there are four possible choices, all of which type-check and all of which have the same effect:

Collections.copy(objs, ints);
Collections.<Object>copy(objs, ints);
Collections.<Number>copy(objs, ints);
Collections.<Integer>copy(objs, ints);



The first call leaves the type parameter implicit; it is taken to be Integer, since that is the most specific choice that works. In the third line, the type parameter T is taken to be Number. The call is permitted because objs has type List<Object>, which is a subtype of List<? super Number> (since Object is a supertype of Number, as required by the super) and ints has type List<Integer>, which is a subtype of List<? extends Number> (since Integer is a subtype of Number, as required by the extends wildcard).

We could also declare the method with several possible signatures.

public static <T> void copy(List<T> dst, List<T> src)
public static <T> void copy(List<T> dst, List<? extends T> src)
public static <T> void copy(List<? super T> dst, List<T> src)
public static <T> void copy(List<? super T> dst, List<? extends T> src)

The first of these is too restrictive, as it only permits calls when the destination and source have exactly the same type. The remaining three are equivalent for calls that use implicit type parameters, but differ for explicit type parameters. For the example calls above, the second signature works only when the type parameter is Object, the third signature works only when the type parameter is Integer, and the last signature works (as we have seen) for all three type parametersi.e., Object, Number, and Integer. Always use wildcards where you can in a signature, since this permits the widest range of calls.

Use an extends wildcard when you only get values out of a structure, use a super wildcard when you only put values into a structure, and don't use a wildcard when you both get and put.

I think this may help you.
[ October 04, 2008: Message edited by: sannuth kashikar ]
Assertions are used during testing.These statements are not executed during normal execution of program, as assert are disabled by default.Using assert in switch statement means that as per assumptions your are not suppose to reach the default.assertions are used to check your assumptions or the logic you have applied to your code.They are executed when they are enabled.
How important is this method to be used in an application.Is this method used for security purpose or to verify information exchange between to machines.
It would be helpfull if explained with a small example.
Thanks
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)

Sets an application's URLStreamHandlerFactory. This method can be called at most once in a given Java Virtual Machine.

Does this mean that,this method cannot be used with same JVM for different applications.Please explain about this method,i am not able to understand usage to this method.
thanks in avance.
hi,
you will know that in exam.

about the question

wait() method "cannot" be called from non-synchronized context...

wait() method throws runtime exception IllegalMonitorStateException if the current thread is not the owner of the object's monitor.Runtime exceptions are due to logical errors,you have to go back to the code and work on logic to convince JVM .
So "cannot" would be more appropriate.
hi,

Option B is same as option A, since the argument which is passed to the method will be of type T.

import java.util.*;

public class BackLister
{
// INSERT STATEMENT HERE
//A.public static <T> List<T> backwards(List<T>input)
public static <T> List<T> backwards(List<? extends T>input)
{
List<T> output=new LinkedList<T>();
for(T t:input)
output.add(0,t);

return output;
}

public static void main(String[] args) {
List<String> lst1=new ArraryList<String>( ); //#1
List<Object> lst2=new ArraryList<Object>( ); //#2

//more codes

System.out.println(backwards(lst1));
System.out.println(backwards(lst2));
}
}
when 1 and 2 passed to the method , T would either be of type String or of type Object.So option B is correct.

If we consider 2 parameters
public static <T> List<T> backwards(List<T>input,List<T> input1)
{ .... }

and call to backwads(lst1,lst2); would produce compiler error because T should be of same type.

? extends T denotes an unknown type that is subtype of T

public static <T> List<T> backwards(List<? extends T>input,List<T> input1)
{ .... }
call to backwads(lst1,lst2); would be successful because String is a subtype of Object.

? super T denotes a unkown that is supertype of T.In option C the supertype of T cannot be assigned to T in the for loop hence fails to compile.

option D,E returns subtype and supetype hence valid
[ September 19, 2008: Message edited by: sannuth kashikar ]
hi,

@Pranav Bhatt

public class Test extends Thread {
private static String str = "Initialised";

public static void main(String[] args) {
Test myTest = new Test();
myTest.myFunction(str); //calls myFunction()
System.out.println(str); // "" str value is "Initialized" only as the changes in below methods won't change the original str "" as in Java its Pass by value(copy is passed) not pass by reference
}

private void myFunction(String str) {
str = "Did this in myFunction()";
start(); // calls run()
//System.out.println(str);
}

public void run() {
str = "Did this in run()";
}
}

str in the run method is a static variable which is declared in 2nd statement and when run method runs str will refer to " Did this in run()" object ,since it is a class variable.
uncomment println statement and run 4 to 5 times and will get different outputs.Its upto scheduler which will be executed first,main or run.Hence "Initialised" is not a guaranteed output.
[ September 19, 2008: Message edited by: sannuth kashikar ]
Remove PkgAccess from 3rd line only tiger as 4th statement and add com. as in 5th statement will compile.
static import is used to import static members of a class.

import static com.sun.PkgAccess.*; // import static members of class PkgAccess
public class PkgAccess2 {
int x1 = PkgAccess.tiger; //is a different package which
int x2 = tiger; //ok since your have imported in 1st statement
int x3 = com.sun.PkgAccess.tiger; //ok , full package name
int x4 = sun.PkgAccess.tiger; // invalid ,not a complete package name
}
All the 4 method calls will fail since the compiler cannot determine which version of the method to be called, to box or unbox the arguments passed to it. Integer is object version of int.The operations performed on int can also performed on Integers.
so,
inst_test.method (1, 1, 1); // to box or is of type int

inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
// to unbox or is of type Integer

inst_test.method ( 1 , new Integer(5) );//wheather to box or unbox

inst_test.method ( new Integer(10) , 1 );//wheather to box or unbox

Hence compiler error.
I will be taking scjp 1.5 on tuesday.Try Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (CX-310-055) Free Proficiency Assessment System.
U can also try e-practice 10 questions from sun which is free for all versions.
WGS-PREX-10-QUEST

Java.Inquisition
Exam Simulator
and
faq.
These may help you.I feel confident after taking these Practice tests. All the Best for the Exam.
[ September 16, 2008: Message edited by: ssk ]