• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

This is from Tip Smarts Sample Paper  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone can you please let me explain about this:
class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}
If I compile & run this I am getting 100 as output
----
class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;
System.out.println(b);
}
}
If I compile this, I am getting compiler error.
Please explain the difference

------------------
Ch. Vijayalakshmi
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first case variable is declared as final and compiler knows that a final variable may not be modified once it has been assigned a value. Final variables play the same role as constants in C++ and #define'd constants in C.
As the value assigned to byte var. is with in its range and var is declared as final, compiler have no objection.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
in the first case we have initiliased b toa value 100.
so we get the desired output.
in the second case the error is that we are trying to implicitly type cast an integer to a byte..this type of implcitly type casting is not allowed in java.
hope the answer is satisfactory..
sammy
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijayalakshmi,
In the first example, you declared 'i' as 'final' variable with a value of '100'.
Whenever you declare a final variable in Java, the compiler optimizes the bytecode to use the declared value in place of the variable reference (at least, that's the net result). So the compiler changes the code to do something like

'100' is within the correct range for a 'byte' so Assignment Conversion ( see JLS §5.2 ) rules apply and no error is generated.
In your second example, 'i' is not 'final', implying that it's value can change at any point during execution. The compiler does not optimize the bytecode. It sees 'i' as any integer value; you cannot assign any integer to a byte; only those within -128 to 128 so a compile error occurs.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I AM MIGHTY! Especially when I hold this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!