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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:21:13+00:00 2026-06-02T18:21:13+00:00

i am trying some java code like this class Test { public static void

  • 0

i am trying some java code like this

class Test {
public static void main (String [] args){
    byte b = 10;
    b = b + 10;
}

}

after saving when i tried to compile it , it is giving me an error

D:\java\Test.java:4: possible loss of precision

found : int
required: byte
b = b + 10;
^
1 error

but there is no if try something like this

b++;
b+=10;

it is perfectly alright
what is reason for this ?

  • 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-02T18:21:14+00:00Added an answer on June 2, 2026 at 6:21 pm

    You have to write your original code as

    b = (byte)(b + 10);
    

    The problem is that b + 10 is of type int, as the byte is widened to an int.

    The reason for this is that there is a conceptual ambiguity, if b was, say, 120. Is then b+10 equal to 130, or is it equal to -126?

    Java designers decided that addition should be carried out in int in this case, so that 120+10 is 130. Then it can’t be stored into a byte.

    For b+=10, it’s clear that you want to modify b, so it’s a byte addition.

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

Sidebar

Related Questions

I'm converting some old Java code to Scala. I have this. class XYCustomRenderer(dataMax: Double,
I am trying to write some test code for my java application using Scalatest.
I am trying to post some java code snippets on wordpress.com using the following
All, I'm trying to do some unit testing in some archaic java code (no
I'm trying to clean up some warnings in some old Java code (in Eclipse),
A very small portion of our codebase is some legacy Java code. I'm trying
I am trying to automate some Weka classification using Java code. I'm getting the
I am trying to convert some code to Objective-C from Java and I'm getting
I'm trying to learn linked-lists in Java and had some questions about the code
I am trying to mock a class like so (thank's google for code design

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.