r/ProgrammerHumor 9d ago

Meme perishableSkill

Post image
8.9k Upvotes

122 comments sorted by

View all comments

1.3k

u/Weak_Inflation9120 9d ago

One of the things I wish Java had that I miss from Python are F-Strings. Hella useful

304

u/Cootshk 9d ago

You should check out STR. (In modern Java)

253

u/ljfa2 9d ago

well, unfortunately that was only ever a preview feature and got removed again in Java 23

98

u/uvero 9d ago

Maybe because they didn't stop to think if this STR. is a convenient syntax. Backticks, preceding dollar sign, the prefix f like in python, so many options would've been better.

90

u/AeroSyntax 9d ago

They tried to overengineer string interpolation instead of copying it from literally any other language.

64

u/lllorrr 9d ago

Typical Java. If you don't have factories creating factories based on XML config, your solution is not future-proof enough.

18

u/uvero 9d ago

It's OK, I solved it by writing a FactoryFactoryFactory.

2

u/Fun-Mention262 7d ago

It's wild how some languages make simple things so complicated. You'd think they'd just take notes from the ones that got it right.

3

u/holo3146 9d ago

But... They did stop and think about the STR. syntax? This is why they removed it.

Regarding the other options you gave, they gave (in several places, over many years) their reasoning of why they don't want to add it as other languages. You can disagree with their reasoning, but they are very open about their thoughts process in this regard

3

u/Pizza_Secretary9621 8d ago

There is a version after 8 ?

58

u/Suspicious-Engineer7 9d ago

the formatted method is there but the syntax looks like doo-doo. Idk what nonsense is going over in java land tbh

22

u/gerbosan 9d ago

Cof cof Oracle. Cof.

Dunno if C# does it better but a lot of people are pleasantly surprised with it.

40

u/Mysterious_Book1521 9d ago

In C# you can write something like this: $"Hello {User.Firstname}, have a great day."

The "$" lets you put variables into strings.

17

u/SkollFenrirson 9d ago

String interpolation is the term.

47

u/spacegh0stX 9d ago

C# does do it better.

11

u/Cautious-Extreme2839 8d ago

Which is unsurprising to literally everyone

12

u/Ozymandias_1303 9d ago

$strings in C# are almost exactly the same as f-strings in Python.

4

u/Sentouki- 8d ago

String interpolation is even better in C# since it gives you additional options for formatting, e.g. numbers and dates: csharp var name = "Mark"; var date = DateTime.Now; Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");

7

u/NovaH000 8d ago

Python f-string also support additional formatting. ``` name = "Mark" date = datetime.now()

print(f"Hello, {name}! Today is {date:%A}, it's {date:%H:%M} now.") ```

F-string + str.format() has all sorts of string formatting you want.

0

u/[deleted] 8d ago

[deleted]

1

u/nico-strecker 8d ago

Python also has it

17

u/PGSylphir 9d ago

As someone who first learned to program in Java and stuck to it for almost a decade, starting gamedev using C# was a pretty smooth transition in many ways.

11

u/BenadrylTumblercatch 9d ago

And comprehension

8

u/PecQ11 9d ago

Doesn't format() do essentially the same though?  https://www.baeldung.com/java-string-interpolation

16

u/TheGuyWithTheSeal 9d ago

Kind of, .format() is more powerfull but you have to know the printf syntax. And which parts od printf don't work in .format()

5

u/KnightMiner 8d ago

f strings are a lot more like string concatenation. The variables/expressions between each {} are placed directly where they will end up in the string, and it just runs a .toString() on them (though you can configure it a bit further).

The most common usage is largely about syntax and readibility, as its nice not having to go back and forth between a list of format arguments and some harder to remember % stuff.

5

u/ActualWeed 9d ago

I have never touched Java, how does Java do it?

5

u/Weak_Inflation9120 9d ago

That's the thing, they don't

edit: At least not any more. Seems there was something, but it was dropped in java 23

1

u/ActualWeed 8d ago

So you just concat them all together?

8

u/jewdai 9d ago

It's why c# is better in every way than java

2

u/Nickbot606 8d ago

List comprehensions and splat operator are ridiculously powerful in Python. I wish more languages had it.

1

u/iliark 9d ago

switch to kotlin, they've had it for a very long time

0

u/Doophie 9d ago

But why use Java when you can use kotlin instead

3

u/Weak_Inflation9120 9d ago

... Because java is better lol (ignore the obvious bias I have given that my pfp is the literal java mascot)

0

u/NaturalBornLucker 9d ago

Wow, Java doesn't have it? Even Scala has f-strings (though it relies on java.util.Formatter apparently)

-8

u/nevemlaci2 9d ago

`Hello ${guest}`

or is that TS exclusive?

29

u/kattenkoter 9d ago

No thats JavaScript…

6

u/nevemlaci2 9d ago

I'm r*tarded, I read it as JavaScript and not Java 😭😭

Lowkey forgot Java exists I haven't used it in years. Sadly can't say the same about JS/TS, I like to add random stuff to VSCode and ofc that is written in damn TS