aspose file tools*
The moose likes Beginning Java and the fly likes implicit casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implicit casting" Watch "implicit casting" New topic
Author

implicit casting

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84

here 7 is integer litearl representing int as default type so the compiler
converts this statement as

but when i pass the 7 to a method i am getting error why



"I Love Java Ranch"
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

In java by default all integer are considered as int. Your method takes byte and you are passing int hence it is thowing the error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you have declaration and initialisation on the same line, the compiler will work out that the literal (7) is in the correct range and take that as a byte value. That only works if you use one line.
saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
is there any difficulty for the compiler for matching
argument and parameter by providing casting..or is there any other reason

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

In java, implicit casting is not allowed for method calls and argument
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23


Normally, integer literals are always interpreted as int - the reason that it works in a line where you declare and initialize a byte is because that statement is treated as a special case by the Java compiler.

The reason why Java does not allow this with a method that takes a byte is because it can lead to confusing situations. For example:

To avoid all these potential problems, Java simply does not automatically convert values to a byte.

Note also that the byte declaration and initialization only works when you initialize the byte with a compile-time constant. This, for example, doesn't work:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Campbell Ritchie wrote:If you have declaration and initialisation on the same line, the compiler will work out that the literal (7) is in the correct range and take that as a byte value. That only works if you use one line.

Nope, also for any other assignments:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

And how come that for widening conversion it works.. that's quite confusing.. if the reason of forbidding the narrowing implicit conversion was for the clarity's sake. Why would it work for the widening implicit conversion? I understand that even though passing Integer.MAX_VALUE to a "getFloat(float f)" it will always work so it's not risky.. however it's not coherent with the overall clarity of the implicit conversion. At least this is my humble view on it.


OCAJP 7, OCPJP 7
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Nick Widelec wrote:And how come that for widening conversion it works.. that's quite confusing.

Why shouldn't it? We're talking about numeric conversions here, so it stands to reason that if I can store 1 in a byte, I should be able to convert it to a short, int or long.

What is slightly more mystifying is why Java allows doublefloat conversions without a specific cast; but I'm sure they had their reasons.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

Nick Widelec wrote:And how come that for widening conversion it works..

Because in a widening conversion, there's never a risk that you loose information. The value that's in the narrower type always fits in the wider type.
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 133

Malatesh Karabisti wrote:In java by default all integer are considered as int. Your method takes byte and you are passing int hence it is thowing the error.

Exactly.
You(OP) can pass 7 by using


Are you better than me? Then please show me my mistakes..
Max Kalashnikov
Greenhorn

Joined: Jul 18, 2013
Posts: 1
There is explanation in JLS ( 5.3. Method Invocation Conversion) :

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion (§5.2). The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process (§15.12.2).

Thus, the program:


causes a compile-time error because the integer literals 12 and 2 have type int, so neither method m matches under the rules of (§15.12.2). A language that included implicit narrowing of integer constants would need additional rules to resolve cases like this example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implicit casting