Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-13-2008, 08:11 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
int to byte
I found something strange. Please review the code below:

Code:
final int i = 100; byte b = i; System.out.println(b);
Output:
Code:
100
Removing the final keywords, causes exception.
Code:
int i = 100; byte b = i; System.out.println(b);
Output:
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to byte
What is the reason? What role final keywords plays here?

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-13-2008, 08:22 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
This is because once you marked a variable as final, it is now immutable, cannot be changed after the declaration. compiler is intelligent enough to make the decision by allowing it to be set to a byte variable if the value is in range.
the other case, compiler cannot guarantee if there will not be a change on the int before it is assigned to the byte so it does not allow the setting it. Otherwise this code would create a runtime error:

int i = 100;
... lots of code here and i may be altered and set to 10000...
byte b=i; <-- error!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Byte Values javaplus New To Java 1 06-23-2008 01:08 AM
Byte Array sandor New To Java 4 06-20-2008 10:40 PM
using Byte arrays mew New To Java 2 01-30-2008 04:54 AM
BufferedImage to Byte Java Tip Java Tips 0 01-22-2008 09:17 PM
Byte Streams JavaForums Java Blogs 0 12-26-2007 03:01 PM


All times are GMT +3. The time now is 02:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org