Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit Type Casting

 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java size of both "int" and "float" is 4 byte then during method Overloading "int" is implicitly type casted to float but why "float" is not implicitly type casted to " int "

same question i have with "short" and "char"

size of both short and char is 2 byte in java then like int and float(whoose size is same --4 byte ) why char and short are not implicilty type casted into each other.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cast from a float to an int you're potentially losing information. It doesn't matter how many bytes are used: can you represent 2.5 as an int? Whenever you're losing information like this, the compiler requires you to use an explicit cast to confirm you don't mind.

Similarly, while short and char have the same number of bytes, they don't have the same range of possible values - chars are unsigned.
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or in other words, implicit casting is determined by the range of the type (not counting infinity for float and doubles), and not determined by the size, in bytes, of the primitive type.

Henry
 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understood about int and float but

when i write

byte b1=123;

then here type of 123 is int, and it is implicitly casted into byte.
but byte and int don't have same range. the range of byte is only upto 127, so if i write 128 instead of 123 then it will show error.

Q1) so, my question is that if the value of int that comes under the range of byte can be assigned to byte, then why the value of "char" that comes under the range of "short" is not assingned to short.


Q2) in the above when i write byte b1=123; it gives no error. but when i pass integer values (10 and 5) which is under the range of byte , as argument to the method show() then it show error that the method show(byte a,byte b) is not applicable for argument (int,int) ------ why ?

bellow is the code of Method Overloading.

public class Overload {
public static void main(String[] args) {

OverloadManager obj=new OverloadManager();

obj.show(10,5);

}
}


class OverloadManager{

void show(byte a,byte b){
System.out.println("byte a,byte b");
}


}
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:
when i write

byte b1=123;

then here type of 123 is int, and it is implicitly casted into byte.
but byte and int don't have same range. the range of byte is only upto 127, so if i write 128 instead of 123 then it will show error.

Q1) so, my question is that if the value of int that comes under the range of byte can be assigned to byte, then why the value of "char" that comes under the range of "short" is not assingned to short.


Unfortunately, this is a bad example, as you are mixing concepts. Basically....

Implicit casting also has special rules regarding "compile time constants". It may be best to ignore it for now -- as it will just confuse you. An int primitive can't be implicitly cast to a byte, just like a char primitive can't be cast to a short. Period. It is the same as both are not allowed...... but when compile time constants rules are involved, then both combinations are allowed. Again, it is the same, and this time, both are allowed.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:
Q2) in the above when i write byte b1=123; it gives no error. but when i pass integer values (10 and 5) which is under the range of byte , as argument to the method show() then it show error that the method show(byte a,byte b) is not applicable for argument (int,int) ------ why ?


Again, this is more related to compile time constant rules, than just implicit casting. The answer is simple. The Java Language Specifications have compile time constant rules specific to assignment, but doesn't have those rules for method parameters. I would guess the reason is because it is not possible to determine whether a method will only be called with constants, and that can be determined at compile time, when the method is compiled.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic