• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using StringBuilder in switch

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code snippet below



In the code snipper above toString() converts the StringBuilder to String right, then why am i getting a compiler error that StringBuilder cannot be converted to int ..incompatible types.
how does this line affect my StringBuilder -> sb.append(Str.toString());
the code works fine when i modify this line to ->sb.append(Str)

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35269
383
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya,
toString() does convert to a String. However, it returns the String rather than changing the type of the variable. (You can't change the type of an existing variable like that in Java.) Since you don't assign the return type to a variable, it is ignored.

This does work:



The compiler error should say it isn't an int or String or an enum. Not just an int.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:In the code snipper above toString() converts the StringBuilder to String right, then why am i getting a compiler error that StringBuilder cannot be converted to int ..incompatible types.

A StringBuilder is a helper class to build strings (hence its name). As you (should) know a String object is immutable, so for every change (e.g. append another string, lower case, replace a character,..) a new String object has to be created. That could become very memory intensive. If you use a StringBuilder, only one object needs to be created to perform all these changes. Once you are happy with the string you have built, you can invoke the toString() method. This will convert the StringBuilder to a String, but (as Jeanne already mentioned) it doesn't change the type of the variable. Changing the type of a variable is impossible in Java. But if you store the string returned by the toString() method in a String reference variable, you can use this variable in a switch statement. Let's illustrate with a small code snippetOr you could use the toString() method in the expression of the switch statement, like in this exampleBoth code snippets will compile sucessfully.

Ramya Subraamanian wrote:how does this line affect my StringBuilder -> sb.append(Str.toString());
the code works fine when i modify this line to ->sb.append(Str)

Both statements sb.append(Str.toString()); and sb.append(Str); are equivalent. The same string will be appended to the StringBuilder instance. And this code snippet proves it's the same string

And finally, do you know there's a difference between the statements String str = new String("S"); and String str = "S";? If you don't, you must definitely read this article about strings and the String Literal Pool. You might even read the article if you know both statements are different, it's really an excellent article.

Hope it helps!
Kind regards,
Roel
 
Jude Niroshan
Ranch Hand
Posts: 130
5
Eclipse IDE Java Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:
how does this line affect my StringBuilder -> sb.append(Str.toString());
the code works fine when i modify this line to ->sb.append(Str)


I got a question over-here. I don't agree with that statement. Code can't work even after that modification she has done. Only convertible int values, strings or enum variables are permitted as the parameter for a switch.
Note : Boolean also cannot be passed into it as a parameter.Exceptional scenario
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude Niroshan wrote:
Ramya Subraamanian wrote:
how does this line affect my StringBuilder -> sb.append(Str.toString());
the code works fine when i modify this line to ->sb.append(Str)

I got a question over-here. I don't agree with that statement. Code can't work even after that modification she has done.

Yes, you are definitely correct! The code snippet in the 1st post won't compile successfully by changing the content of the StringBuilder object by appending/inserting/... different values. The one and only way to successfully compile that code snippet, is to change the switch parameter to one of the allowed data types:
  • convertible int values (that is any value which can be converted to an int, either by widening, unboxing or a combination)
  • strings
  • enum variables


  • Some examples of convertible int values. All these code snippets will successfully compile

    Hope it helps!
    Kind regards,
    Roel
     
    Puspender Tanwar
    Ranch Hand
    Posts: 392
    2
    Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    every thing has been explained by Roel and jeanne. Lastly i want to add this to the Roel's last post(if you don't know about this).

    Only char, byte, short, int, enum, string and wrapper are allowed in the switch expression. // switch (expression)

    and only compile time constants are allowed for the case in switch .
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Puspender Tanwar wrote:Only char, byte, short, int, enum, string and wrapper are allowed in the switch expression. // switch (expression)

    True! But wrapper is pretty vague. So a little clarification: only the primitive wrapper classes of the allowed primitive data types are (of course) allowed in the switch expression (so using Long, Float or Double will result in a compiler error).
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    don't agree with that statement. Code can't work even after that modification she has done. Only convertible int values, strings or enum variables are permitted as the parameter for a switch.


    @ Jude - you are right .Sorry I missed that.

    certain things are running in my mind about this

    Changing the type of a variable is impossible in Java. But if you store the string returned by the toString() method in a String reference variable, you can use this variable in a switch statement. Let's illustrate with a small code snippet


    it is not because -> String is immutable, the StringBuilder modified to string is lost hence the compiler still considers sb to be StringBuilder and not String and so throws an error.

    even when we change the data types for other primitives through casting , they must be assigned by creating a new variable like or use it directly as this

    got confused because StringBuilder is mutable and deals with building strings.

    @Roel - the article is good . Thanks for your inputs Jeanne,Puspender, Jude.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:certain things are running in my mind about this

    Changing the type of a variable is impossible in Java. But if you store the string returned by the toString() method in a String reference variable, you can use this variable in a switch statement. Let's illustrate with a small code snippet


    it is not because -> String is immutable, the StringBuilder modified to string is lost hence the compiler still considers sb to be StringBuilder and not String and so throws an error.

    What's this about? The statement you have quoted is definitely true! So why do you think it's not (true)?
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay..i have to rephrase that .."its not only because " String is immutable, the StringBuilder modified to string is lost hence the compiler still considers sb to be StringBuilder and not String and so throws an error.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:okay..i have to rephrase that .."its not only because " String is immutable, the StringBuilder modified to string is lost hence the compiler still considers sb to be StringBuilder and not String and so throws an error.

    Still a little about your statement... sb is declared as a StringBuilder (on line1), so the compiler will always consider sb as a StringBuilder. When sb is used in a switch expression, the compilation will always fail. It's completely irrelevant if String is immutable or if the string created by the toString() method is lost or... The compiler only cares about the type of the variable used in switch expression. In this case, it's reference variable sb which is a StringBuilder and that's not allowed so you'll get a compiler error.
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i meant, its not enough if we convert the type, we must assign it to another variable as we do in casting of other primitives and ..right it is irrelevant that String is immutable.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:i meant, its not enough if we convert the type, we must assign it to another variable as we do in casting of other primitives

    True! Or you could use the converted value directly as you illustrated in one of your previous posts
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic