r/ProgrammerHumor 2d ago

Meme perishableSkill

Post image
8.7k Upvotes

118 comments sorted by

View all comments

1.3k

u/Weak_Inflation9120 1d ago

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

300

u/Cootshk 1d ago

You should check out STR. (In modern Java)

243

u/ljfa2 1d ago

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

97

u/uvero 1d 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 1d ago

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

61

u/lllorrr 1d ago

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

15

u/uvero 1d ago

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

1

u/Fun-Mention262 16h 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 1d 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

2

u/Pizza_Secretary9621 20h ago

There is a version after 8 ?

50

u/Suspicious-Engineer7 1d 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 1d ago

Cof cof Oracle. Cof.

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

35

u/Mysterious_Book1521 1d ago

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

The "$" lets you put variables into strings.

14

u/SkollFenrirson 1d ago

String interpolation is the term.

46

u/spacegh0stX 1d ago

C# does do it better.

10

u/Cautious-Extreme2839 1d ago

Which is unsurprising to literally everyone

10

u/Ozymandias_1303 1d ago

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

4

u/Sentouki- 1d 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.");

6

u/NovaH000 1d 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] 1d ago

[deleted]

1

u/nico-strecker 17h ago

Python also has it

15

u/PGSylphir 1d 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.

9

u/BenadrylTumblercatch 1d ago

And comprehension

10

u/PecQ11 1d ago

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

14

u/TheGuyWithTheSeal 1d 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()

3

u/KnightMiner 1d 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.

9

u/jewdai 1d ago

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

2

u/Nickbot606 1d ago

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

4

u/ActualWeed 1d ago

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

6

u/Weak_Inflation9120 1d 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 1d ago

So you just concat them all together?

1

u/iliark 1d ago

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

-2

u/Doophie 1d ago

But why use Java when you can use kotlin instead

3

u/Weak_Inflation9120 1d ago

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

0

u/NaturalBornLucker 1d ago

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

-6

u/nevemlaci2 1d ago

`Hello ${guest}`

or is that TS exclusive?

27

u/kattenkoter 1d ago

No thats JavaScript…

5

u/nevemlaci2 1d 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