• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implicit cast of primitives

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This makes me really angry:


Line 1,2,4 are ok. And line 3 is not.
Why is there no implicit cast in line 3? Can anybody explain?
Is there another situation where the implicit cast is NOT done?

Gru�
J�rg
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you pass data as an argument you must explicitly cast it to the type you want, if it will not be implicitly casted into an int. For example this will not compile...

Short s = new Short(1);

But this will...

Short s = new Short((short)1);

So watch out when you invoke functions/constructors. The default data type is int.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As noted above, assignment conversion and method invocation conversion are different things. For the gritty details, see...
JLS 5.2 Assignment Conversion
JLS 5.3 Method Invocation Conversion

In particular, note the following from 5.3:

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).

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by J�rg Waldmann:
... Is there another situation where the implicit cast is NOT done? ...


Yes.

Basically, if a variable is final with its value known at compile time, then it can be assigned to a narrower primitive type if it fits within range. This implies that if the variable is not final and/or its value is not known at compile time and/or it does not fit within range, then the conversion ("implicit cast") will not work.


Or in the language of the JLS...

"...if the expression is a constant expression ... of type byte, short, char or int ... a narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable." (Ref: 5.2.)

So what is a "constant expression" in this case?

"A compile-time constant expression is an expression denoting a value of primitive type ... that ... is composed using ... literals of primitive type ... [or] simple names that refer to constant variables..." (Ref: 15.28.)

And what is a "constant variable"?

"We call a variable ... of primitive type ... that is final and initialized with a compile-time constant expression ... a constant variable." (Ref: 4.12.4.)


Note: In quoting the JLS, I omitted parts (...) in order to focus only on primitive assignment conversions. In particular, I highlighted only two of several bulleted items that constitute a "constant expression."
[ April 15, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was going through the post. I couldn't understnad why there is an explicit cast needed ?

The method is defined as setB(byte b) and

the method is called as setB(5), where the literal value '5' is within the range of byte. So why explicit casting is needed ?

I don't know if I am missing something ?

Cheers,
-Biswa
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Biswamohan Routray:
... The method is defined as setB(byte b) and the method is called as setB(5), where the literal value '5' is within the range of byte. So why explicit casting is needed ? ...


Again, assignment conversion is different than method invocation conversion.

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion.

 
reply
    Bookmark Topic Watch Topic
  • New Topic