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 6540229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:57:11+00:00 2026-05-25T10:57:11+00:00

Why do I have to cast 0 to byte when the method argument is

  • 0

Why do I have to cast 0 to byte when the method argument is byte?

Example:

void foo() {
   bar((byte) 0);
}

void bar(byte val) {}

I know that if the argument is of type long I don’t have to cast it, so I’m guessing that Java thinks of mathematical integers as integers runtime.

Doesn’t it discourage the usage of byte/short?

  • 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-05-25T10:57:11+00:00Added an answer on May 25, 2026 at 10:57 am

    The literal 0 is an integer, and there is no automatic cast from integer -> byte, due to a potential loss of precision. For example, (byte)1024 is outside the valid range. Perhaps the compiler could be smarter and allow this for small integer literals, but it doesn’t…

    Widening to long is okay, since every integer can be a long with no loss of information.

    And yes, for this reason I would almost never use short or byte in any APIs exposed by my code (although it would be fine for intermediate calculations)

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

Sidebar

Related Questions

I have a method that returns byte[] but for now would like to avoid
I have following exception in the foreach loop Unable to cast object of type
I have to be missing something obvious here. I don't get why this cast
I have a class Something that implements ISomething . How can I convert/cast from
I have a method that contains the following Linq to SQL code: public List<L2SBusinessEntities.Report.MesReport>
So Scope_Identity() returns an ?-Byte Numeric Type in SQL Server. That is not awesome.
I'd like to know if there is a simple way to cast a byte
I have a question about the safety of a cast from long to int.
If I have an object reference that references a byte? , short? or int?
I have a byte array containing 16 & 32bit data samples, and to cast

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.