aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Plz explain me the answer of this code........ 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 "Plz explain me the answer of this code........" Watch "Plz explain me the answer of this code........" New topic
Author

Plz explain me the answer of this code........

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
Q. what will happen when following code is executed ??

class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;
System.out.println(b);
}
}


ans.- compilation error.
(as 100 falls in the range of byte)
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Well type checks happened on the variable type, and not on the values. So if you try to assign any int variable value to byte variable, it won�t compile.

You need to change your code as below to compile the code

byte b = (bype)i;.


My blood is tested +ve for Java.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

check code above you will get your answer.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
Thanks a lot gowher.....i got it.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks chetan..
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023


The i's value must be within the range of byte (-128 to 127), otherwise it is generating a compiler error.
[ August 16, 2006: Message edited by: wise owen ]
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Unbelievable!!!

Compiles fine:



Doesn't compile: (!!!)




Doesn't compile: (!!!)



Why???


SCJP 5.0 - 95%<br />Preparing for SCWCD
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
any ideas?
Rafal Grzybacz
Greenhorn

Joined: Aug 19, 2006
Posts: 6
Interesting, I would expect a compile error: possible loss of precision for the first block of code.

The code compiles when we have:

final short i = 100;
byte b = i;

AND

final int i = 100;
byte b = i;

Could this be because the byte(8 bit), short(16 bit) and int (32 bit) all fall in the range of a 32-bit register and the long primative type has a 64 bit size and therefore loses 32 bits when assigned to a byte or an int?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From the Java Language Specification:
In addition, if the expression is a constant expression (�15.28) 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 represent-
able in the type of the variable.
FT


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Any ideas on why long is treated differently here? (I find it helpful to know "why" when learning language rules, and this one seems a little arbitrary on first glance.)


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
I found This post, which gives a reasonable justification for treating long differently.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
As per my view towards the implicit narrowing is that.... the reason for allowing the implicit narrowing for:

// code:
final int i=100;
byte b=i;

is that literals can never be byte,short or char type so whenever there is any need to asign a constant value to a variable of type byte,short or char ,java tells why should we bother about converting the int literal (in above case, 100) explicitly to byte before assigning it to variable b.... because in any case its not possible to assign 100 as a byte to variable b.
But in case of following codes:

//code:
final long l=100;
byte b=i;

// code:
final long l=100;
int i=l;

....java compiler tells that we should cast it explicitly to remove ambiguity or any confusion.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Plz explain me the answer of this code........