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

  • Home
  • SEARCH
  • 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 6672869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:33:34+00:00 2026-05-26T03:33:34+00:00

byte b1 = 3; byte b2 = 0; b2 = (byte) (b2 + b1);

  • 0
byte b1 = 3;
byte b2 = 0;

b2 = (byte) (b2 + b1);  // line 3
System.out.println(b2);

b2 = 0;
b2 += b1;               // line 6
System.out.println(b2);

On line 3, it’s a compiler error if we don’t typecast the result to a byte — that may be because the result of addition is always int and int does not fit into a byte. But apparently we don’t have to typecast on line 6. Aren’t both statements, line 3 and line 6, equivalent? If not then what else is different?

  • 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-26T03:33:34+00:00Added an answer on May 26, 2026 at 3:33 am

    Yes, the two lines are equivalent – but they use different parts of the language, and they’re covered by different parts of the JLS. Line 3 is the normal + operator, applied to bytes which have been promoted to int, giving an int result. That has to be cast before you can assign it back to a byte variable.

    Line 6 is a compound assignment operator as described in section 15.26.2 of the JLS:

    At run time, the expression is evaluated in one of two ways. If the left-hand operand expression is not an array access expression, then four steps are required:

    • First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
    • Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
    • Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
    • Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

    It’s the last part (as highlighted) that makes it different.

    In fact, the start of the section shows the equivalence:

    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

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

Sidebar

Related Questions

byte[] bytes = new byte[] { 1, -1 }; System.out.println(Arrays.toString(new String(bytes, UTF-8).getBytes(UTF-8))); System.out.println(Arrays.toString(new String(bytes,
I THINK this line is causing a 3.5K Byte memory leak, any ideas why?
Virtually every code example out there reads a TXT file line-by-line and stores it
I have some scientific image data that's coming out of a detector device in
BYTE* pImageBuffer = NULL; eResult res = PlayerLib::CreateImageSnapshot( iPlayerRef, eBMP, &pImageBuffer ); if( res
byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); How will I
byte[] mediaBytes = Convert.FromBase64String(<<strings>>); XpsDocument doc; ms = new MemoryStream(mediaBytes, 0, mediaBytes.Length); Uri DocumentUri
Struct { byte F1[2] SHORT F2 byte F3[512] } BPD CBD { SHORT CLENGTH
I have a byte array in memory, read from a file. I would like
I have a byte[] array, the contents of which represent a TIFF file (as

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.