Dejas
Dejas

Reputation: 3621

How is this string output possible?

I am unsure of how the following output is possible in the Java language. Notice the presence of the double dash in the output:

String keyName = projectId.AsString + "-comments-" + creationMomentString + "-" + _rand.nextInt() +  ".txt";
System.out.println(creationMomentString);
System.out.println(_rand.getClass());
System.out.println(keyName);

output:

0000001466400377645
class java.security.SecureRandom
o7grn3qt-comments-0000001466400377645--874329600.txt


-------------------------------------^

Upvotes: 0

Views: 119

Answers (4)

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

its not a double dash (--) as of you thinking about. the function nextInt() of class

java.security.SecureRandom 

generate a Negative Unique Random Number which is -874329600.

So Your Complete String As File name will appered as o7grn3qt-comments-0000001466400377645--874329600.txt

like below abbreviation..

projectId.AsString + "-comments-" + creationMomentString + "-" + _rand.nextInt() +  ".txt";
   ||                     ||               ||                         ||              ||
o7grn3qt              -comments-    0000001466400377645     -    -874329600          .txt

Upvotes: 0

AlexD
AlexD

Reputation: 31

It's definitely a minus sign from a value returned by Random().nextInt(). In Javadoc we can see:

All 2^32 possible int values are produced with (approximately) equal probability.

Upvotes: 0

yshavit
yshavit

Reputation: 43391

Random.nextInt can return a negative. What you're seeing is the "-" you explicitly put in, followed by a negative number.

Upvotes: 2

Seelenvirtuose
Seelenvirtuose

Reputation: 20608

Well ...

The call to

 _rand.nextInt()

returns an integer value. Integers can be negative. For example it could be

-874329600

thus producing the output you got.

The first minus sign is the textual output of your string concatenation. The second minus sign is the sign of the number.

Upvotes: 8

Related Questions