Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Java Oracle

New in Java 13: Text Blocks (oracle.com) 57

The October issue of Oracle's Java magazine includes an article reminding us that Java 13 includes a long-awaited new features: text blocks. With text blocks, Java 13 is making it easier for you to work with multiline string literals. You no longer need to escape the special characters in string literals or use concatenation operators for values that span multiple lines. You can also control how to format your strings. Text blocks -- Java's term for multiline strings -- immensely improve the readability of your code...

A text block is defined using three double quotes (""") as the opening and closing delimiters. The opening delimiter can be followed by zero or more white spaces and a line terminator. A text block value begins after this line terminator.

This discussion has been archived. No new comments can be posted.

New in Java 13: Text Blocks

Comments Filter:
  • Cool I guess (Score:5, Insightful)

    by Anrego ( 830717 ) on Sunday October 27, 2019 @03:52PM (#59353222)

    I mean, yeah that will probably come in handy occasionally I guess.
    Realistically though, any time I have a block of string more than a few lines long, it's usually coming from a config file or database. I associate "big block of text" as more of a scripting thing than a proper application thing. But it does no harm to have it supported so what the hell.

    • by Livius ( 318358 )

      Sometimes you need a bunch of bulk text in one place in the code, and it's a nuisance, but I'm not convinced this is much of an improvement.

      • Re:Cool I guess (Score:4, Insightful)

        by ShanghaiBill ( 739463 ) on Sunday October 27, 2019 @04:19PM (#59353276)

        From the description in the summary, it works exactly like text blocks in Python. It is a handy feature. It makes code both easier to write and easier to read.

        • From the description in the summary, it works exactly like text blocks in Python. It is a handy feature. It makes code both easier to write and easier to read.

          Serves the same purpose as "here docs" in Perl.

    • Realistically though, any time I have a block of string more than a few lines long, it's usually coming from a config file or database.

      I had the same thought, there have been very few times I've had extended blocks of text, and none that I can think of the text wouldn't have been better off in a file that was loaded at runtime.

      I was wondering what Java people would end up using this for (and I ask that as someone who has done a lot of Java server development before in the past).

      • by Anrego ( 830717 )

        This seems like the kind of feature you would use in a really simple application that is never going to be translated into another language and where setting up a configuration system and template engine and dealing with having to deploy more than just a single file becomes more bother than its worth.
        That is, this feels more like a convenient cop-out that may occasionally be justified than something that really adds anything to the language. I won't claim I've never had a big blob of text in a message to th

        • by jma05 ( 897351 ) on Sunday October 27, 2019 @09:52PM (#59353854)

          It is an extremely useful feature that should be present in every language. It is long overdue.
          You don't need to have large text blobs in source. I start wishing for it when I have like 3 lines, like a bit of formatted SQL in code.

          Java + SQL looks so ugly that everyone reaches for an ORM right away, even for simple use. No one does that in Python or similar language. This is so basic that even C++ 11 has it (raw string literals). As usual, Java is late to the game in modernizing the language, even compared to C++. This should have been introduced in Java 8.

        • This seems like the kind of feature you would use in a really simple application that is never going to be translated into another language and where setting up a configuration system and template engine and dealing with having to deploy more than just a single file becomes more bother than its worth.

          So...most applications, then.

      • The same reason java has inner classes. It may be unwanted to decoupling part of the code that has no place but as part of the same class. Example: a string template, unit test data. Anyone have others to list?
    • I'm one of those weirdos who still hasnt adoped orm persistance frameworks.. i see a big win for things like complex sql queries.

      This is mostly an issue in my mind where say wanting to copy the query to test or modify in the database management/dev tools.

      Basic quality of life improvment in my humble opinion :)

    • Proper applications need to write native SQL, unless you hate good performance and low cloud-computing costs. If you think Java won't benefit from multi-line Strings, you either never touch a production DB or you only use Hibernate. If the 2nd is true, your users hate you because your app is slow.

      I write proper applications with literally millions of users for a multi-billion dollar company. Because I am writing proper applications, I need to write templates for XML and JSON interfaces as well as na
      • by RightSaidFred99 ( 874576 ) on Sunday October 27, 2019 @08:33PM (#59353718)
        You do know that ORM frameworks ultimately just write sql code and run it, right? Sure sometimes you will be able to hand craft a better query but that won't be the norm. And decent ORMs allow you to do that when you need to.
        • Securely. I guess the GP's production code is only run in enterprise environments where people with names like Bobby ';DROP * from *;' Tables mysteriously never made it through the hiring process due to system outages whenever they submitted their resume.

          • Look up java.sql.PreparedStatement! This problem was solved in the late 90s, back when I was in school jamming to Firestarter by the Prodigy and making fun of people wearning JNCO jeans....eagerly waiting for Quake to come out. :)
            • by jrumney ( 197329 )

              Your timeline is off. Quake came out in 1996, before both Firestarter and Java 1.1 (the first version with java.sql).

        • You do know that ORM frameworks ultimately just write sql code and run it, right? Sure sometimes you will be able to hand craft a better query but that won't be the norm. And decent ORMs allow you to do that when you need to.

          I am very well aware. It is my daily job to write Java, DB, and JPA code. One of the banes of my existence is showing bad developers how to get results in queries instead of loading giant datasets into memory and manipulating them in Java.

          JPA is fine for basic CRUD. Once you have to join lots of tables or do analysis on data, it often just doesn't cut it and you have to write a direct SQL query, which you can pass to Hibernate. I often replace 1000 line blocks of slow, bloated code from people who s

    • SQL code blocks?
  • by account_deleted ( 4530225 ) on Sunday October 27, 2019 @04:01PM (#59353232)
    Comment removed based on user account deletion
    • Re:Yay (Score:4, Interesting)

      by Anrego ( 830717 ) on Sunday October 27, 2019 @04:08PM (#59353250)

      I feel like while this has its legit uses, it's more of an anti-feature.
      Some obvious exceptions, but generally I think if you have a wall of text, it aught to be in a separate file and not just hammered into the code. If substitutions are required, you should be using a template engine. Big blobs of static text make it harder to translate a program into another language, and couple presentation logic to business logic. Both of these are at best irrelevant but never good.

      • The template engine would usually be 100 times slower than the compiler, why the funk would I bother with that and learn the template engine API?

        Most languages have multi line texts.
        And it cant be so hard to do a search and replace in proper text editor and convert a multi line string into a concatanated list of single line strings.

        • by Anrego ( 830717 )

          Legit not sure if trolling or serious, so will give benefit of doubt.

          The template engine would usually be 100 times slower than the compiler, why the funk would I bother with that and learn the template engine API?

          Substitutions pretty much have to be a runtime thing, because the compiler isn't going to know the user input to be interpolating in. Regardless most good template engines actually compile the templates, and even then resolving a template to output is happening at the presentation layer where it's unlikely to be the performance bottleneck.

          Most languages have multi line texts.
          And it cant be so hard to do a search and replace in proper text editor and convert a multi line string into a concatanated list of single line strings.

          It took me awhile to figure out what you were on about here.. but I meant spoken languages, not progra

          • Substitutions pretty much have to be a runtime thing, because the compiler isn't going to know the user input to be interpolating in.

            The compiler will transform something like this:
            """
            first line ${firsVar}
            ${topicVar} about the topic
            """

            into code like this:

            StringBuilder res = new StringBuilder()
            .append("first line ")
            .append(firstVar).append("\n")
            .append(topicVar)
            .append(" about the topic).append("\n")

            Regardless most good template engines actually compile the templates,
            Yes, a template could be compiled

      • SQL queries. That I think is the big use case for multiline text blocks. A 50 line query no longer requires StringBuilder line by line concatenation. That will improve readability.

        • by Anrego ( 830717 )

          Yeah, that's certainly valid, though having things broken up does let you commentate on the relationships as you build the query.

        • WOW. I was baffled how SQL injection is still even a thing let alone a top mention in security talks, and...there you have it. Java(TM) - the problems of yesterday, today
          • SQL injection is negated nowadays by parametrized queries. The entire statement is not built dynamically, just the shell, like this: "Select col1, col2, col3 from myTable where col1 = ? and col2 like ?;" Then, from incoming data, you extract the parameters (which the first and second question marks represent, and are automagically identified as parameter 1 and parameter 2 by the java interpreter/compiler), and via a java statement, load them into the query. Very simple.

            ANY language, Java, C#, PHP, etc, whe

  • by Retired ICS ( 6159680 ) on Sunday October 27, 2019 @04:28PM (#59353298)

    Stop Thief!

    You stole that idea from Python. It is a copyright infringement for you to be using it.

    Oracle, hoisted by their own petard.

  • Congrats (Score:4, Informative)

    by drinkypoo ( 153816 ) <drink@hyperlogos.org> on Sunday October 27, 2019 @05:54PM (#59353446) Homepage Journal

    Congratulations, Java, for finally catching up to the bourne shell. Well, almost. In the shell you can use whatever delimiter you want.

  • by account_deleted ( 4530225 ) on Sunday October 27, 2019 @06:21PM (#59353482)
    Comment removed based on user account deletion
    • Re: (Score:2, Interesting)

      by Anonymous Coward

      I'll bite, what is so special about 1.8 (or I guess so special about later versions).

  • Unencumbered text blocks were always supported weren't they?

    String stuff = new String(Files.readAllBytes(Paths.get("mystuff.txt")));

  • by account_deleted ( 4530225 ) on Sunday October 27, 2019 @08:17PM (#59353688)
    Comment removed based on user account deletion
  • The approach chosen for Java 13 is not even particularly feature-rich. It doesn't allow for compiler-checked string interpolation or producing anything other than java.lang.String values (e.g. generating lazy CharSequences or parsing Text Blocks into completely different types doesn't seem to be possible). At least they seem to be getting indentation right, so you can produce any amount of indentation in the text while staying flexible regarding coding style. Seems to me like an quite conservative, although

  • Huh, so Java is finally doing the thing I could do in shell scripts decades ago? They are even doing it just the way I do it in Python. Trendy. So when are they are going to have garbage collection that works instead of hogging up all of your precious RAM like the hoarders who wrote it probably do to their homes and then freezing up the whole application to do house cleaning on a modern multiple hardware threaded system because we say we understand threading, but in a practical sense they really don't?
  • Java 13? Last I was hearing about was Java 9, and the JRE is still Java 8. What gives?
    • The "leap" from JDK 8 to JDP 9 took about 3.5 years. I think this was because of some silly "community arguments" or something similar. After this, they seem to be on a twice-a-year release schedule. So every half a year you now get a new Java version. One year from now it will be JDK 15 already. If you look at the features introduced, it does not seem very exciting, but whatever. Almost looks like an Oracle business model with all the support times and licenses.

      https://en.wikipedia.org/wiki/... [wikipedia.org]

      JDK8 I belie

  • Wow Oracle/Java13 devs, I can see your MIT education really pays for itself!

        - Barney Calhoun

Ocean: A body of water occupying about two-thirds of a world made for man -- who has no gills. -- Ambrose Bierce

Working...