Search code examples
c#javalanguage-comparisons

Why doesn't Java have a way of specifying unescaped String literals?


In C#, if you want a String to be taken literally, i.e. ignore escape characters, you can use:

string myString = @"sadasd/asdaljsdl";

However there is no equivalent in Java. Is there any reason Java has not included something similar?

Edit:

After reviewing some answers and thinking about it, what I'm really asking is:
Is there any compelling argument against adding this syntax to Java? Some negative to it, that I'm just not seeing?


Solution

  • Java has always struck me as a minimalist language - I would imagine that since verbatim strings are not a necessity (like properties for instance) they were not included.

    For instance in C# there are many quick ways to do thing like properties:

    public int Foo { get; set; }
    

    and verbatim strings:

    String bar = @"some
    string";
    

    Java tends to avoid as much syntax-sugar as possible. If you want getters and setters for a field you must do this:

    private int foo;
    
    public int getFoo() { return this.foo; }
    public int setFoo(int foo) { this.foo = foo; }
    

    and strings must be escaped:

    String bar = "some\nstring";
    

    I think it is because in a lot of ways C# and Java have different design goals. C# is rapidly developed with many features being constantly added but most of which tend to be syntax sugar. Java on the other hand is about simplicity and ease of understanding. A lot of the reasons that Java was created in the first place were reactions against C++'s complexity of syntax.