Could we add \a as an escape sequence for bell?
Pavel Rappo
pavel.rappo at gmail.com
Mon Oct 27 22:53:19 UTC 2025
On Mon, Oct 27, 2025 at 9:19 PM David Alayachew
<davidalayachew at gmail.com> wrote:
>
> Woah, thanks.
>
> So, in your second example, it's literally complaining because, as far as the Java compiler is concerned, I typed in a literal new line in between the single quotes, right? Like this?
>
> System.out.println('
> ');
>
> Correct?
Yes.
> Very important context, ty vm.
>
> So, \7 is fine because that actually is an escape sequence. Much like \n and friends.
Yes. BTW, another way to write down \n would be \12:
jshell> Integer.toOctalString('\n')
$1 ==> "12"
jshell> '\n' == '\12'
$2 ==> true
> But \uXXXX gets turned into the literal bytes, which means that something like a new line gets treated as if I literally pressed the new line button on my keyboard, which means that it's treated not as a new line character in my string, but as a new line character in my source file itself lol.
>
> Ty vm.
Like I said, escape sequences are only recognised in certain context.
But unicode escapes are recognised everywhere. For example, \n does
not mean anything in a javadoc comment, On the other hand, \u000A
means the same thing wherever it appears: source, javadoc, or your
copyright header. Still, \n and \u000A are not the same:
jshell> '\n' == '\u000A'
| Error:
| illegal line end in character literal
| '\n' == '\u000A'
| ^
-Pavel
-Pavel
> On Mon, Oct 27, 2025, 9:44 AM Pavel Rappo <pavel.rappo at gmail.com> wrote:
>>
>> Correcting a typo*:
>>
>> jshell> System.out.println('\u000A')
>> | Error:
>> | illegal line end in character literal
>> | System.out.println('\u000A')
>> |
>>
>>
>> On Mon, Oct 27, 2025 at 1:32 PM Pavel Rappo <pavel.rappo at gmail.com> wrote:
>> >
>> > A word of caution. While in this use case the difference is probably
>> > non-essential, Unicode escapes (e.g. \u0007) are __not__ a kind of
>> > escape sequences (e.g. \7). The former are translated into raw bytes
>> > __before__ the compiler goes any further. The latter are recognised by
>> > the compiler only in context of character literals, strings, or text
>> > blocks.
>> >
>> > To illustrate the difference, let's consider "line feed" (0xA):
>> >
>> > jshell> System.out.println('\n')
>> >
>> >
>> >
>> > jshell> System.out.println('\000A')
>> > | Error:
>> > | unclosed character literal
>> > | System.out.println('\000A')
>> > | ^
>> >
>> > While the former is recognised within the context of a character
>> > literal, the latter just breaks the source code in a similar way as if
>> > the source code had the actual line feed in it.
>> >
>> > Unicode escapes allow you to simply input a Unicode character "by
>> > reference". This is convenient in some cases, such as when your
>> > environment cannot output these characters conveniently or display
>> > them properly.
>> >
>> > -Pavel
>> >
>> > On Mon, Oct 27, 2025 at 12:56 PM David Alayachew
>> > <davidalayachew at gmail.com> wrote:
>> > >
>> > > Ah, I misread the JLS Language Grammar!
>> > >
>> > > I was aware of \u0007, but not \7. I see now that that works. Then nevermind, that meets my needs just fine. No need for my suggestion.
>> > >
>> > > The 4 digit unicode is fine, a good escape hatch, but also easy to forget, since my brain interprets all 4 digits as significant. And char bell = 7; is also unideal. But a 1 digit escape works perfectly for me. Would have preferred \a, but \7 is more explicit and easier to look up.
>> > >
>> > > Ty vm!
>> > >
>> > > On Mon, Oct 27, 2025, 6:15 AM Andrew Dinn <adinn at redhat.com> wrote:
>> > >>
>> > >>
>> > >>
>> > >> On 26/10/2025 17:26, David Alayachew wrote:
>> > >> > Also, here is the JLS 25 entry about escape sequences -- https://
>> > >> > docs.oracle.com/javase/specs/jls/se25/html/jls-3.html#jls-3.10.7
>> > >> > <https://docs.oracle.com/javase/specs/jls/se25/html/jls-3.html#jls-3.10.7>
>> > >> >
>> > >> > All I really want is for that to be added to the list, so that I can do
>> > >> > it the same as I would in other languages. It sounds like an in-place
>> > >> > replacement done by the compiler.
>> > >>
>> > >> What is wrong with using \u0007 or \7? (as documented in the html page
>> > >> you cited).
>> > >>
>> > >> regards,
>> > >>
>> > >>
>> > >> Andrew Dinn
>> > >> -----------
>> > >> Red Hat Distinguished Engineer
>> > >> He/Him/His
>> > >> IBM UK Limited
>> > >> Registered in England and Wales with number 741598
>> > >> Registered office: Building C, IBM Hursley Office, Hursley Park Road,
>> > >> Winchester, Hampshire SO21 2JN
>> > >>
>>
More information about the core-libs-dev
mailing list