• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implicit casting

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, implicit casting is not allowed for method calls and argument
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:

 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Max Kalashnikov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic