Explaining a better way to Java language design to beginners.
David Alayachew
davidalayachew at gmail.com
Thu Apr 18 03:46:32 UTC 2024
Then I will add one more point on the pile -- the new String Templates
feature means that you really don't need to use + any more for
concatenation. In fact, it's usually easier to read without it. So you can
completely side step this issue by telling them to use String Templates.
You can even point out that using + is not the ideal anymore (if it ever
was).
https://docs.oracle.com/en/java/javase/21/language/string-templates.html#GUID-CAEF15BD-C3D1-43D4-B38F-1615B0B1699D
And here is the JEP for String Templates -- https://openjdk.org/jeps/459
On Wed, Apr 17, 2024 at 11:26 PM tzengshinfu <tzengshinfu at gmail.com> wrote:
> Hello, David,
>
> Thank you for your reply.
>
> What troubles beginners is that types which intuitively could be operated
> with the + operator require the use of the add() method,
> while the String, treated as an object, can be concatenated with the +
> operator.
> This inconsistency in logic somewhat creates noise in the learning process.
> (Of course, it will seem natural after understanding more, but at the
> beginning of learning, it's all about confusion.)
> However, I understand that everything cannot be changed (given Java's
> position as a leading language).
>
> So, I'll reiterate that phrase: "Don't ask, just write, let it become a
> part of you!"
>
>
> /* GET BETTER EVERY DAY */
>
>
>
>
> David Alayachew <davidalayachew at gmail.com> 於 2024年4月17日 週三 上午6:07寫道:
>
>> You missed a point.
>>
>> String is the single most used class in Java BY FAR. Nothing comes close
>> to it. And the Java designers knew this would happen before hand.
>>
>> When you are the literal most used class in Java, you get a few
>> privileges. Using + for concatenation is only one of them.
>>
>> String is informally known as a "Blessed Type". This is an informal name
>> to represent types that are so frequently used or so useful that they get
>> treated differently by the JVM.
>>
>> So, in short, blessed types get some extra love and some extra
>> privileges. Since String is a "Blessed Type", it gets many privileges, one
>> of which is the + operator.
>>
>>
>> On Tue, Apr 16, 2024, 12:43 PM tzengshinfu <tzengshinfu at gmail.com> wrote:
>>
>>> Hi, folks,
>>>
>>> I'm glad that "JEP 463: Implicitly Declared Classes and Instance main
>>> Methods" can help beginners start writing basic programs with limited
>>> skills and understanding. However, recently I came across this post [
>>> https://twitter.com/relizarov/status/1767978534314627304] and I recall
>>> having asked similar questions here before [
>>> https://mail.openjdk.org/pipermail/amber-dev/2023-October/008334.html].
>>> But today, I am explaining Java's design to a beginner from the perspective
>>> of a semi-experienced developer.
>>>
>>> As we transitioned our tech stack from C# to the Java ecosystem, we also
>>> welcomed a new colleague who was previously accustomed to PHP. During my
>>> guidance on Java, we had the following conversation:
>>>
>>> New colleague: Why do we use `equals()` for string comparison instead of
>>> `==` or `===`?
>>> Me: Because `String` is an object.
>>> New colleague: Then why can we concatenate strings with the `+` operator?
>>> Me: The `+` operator is actually shorthand for `StringBuilder.append()`.
>>> From an OOP perspective, you can also use `string1.concat(string2)`.
>>> New colleague: Why isn't the `+` operator used for `BigDecimal` addition
>>> here?
>>> Me: Because it's also an object...
>>> New colleague: Looking back, if strings are objects, shouldn't it be
>>> `String string1 = new String()` and then `string1.setValue(new char[] {
>>> 's', 't', 'r', 'i', 'n', 'g' })`?
>>> Me: That would be too cumbersome... By the way, how did you compare
>>> strings in PHP?
>>> New colleague: `===`, `strcmp()` is also an option, but less common.
>>> And after many more questions...
>>> Me: Don't ask, just write, let it become a part of you!
>>>
>>> Do you have any better explanations for Java's design for beginners?
>>>
>>>
>>> /* GET BETTER EVERY DAY */
>>>
>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240417/1522c448/attachment-0001.htm>
More information about the amber-dev
mailing list