File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit Type Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit Type Casting " Watch "Implicit Type Casting " New topic
Author

Implicit Type Casting

abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
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

Joined: Apr 06, 2010
Posts: 4462
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit Type Casting