Sergej Smoljanov wrote:method choose in invocation:
1. One to one (byte to byte) if there is no:
2. Widening: byte to short to int ... if no:
3. Boxing: byte to Byte if no
3. to Number if no:
4. to Object if no:
5. var args method.
and if you pass null method that accept object is chosen (most specific)
if there is equal most specific method - compiler error because ambiguous.
Note: Text content in the code blocks is automatically word-wrapped
in this case when you invoke meth (Byte b), bb and b - refer to same object,
but when you asign b = 100; at //3 you create new object and assign it to variable b, at this point b and bb refer to different object. to understand this
Sergej Smoljanov wrote:and one more about wrappers.
Byte b =10;
meth(b);
first:
1 to 1 chosen (void meth(Byte){}) of no
not used method with parameter other wraper.
to Number if no
to Object if no
unboxing to byte if no
widening:
to short
to int
to long
to float
to double
after unboxing never boxing
last - var args
Roel De Nijs wrote:
Now it's time for a little pop quiz. Does the following code compiles or not?
Prathima gaitonde wrote:
Roel De Nijs wrote:
Now it's time for a little pop quiz. Does the following code compiles or not?
At fist glance, I though it will compile but it will not. As byte range is -127 to 128, value 200 is not big enough to fit into byte. Am I right till here?
Prathima gaitonde wrote:
Sergej Smoljanov wrote:method choose in invocation:
1. One to one (byte to byte) if there is no:
2. Widening: byte to short to int ... if no:
3. Boxing: byte to Byte if no
3. to Number if no:
4. to Object if no:
5. var args method.
Can you(or anybody) please explain me what exactly these statements mean? If possible with bit of java code please.
Prathima gaitonde wrote:Finally I dint get following post either.
Sergej Smoljanov wrote:and one more about wrappers.
Byte b =10;
meth(b);
first:
1 to 1 chosen (void meth(Byte){}) of no
not used method with parameter other wraper.
to Number if no
to Object if no
unboxing to byte if no
widening:
to short
to int
to long
to float
to double
after unboxing never boxing
last - var args
Prathima gaitonde wrote:
and if you pass null method that accept object is chosen (most specific)
if there is equal most specific method - compiler error because ambiguous.
What does this mean?
Prathima gaitonde wrote:I take in line 2 you mean invocation of method meth(bb), instead of void meth(Byte bb) is that right?
in this case when you invoke meth (Byte b), bb and b - refer to same object,
but when you asign b = 100; at //3 you create new object and assign it to variable b, at this point b and bb refer to different object. to understand this
Roel De Nijs wrote:
Here are a few examples. All these examples will compile successfully. It's up to you to determine which method will be invoked (before confirming your answer by compiling and running the examples).
reference to meth is ambiguous, both method meth(byte...) in Test and method meth(Byte...) in Test match
Prathima gaitonde wrote:
Roel De Nijs wrote:
I found this example gives compile time error :method call is ambiguous. Sorry if I am wrong.
My understanding on this post, please correct me if I am wrong.
1>As long as parameters of the method, are from the same hierarchy list there is no ambiguity(compile time error).
Prathima gaitonde wrote:2>Upon Invocation, That method which takes, the parameter, of type most subclass will be called. Provided, method call is such that argument fits into all the methods.
Example:
Here, out put of the program is fnfexception:null. If we uncomment the last method then, its a compile time error.
Prathima gaitonde wrote:Out put of Example A is: string: null
Out put of Example b is: double: null
Prathima gaitonde wrote:3> If we change the method call to call(10), then:
Out put of Example A is: object: 10
Out put of Example b is: object: 10
Example C is: Compile time error. Overloaded call is not applicable for the argument(int) .
Roel De Nijs wrote:
Out put of Example A is: object: 10
Out put of Example b is: object: 10
Example C is: Compile time error. Overloaded call is not applicable for the argument(int) .
Bang on! Can you explain why object: 10 is printed in ExampleB and not double: 10? Why is 10 not a Double (with upper case d) as it easily fits into a double (with lower case d)? (note: I'm just playing to be confused, I know the answer, just want to check if you know it too )
And finally another little pop-quiz. What's the result of this code (which looks almost the same as ExampleC)?
Prathima gaitonde wrote:Here, int constant gets promoted to Integer Object, which cant be fit into either String or Double, but Object can take it as, its a superclass of Integer.
Prathima gaitonde wrote:Answer is string:null, as method get, returns string object with value null.
Prathima gaitonde wrote:4. If the method call is with parameter null then, the object argument should be of different hierarchy level, otherwise compile time error.
5. If the methods argument differ in hierarchy level then, method that have a argument of type lower order gets called.
Prathima gaitonde wrote:6. Even in case of methods with primitive argument, that method which has the, least enough space to take the parameter is called.
Roel De Nijs wrote:
Prathima gaitonde wrote:
6. Even in case of methods with primitive argument, that method which has the, least enough space to take the parameter is called.
I don't understand what you mean with this statement. Can you rephrase it or give a code example?
Prathima gaitonde wrote:
Its really nice to see your post, you have great patience to explain the concept to me. With your and Roel's help I am stating my views about overloaded methods, both of you please correct me if I am wrong or need to know something else, such that it will help, even others.
Prathima gaitonde wrote:The out put of the code is Short, as int is assigned to byte it may be obvious that, one thinks the output of the code will be Int. Also int and short both can take byte, so this point was constructed by me to make it easy to remember.
Henry Wong wrote:Phase three requires an understanding of varargs, generics, etc., and is rife for confusion. Regardless, all three phases also requires an understanding of the concept of most specific method ...
Sergej Smoljanov wrote:....
may be some thing i forgotten when i prepare i see table about casting wrapers to primitive and primitive to wrapers, sorry don`t remember in which source.
Roel De Nijs wrote:
And a reply of me would not be complete if I didn't have a few pop quiz questions So here we go:
1/ can a long be widened to a float?
2/ can a byte be widened to a char?
3/ can a char be widened to a short?
4/ can a char be widened to a float?
Prathima gaitonde wrote:1>Long can fit only in double
Prathima gaitonde wrote:2> char can fit only in int, float, long, double.
Prathima gaitonde wrote:3>Can byte be widened? Is it not the data type in java with small range, than any other data types? Yes, it is true that byte cant fit into char.
Roel De Nijs wrote:
Spot-on! Although you made a little slip-up in your explanation: As byte range is -127 to 128, value 200 is too big to fit into byte
Roel De Nijs wrote:
And a reply of me would not be complete if I didn't have a few pop quiz questions So here we go:
1/ can a long be widened to a float?
2/ can a byte be widened to a char?
3/ can a char be widened to a short?
4/ can a char be widened to a float?
Prathima gaitonde wrote:
1>Long can fit only in double
Sharmili Rameshbabu wrote:
Roel De Nijs wrote:
Spot-on! Although you made a little slip-up in your explanation: As byte range is -127 to 128, value 200 is too big to fit into byte
Isnt the range of byte -128 to 127??
Sharmili Rameshbabu wrote:output is float. So it means long is widened to float. Is it not?
A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
Roel De Nijs wrote:
Still for me it's a little bit weird that long fits float, because long is a 64-bit data type and float is just 32-bit. This little code snippet shows what happens when you assign the maximum value of long to a float:
Output: 9223372036854776000,000000 9223372036854775807
So you clearly have precision loss. But you do not need a cast to assign a long to a float, so clearly long can be widened to float.
Henry Wong wrote:You can also lose precision going from a long to a double too. A double only allows 52 bits for the mantissa, so it is possible to choose a long value that can't be converted to a double without loss of precision.