Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:17+00:00 2026-06-15T08:14:17+00:00

byte b = 100 ; compiles without any errors, but int i = 100

  • 0
byte b = 100 ; 

compiles without any errors, but

int i = 100 ; 
byte b = i ; 

throws an error. Why? Even when assigning 100 directly to b, we are assigning an int literal. So why did I get an error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:14:17+00:00Added an answer on June 15, 2026 at 8:14 am
    byte b = 100 ;
    

    Here 100 is a compile time constant. And hence can be assigned to the byte.

    int i = 100 ; 
    // i = i + 100;  // It's allowed to add this statement later on, 
                     // if `i` is not declared final. And hence, the next assignment
                     // will clearly fail at runtime. So, compiler saves you from it
    byte b = i ; 
    

    Now in this case, since i is not declared final, so it is no more a compile time constant. And in that case, you can later on come and modify the value of i, in between the initialization of i, and assignment of i to byte, as in the above case. Which will certainly fail.
    That is why compiler does not allow the assignment of i to byte type.

    But, you can use an explicit casting for it to compile, which may of course crash at runtime. By doing an explicit cast, you tell the compiler that – “I know what I’m doing, just do it for me”. So, it will not bother about runtime behaviour of that casting, and will trust you that you are not doing anything wrong.

    So, either you can declare your int i as final, or you need to do the casting: –

    int i = 100 ;    // replace 100 with 130, and you will be surprised that it works
    byte b = (byte)i ; 
    

    So, when you use a value 130 and cast it to byte, you pass through the compiler, but will certainly crash at runtime. And this is the problem that the compiler was trying to avoid.


    Now let’s go back the first case: –

    byte b = 128;
    

    The above assignment will now fail to compile. Because even though the value 128 is a compile time constant, it is not big enough to fit in a byte, and that the compiler knows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a) Shouldn’t the following assignment cause an error, since number 100 is a literal
I wanted to create an external table, but did not have the CREATE ANY
I am looking for a way to encode 100 byte on paper and hope
protected IplImage processImage(byte[] data, int width, int height) { int f = 4;// SUBSAMPLING_FACTOR;
The example below compiles: public static void Main() { Byte b = 255; b
Could someone point me, why here: Byte b = 100; b = (Byte)(b+200); I
byte[] bytes = new byte[100]; bytes.Initialize(); What's the default values in above bytes byte
InputStream data = realResponse.getEntity().getContent(); byte[] preview = new byte[100]; data.read(preview, 0, 100); // Now
byte b; int i; unchecked { b = 255 + 255; //overflows i =
If compiler is able to implicitly convert integer literal into byte type and assign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.