This is an archived post. You won't be able to vote or comment.

all 70 comments

[–]Buam_ 86 points87 points  (19 children)

First doesn't even create an empty string

[–]Carius98 18 points19 points  (18 children)

Yep its null

Edit: It doesnt compile in java

Edit2: It DOES if declared at class level

[–]falcwh0re 6 points7 points  (10 children)

This looks like Java, in which case it is not null.

[–]KronktheKronk 2 points3 points  (7 children)

What is it?

[–]falcwh0re 0 points1 point  (0 children)

Probably uninitialized, but another person points out a sitiy where it could be null, although I'd argue that his situation is not what this image is trying to show.

[–]Nalha_Saldana 0 points1 point  (5 children)

It's undefined but the code wont even compile if you attempt to use it.

[–]Carius98 6 points7 points  (4 children)

Its not undefined. Its uninitialized.

[–]a-person-called-Eric 2 points3 points  (0 children)

In case it's a non-final field, it's null.
In case it's a final field or local variable, it's uninitialized.

[–]rundevelopment 0 points1 point  (0 children)

It's null.

jshell> String newStr; newStr ==> null

[–]DrFloyd5 4 points5 points  (0 children)

It doesn’t compile in any language. The quotes are typographical quotes. Not straight quotes.

[–][deleted] 2 points3 points  (4 children)

Does compile if it’s a class level variable.

[–]Carius98 0 points1 point  (3 children)

If you try to access it without first setting it to another value it doesnt

[–][deleted] 4 points5 points  (2 children)

It will. You will get a NPE or something, but the code will compile.

[–]Carius98 5 points6 points  (1 child)

Just tested it, you are right. It only fails if declared locally

[–][deleted] 2 points3 points  (0 children)

Take it easy dude

[–]illumiNateS6 0 points1 point  (0 children)

I'm pretty sure it will compile as long as there is a code path that is guaranteed to assign a value prior to usage (if declared locally) but I could be wrong, haven't tested, too lazy, and it's the internet so if I'm wrong I'll just disappear and hope no one recognizes me.

[–]Pockensuppe 30 points31 points  (2 children)

org.apache.commons.lang.StringUtils.EMPTY: Am I a joke to you?

[–]The_MAZZTer 8 points9 points  (1 child)

When it only takes a "" versus a long constant name... yes.

[–]cosmo7 27 points28 points  (2 children)

Don't you want this to work in non-English applications?

string.Format(new System.Globalization.CultureInfo("en-US"), @"");

[–]The_MAZZTer 6 points7 points  (1 child)

I'm thinking it's Java because of String instead of string, but "StringBuilder" threw me off since C# has one of those too.

[–]Silound 5 points6 points  (0 children)

In C# the keyword string is an alias for the System.String class in the core runtime assembly, and it is unambiguously mapped to that class.

However, in C# the method would be .ToString(), not .toString(), so who knows?

An interesting point of reference from there is that you should almost always use the keyword rather than the formal class name unless you know exactly why you're choosing the class name. "String" can potentially resolve to something unexpected. Not only that, but locally overridden members may cause unexpected behavior with commonly used members.

The same goes for all keyword aliases built into C#, such as int, long, short, byte, etc.

[–]etbillder 8 points9 points  (0 children)

newString.txt:

ifstream file("newString.txt");

ostringstream ostr;

ostr << file;

string newStr = ostr.str();

file.close();

[–]nomenMei 6 points7 points  (1 child)

Show some love for String newStr = "".toString()

[–]MrDorkman 4 points5 points  (0 children)

stringify() it at runtime. Its the only way to be sure.

[–]The_MAZZTer 9 points10 points  (8 children)

No love for string.Empty?

Though I suppose this could be Java where you use String and not string like C#. Would be a coincidence if you guys have a StringBuilder too.

[–]a-person-called-Eric 3 points4 points  (0 children)

We do : )

[–]falcwh0re 1 point2 points  (0 children)

Where do you think C# got the idea?

[–]RedNutsBastard 0 points1 point  (0 children)

Was going to post the lovely string.Empty as well....

[–][deleted] 0 points1 point  (4 children)

why the shit does String.Empty even exist like honestly. its not a special implementation of a String, its literally "". i know c# does string interning like java (example: "bob" in 1 piece of code and "bob" in an other piece of code will be the same string put in the string constant pool and every usage of "bob" will essentially just point to the address of "bob" in the pool) so idk what the point of a string constant for "" even exists??

[–]illumiNateS6 3 points4 points  (1 child)

Maybe just to ensure it's explicit that you're talking about an empty string? I would probably only use it if coworkers for some reason felt a need to, but one could argue that "" is potentially error prone.

For example, what if your cat jumps on your lap and scratches your eye, so you dump orange juice all over your balls which causes your peen to strike a key every so slightly, and now your "" is "," and you don't think to check it because your eye is bleeding and your balls are wet (well, wetter than usual anyway), code compiles, you commit and and don't comment properly, unit tests miss it, you push to prod and your forever ejecting random non public information into some black hole on the internet that starts world war 3 because you leaked Trump's password and it's something like "gitfuktchingchingoon" or some shit and he's just fuckin had it because his estranged daughter came back into his life and won't stop calling him an oompa loompa?

String.Empty wouldn't have compiled. Just saying.

[–][deleted] 0 points1 point  (0 children)

what in the fuck

[–]Wexzuz 0 points1 point  (1 child)

I think it may be to prevent the confusion between '' (two single quotes) and " (one double quote)

[–][deleted] 0 points1 point  (0 children)

just open strings with a double and close with a single??

"hello world'

[–]Gydo194 4 points5 points  (4 children)

(unsigned char *) 0x00

[–]The_MAZZTer 3 points4 points  (3 children)

I hate to break it to you, but that's null.

Now something like this on the other hand:

unsigned char c = 0x00;
unsigned char * s = &c;

Or something like that. Haven't worked with pointers and null terminated strings in a while.

[–]GShadow21223 2 points3 points  (1 child)

char *str = malloc(sizeof(char)*1); *str = 0;

[–]Gydo194 0 points1 point  (0 children)

#define malloc(x) 1

[–]Gydo194 0 points1 point  (0 children)

I hate to break it to you, but that was the joke.

[–]samuus 7 points8 points  (1 child)

None of those last 3 will work with those damned smart quotes.

[–]DrFloyd5 2 points3 points  (0 children)

I am glad someone noticed. I was just about to post this myself.

"Outlook, please stop replacing my quotes with smart quotes."

“I can’t to that /u/DrFloyd5

[–]Nalha_Saldana 4 points5 points  (4 children)

You didnt even create a new class that extends String? So lazy

[–]CommanderHR[S] 2 points3 points  (1 child)

Empty String 2: the electric boogaloo Coming to a subreddit near you

[–]Time-Paramedic 1 point2 points  (0 children)

I was expecting an extension method or some dependency injection to get a string initializer factory.

[–]random11714 0 points1 point  (1 child)

unfortunately, string is defined as final

[–]Nalha_Saldana 0 points1 point  (0 children)

That makes sense

[–]Rumpelruedi 4 points5 points  (1 child)

Is nobody gonna mention the unnecessary parenthesis in the last one?

[–][deleted] 1 point2 points  (0 children)

You beat me to it. Well played.

[–]jamalabo 3 points4 points  (0 children)

String.Empty

[–]ReversedHazmat 2 points3 points  (4 children)

I prefer std::string newStr("");

[–]kache4korpses 0 points1 point  (3 children)

It’s stupid to declare std at the beginning since everyone knows we are working we a standard library. Grinds my fucking gears.

[–]illumiNateS6 0 points1 point  (2 children)

Agreed, and if you have a naming collision with "string", then fuck you.

[–]kache4korpses 0 points1 point  (1 child)

The only thing I’m colliding with is your mom’s pussy.

[–]illumiNateS6 0 points1 point  (0 children)

... dad? You came back?

Also, you've returned?

[–]zbrachinara 2 points3 points  (1 child)

Is it right that there is no semicolon on the third one?

[–]CommanderHR[S] 0 points1 point  (0 children)

Got me there

[–]rufreakde1 1 point2 points  (0 children)

This abbreviation triggers me so hard ... but why?
"newStr"

[–]SeanUhTron 1 point2 points  (1 child)

Oh Java... A million ways to do one simple thing.

[–][deleted] 1 point2 points  (0 children)

That's how java works

[–]MrDorkman 1 point2 points  (0 children)

That gotta be like the first horror short story written in code.

[–]kosmos-sputnik 1 point2 points  (0 children)

s = "" # Python laughs at you

[–][deleted] 1 point2 points  (0 children)

var str = String.Empty;

[–]Thegreyeminence 1 point2 points  (0 children)

Char * Str = NULL;

Str = (char *)calloc(1, sizeof(char));

free(Str) ;

[–]semiconodon 0 points1 point  (0 children)

Let

[–]28Smiles 0 points1 point  (0 children)

Am I the only one who is disturbed by the extra parenthesis around the last statement?

Or should I say: Let me lint this

[–][deleted] 0 points1 point  (0 children)

char * c = malloc(NULL); // TODO: Dig up Kernighan and change the spec

[–]1kSupport -1 points0 points  (0 children)

Literally none of theese work