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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:59:30+00:00 2026-05-25T12:59:30+00:00

In Java given this: String a = str; CharSequence b = charseq; you can

  • 0

In Java given this:

String a = "str";
CharSequence b = "charseq";

you can write

b = b + a;

but cannot write (gives a compiler error)

b += a;

The error is

incompatible types
found   : java.lang.CharSequence
required: java.lang.String

Now in JLS Second Edition this was explainable by this line in 15.26.2 Compound Assignment Operators:

All compound assignment operators require both operands to be of primitive type, except for +=, which allows the right-hand operand to be of any type if the left-hand operand is of type String.

But in JLS Third Edition this comment disappeared, the only thing that is said about compound operator is at 15.26.2 Compound Assignment Operators:

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.

which doesn’t seem to work (see above).

So my question is – what exactly is the relationship between javac and JLS and is this particular example an error in javac or an error in JLS?

  • 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-25T12:59:31+00:00Added an answer on May 25, 2026 at 12:59 pm

    compiler error is a bug in your version javac. As pointed in prior answer this bug is fixed in Java 7.

    See eg Bug ID 7058838 at Sun bug database:

    • description:

    A following function cannot be compiled in java 1.6 or less,. but it can be compiled in java 1.7.

        public static void main(String[] args) {
               Object x = "x";
               String y = "y";
               x += i;
        }
    
    • state:
      Not a Defect
    • evaluation:

      For an Object x and a String y, x+=y is just x=(Object)(x+y). Since y is a String, x undergoes string conversion to produce a string which is concatenated with y, before the no-op cast to Object. The JLS has not changed in this area between SE 6 and SE 7; the program should have been legal for many years.


    For a background, see also old Bug Id 4741726

    • description:

    javac used to allow expressions of the form o += s where o is a variable of type Object and s is an expression of type String. We fixed that recently (4642850) and this caused a build failure (4741702). Perhaps this is common enough that we should relax the spec instead of fixing the compiler?

    • Category:
      java:compiler
    • Release Fixed:
      7(b25) – as far as I understand, this means fixed in build 25 of Java 7
    • evaluation:

    I’m inclined to relax the spec, though we’d have to know what other implementations do before making a final call on this.
    2002-09-04
    JLS3 permits Object+=String because the ‘+’ means string concatenation and that is able to concatenate an Object with a String as easily as a String with an Object.
    2008-01-31

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

Sidebar

Related Questions

Given this code in Java: int i,j; String[] names; names[0] = new String(mary); names[1]
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
Given the following java enum: public enum AgeRange { A18TO23 { public String toString()
How can I parse a String str = abc, \def,ghi\ ; such that I
Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
Possible Duplicate: Split string to equal length substrings in Java Given the following utility
I'm looking for a Java class that for a given string the ${animal} jumped
how i can find the abc from this tag through JAVA code and SAX
I am brand new to Java and was given this piece of code, no
Given this code: class Overloading extends Object { static public void target(Object val, String

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.