This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Primitive assignment

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}


According to me this code will not compile.can t assign int to a byte.
But the mock exam says it will & print 100..
Who is right here?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidhya,

On my machine (Windows XP, JDK 1.5.0_06), this compiles cleanly and prints "100".

Hope that helps.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the variable is declared final, its value is known at compile time and since the value can fit into a byte, the compiler allows it.

Note that if you modify it like the following, it won't compile.

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

Can you please explain the reason why won't it will compile ?

Cheers,
-Biswa
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biswamohan,
In the first code, the value of i was a constant known at compile time. In the second code, the value of i is final, but it's not initialized when it is declared. The compiler will not perform the assignment so the value of i is not a constant known at compile time.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just in addition to Keith's post the same rule is also valid when you use anonymous inner classes.

For example.


Even though there's a valid reference for MyClass declared within main, the compiler is not able to compile your code just because your anonymous inner class doesn't know anything about this reference.

But, what could happen if you declare your MyClass reference as final ?


Now the compiler is happy just because myClass contents is known at compile time due to final keyword.

Hope that helps.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just in addition to Keith's post the same rule is also valid when you use anonymous inner classes.

For example.


Even though there's a valid reference for MyClass declared within main, the compiler is not able to compile your code just because your anonymous inner class doesn't know anything about this reference.

But, what could happen if you declare your MyClass reference as final ?


Now the compiler is happy just because myClass contents is known at compile time due to final keyword.

Hope that helps.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!