Sonali Sehgal

Ranch Hand
+ Follow
since Jul 09, 2009
Merit badge: grant badges
For More
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 Sonali Sehgal

Hi Ankur,

javac -d .../ankur
This command of .../ does not mean anything

It would itself on the command prompt gives you an error that directory not found

if you would want to explore more options use javac -help
Hi Ankur ,
back to your point at the very first program...............I have explained it again..this is the second solution to the other scenario..

line 2 will not compile but at runtime it will give Class cast exception.
line 2 will not compile only because we are doing a forceful personification.
Compiler also reports an error where it says
inconvertible types
found: Car
required :Vehicle
Vehicle v1=(Vehicle)car;//2

1 error

In the above line it is an error as car instance of Vehicle returns false and line 2 is giving a compile time error
the compiler also says found car but required Vehicle which means Vehicle v1=(Vehicle) new Vehicle();
This means this is what you can do in your program and this is the only right one.
That means you can only make a handle of Vehicle and create an instance of it.

For the line 1 you cannot execute it as Car cannot be casted to Move by anyway and this is what the exception exactly for.

Hi Ankur and Rohan,
I have again mentioned the above casting explanation and to let you know more about casting it is as follows:-

Moving up in the inheritance hierarchy it is called upcasting
Car c=(Vehicle) new Nano(); //Compile Time Error
if you do
Car c=(Car) new Vehicle();

It will throw Class cast exception.
Car c1=(Car)v1;// This is called Downcasting.
//With the help of downcasting you can invoke any method of Car class

Compiler can determine the cast is correct or not by only one way
say in the above program the handle car can only be instanceof Move and Vehicle..
you can run this program by the following test.

the output of the above will be true true.

Let me be more specific the car is instance of Move and Vehicle only if the public class car extends Vehicle and implements Move
otherwise at runtime it will throw an class cast exception..only incase if you do not extend Vehicle and implement Move as incase of code 1 and line 2 as
Vehicle v1=(Vehicle)car;//2

Syntax of casting upcasting or downcasting is
Destinationtype handle=(destination type) handle/instance;
As you mentioned in line 2 Vehicle v1= (Vehicle) car and you are not extending Vehicle this is forceful personification and it will throw a class cast exception.
I am looking for a tutorial that can help me find out everything about factory classes .As far as meaning is concerned it is Classes that follow the factory method pattern
Whether this topic can be coverered in SCJP or not I am not much sure about it?
As far as I am concerned I am preparing for SCJP 1.6
Can someone give me tutorials in Java Factory classes in Java 1.6

This System.out gives you an error because it is not the correct forward referencing to correct this you have to use this.i so it specifically points to the reference variable and gives the output.

Well, in the case of forward referencing, we're not trying to read from i, we're assigning a value to it. Therefore, there's no "dangerous" operation occurring here and the compiler allows it. For more information check this link of java ranch I hope it will make it more clear...

Webpage for Forward Referencing

Hi Ankur ,

What I meant to say there is nothing as C in this program......

This is a program of Outer class and Inner class.In case of Inner class it can access private,protected ,default and public or any access modifier variable in inner class and declaring int y can take in that particular x variable of the outer class.

In this code during the execution of the program first the compiler declares and intializes the variable and then after that the instance block executes.

In the above code the variable i is not even declared whether it is an integer ,float or what it is whereas on the first code we are declaring it.Hence it will give your a compile time error of identifier expected.
Hi Ankur,
Please be more specific in your question there is not interfaces in this program and no variable or class name C.Please make your question more clear.

You get an compile time error at line 2 and not at line 1.This is right.

At line 1 the interface object it gets executed successfully because Car is an

instance of Move Interface which returns true and hence the handle of the interface

can point to Car.

But at line 2 the object car is not an instance of the Vehicle neither it implements

Interface neither Vehicle is the super class of the car. In interface the concept is when you create an interface you are defining the contract for what a class can do .Hence it gives a compile time error of incovertible types .In this case this error can be fixed only byt adding the line to public class car extends vehicle implements move.Then your program is right.
Hi Punit,

The methods of interface are always public and abstract if not mentioned they are taken implicitly by the compiler if we put any other access modifier like final ,static private or protected then the program will not compile.
I do understand the explanation Kathy Seirra book gives me:-

It is imporant for you to understand what binarySearch(...) method returns:

public static int binarySearch(Object[] a, Object key)
Searches the specified array for the specified object using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements (as by Sort(Object[]), above) prior to making this call. If it is not sorted, the results are undefined. (If the array contains elements that are not mutually comparable (for example,strings and integers), it cannot be sorted according to the natural order of its elements, hence results are undefined.) If the array contains multiple elements equal to the specified object, there is no guarantee which one will be found.

a - the array to be searched.
key - the value to be searched for.
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

ClassCastException - if the search key in not comparable to the elements of the array.

I also understand this:-

If its unsuccessful, it will return the index where the searched object can be inserted so that the list will remain sorted.
So, if the searched object is to be inserted at 2nd position -3 will be returned.
In the above example, the search is unsuccessful and the new String object is to be inserted at the 0th position(spaces come before alphabets).
So you get -1 as the output. Every time, a binary search will return you an int between -(n+1) to n-1 where n is the length.

My question is how does the calculation as comes in the output in the last 2 lines is how does it calculate.I am not able to get a clear message of that. Because in both sorting and reverse sort "one" is already in the array.

Hi Henry,

I could not understand how does the program calculate the line 4 and line of code 5 and gives the output.I mean I want to know how does the calculation goes about in the program.I could understand some bit of it but not all(as to how does -1 and 2 comes as the output??).

You are stating one not found but one is already there in the array??

four one three two
one =1
now reverse sort
two three one four
one =-1

one = 2

This is a kathy seirra question.Can some please help me understand the last 2 statements of output one=-1 and one=2 how does that come as the output and under what calculation please help me understanding the same and I could not understand the output of line code 4 and 5??