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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:54:18+00:00 2026-05-27T22:54:18+00:00

When adding ‘a’ + ‘b’ it produces 195. Is the output datatype char or

  • 0

When adding 'a' + 'b' it produces 195. Is the output datatype char or int?

  • 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-27T22:54:18+00:00Added an answer on May 27, 2026 at 10:54 pm

    The result of adding Java chars, shorts, or bytes is an int:

    Java Language Specification on Binary Numeric Promotion:

    • If any of the operands is of a reference type, unboxing conversion
      (§5.1.8) is performed. Then:
    • If either operand is of type double, the
      other is converted to double.
    • Otherwise, if either operand is of type
      float, the other is converted to float.
    • Otherwise, if either operand
      is of type long, the other is converted to long.
    • Otherwise, both
      operands are converted to type int.

    But note what it says about compound assignment operators (like +=):

    The result of the binary operation is converted to the type of the left-hand variable … and the result of the conversion is stored into the variable.

    For example:

    char x = 1, y = 2;
    x = x + y; // compile error: "possible loss of precision (found int, required char)"
    x = (char)(x + y); // explicit cast back to char; OK
    x += y; // compound operation-assignment; also OK
    

    One way you can find out the type of the result, in general, is to cast it to an Object and ask it what class it is:

    System.out.println(((Object)('a' + 'b')).getClass());
    // outputs: class java.lang.Integer
    

    If you’re interested in performance, note that the Java bytecode doesn’t even have dedicated instructions for arithmetic with the smaller data types. For example, for adding, there are instructions iadd (for ints), ladd (for longs), fadd (for floats), dadd (for doubles), and that’s it. To simulate x += y with the smaller types, the compiler will use iadd and then zero the upper bytes of the int using an instruction like i2c (“int to char”). If the native CPU has dedicated instructions for 1-byte or 2-byte data, it’s up to the Java virtual machine to optimize for that at run time.

    If you want to concatenate characters as a String rather than interpreting them as a numeric type, there are lots of ways to do that. The easiest is adding an empty String to the expression, because adding a char and a String results in a String. All of these expressions result in the String "ab":

    • 'a' + "" + 'b'
    • "" + 'a' + 'b' (this works because "" + 'a' is evaluated first; if the "" were at the end instead you would get "195")
    • new String(new char[] { 'a', 'b' })
    • new StringBuilder().append('a').append('b').toString()
    • String.format("%c%c", 'a', 'b')
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Adding an element to the head of an alist (Associative list) is simple enough:
Adding source files more than one directory away (e.g. ../../source.cpp or ../../../somewhere_else/source.cpp, vs. just
Adding more than one child to a WPF StatusBar results in poor layout with
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Adding the row one by one in the button click.. (i tried but its
Adding functionality to a class can be done by adding a method or by
When adding new properties to classes, I find myself typing the same things over
When adding a DLL as a reference to an ASP.Net project, VS2008 adds several
When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then
When adding a reference to a project I sometimes see the message... The Target

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.