Vishal Gupta<br />------------<br />SCJA
Rafael Angarita.
SCJP 6.
Author of ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- OCPJP Online Training -- Twitter -- How to Ask a Question
SCJP 5.0 SCWCD 5.0
Rafael Angarita wrote:Hello Vishal,
When we are talking about primitives, implicit cast happens when you do a widening conversion. A narrowing conversion NEEDS a explicit cast, and you are trying to do a narrowing conversion without a cast.
Cheers
scjp6-90%
Rafael Angarita.
SCJP 6.
Vishal Gupta<br />------------<br />SCJA
armando fonseca wrote:
Rafael Angarita wrote:Hello Vishal,
When we are talking about primitives, implicit cast happens when you do a widening conversion. A narrowing conversion NEEDS a explicit cast, and you are trying to do a narrowing conversion without a cast.
Cheers
There s a special situation that you don't need explicit casting, here is:
Let say that here is the semantic of a variable: <target type> <variable name> = <source>;
1. When the source is a constant is an int, byte, short, char within in range of the target type.
2. When the target type is either byte, short or char.
- Armando
Welly Tambunan wrote:
armando fonseca wrote:
Rafael Angarita wrote:Hello Vishal,
When we are talking about primitives, implicit cast happens when you do a widening conversion. A narrowing conversion NEEDS a explicit cast, and you are trying to do a narrowing conversion without a cast.
Cheers
There s a special situation that you don't need explicit casting, here is:
Let say that here is the semantic of a variable: <target type> <variable name> = <source>;
1. When the source is a constant is an int, byte, short, char within in range of the target type.
2. When the target type is either byte, short or char.
- Armando
And i think when you return something from the method that's happen too. Isn't it?
Thanks for the link too Vishal.
scjp6-90%
armando fonseca wrote:
Rafael Angarita wrote:Hello Vishal,
When we are talking about primitives, implicit cast happens when you do a widening conversion. A narrowing conversion NEEDS a explicit cast, and you are trying to do a narrowing conversion without a cast.
Cheers
There s a special situation that you don't need explicit casting, here is:
Let say that here is the semantic of a variable: <target type> <variable name> = <source>;
1. When the source is a constant is an int, byte, short, char within in range of the target type.
2. When the target type is either byte, short or char.
- Armando
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
armando fonseca wrote:
Rafael Angarita wrote:Hello Vishal,
When we are talking about primitives, implicit cast happens when you do a widening conversion. A narrowing conversion NEEDS a explicit cast, and you are trying to do a narrowing conversion without a cast.
Cheers
There s a special situation that you don't need explicit casting, here is:
Let say that here is the semantic of a variable: <target type> <variable name> = <source>;
1. When the source is a constant is an int, byte, short, char within in range of the target type.
2. When the target type is either byte, short or char.
- Armando
You are correct about this, but you have to be very careful because this only happens during assignment conversion context. Basically, it will work when assigning a variable via explicit assignment like you mentioned, and also when returning a value from a method. But when you pass an argument to a method it doesn't take place (technically the matching of arguments to parameter variables during a method invocation is not an assignment conversion context.)
The JLS has all the details, but basically:
scjp6-90%
All code in my posts, unless a source is explicitly mentioned, is my own.
Leonard Fischer wrote:I'm curious if there's a "reason" or "justification" for why assignment conversions are less restrictive than method conversions. Many of us have seen this 1000 times, but the "WHY" is bugging me. As Ruben points out, the following doesn't compile: final int i3 = 10; method2(i3); It seems strange to me that even the following doesn't compile: method2(10). The compiler should be able to recognize the constant 10!
scjp6-90%
Leonard Fischer wrote:I'm curious if there's a "reason" or "justification" for why assignment conversions are less restrictive than method conversions. Many of us have seen this 1000 times, but the "WHY" is bugging me. As Ruben points out, the following doesn't compile: final int i3 = 10; method2(i3); It seems strange to me that even the following doesn't compile: method2(10). The compiler should be able to recognize the constant 10!
All code in my posts, unless a source is explicitly mentioned, is my own.
Legend has it that if you rub the right tiny ad, a genie comes out.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|