简体   繁体   English

Java和C#中的编译时与运行时

[英]Compile-time vs. Run-time, in Java and C#

I have read somewhere one of the main differences between Java and C++/C# is Java does something at Run-time and C# does something at compile-time. 我已经读到Java与C ++ / C#之间的主要区别之一是Java在运行时执行某些操作,而C#在编译时执行某些操作。 Is this true? 这是真的? If so could you explain this a bit more? 如果可以的话,您可以再解释一下吗?

In C#, I created a function which takes in two inputs and returns a random number (called RandomNumber(int x, int y) ), using the Random Object. 在C#中,我创建了一个函数,该函数接受两个输入并使用随机对象返回一个随机数(称为RandomNumber(int x, int y) )。 I then called this function twice in another function, expecting to get two difference values (two different random numbers). 然后,我在另一个函数中两次调用了此函数,期望得到两个差值(两个不同的随机数)。 However, I kept getting same values, which baffled me, since I can do the same thing in Java and expect to get different numbers. 但是,我一直获得相同的值,这使我感到困惑,因为我可以在Java中执行相同的操作,并且期望获得不同的数字。

I then decided, to remove my function - RandomNumber(int x, int y) and call the Random inside my other function, show below. 然后,我决定删除我的函数-RandomNumber(int x,int y)并在我的其他函数中调用Random,如下所示。

    Random random = new Random();
    int randomNum;
    int la;
    randomNum = random.Next(1, 10);
    numOne.Text = randomNum.ToString();
    la = random.Next(1, 10);

This generates two different random numbers. 这将生成两个不同的随机数。 Why is this the case? 为什么会这样呢?

The random number issue has nothing to do with compile-time or run-time. 随机数问题与编译时或运行时无关。 It has everything to do with where the Random class is instantiated. 它与实例化Random类的位置有关。

The Random class doesn't really generate true random numbers. Random类实际上不会生成真正的随机数。 They are generated based on a mathematical formula that includes the current date/time and several other bits of data. 它们是根据包括当前日期/时间和其他几位数据的数学公式生成的。

The following: 下列:

Random r = new Random(100)
for(int i = 0; i < 100; i++)
{
    Console.WriteLine(r.Netc().ToString());
}

will generate a series of numbers that looks random. 将生成一系列看起来随机的数字。

This: 这个:

for(int i = 0; i < 100; i++)
{
    Random r = new Random(100);
    Console.WriteLine(r.Next().ToString());
}

will produce the same numbers for a few loops, then a new series of similar numbers for a few loops. 将为几个循环生成相同的数字,然后为几个循环生成一系列新的相似数字。

This is because of the formula. 这是因为公式。 In the second example, each Random class is being created with very similar conditions, and will therefore produce the same number until the time changes enough to change the outcome. 在第二个示例中,将在非常相似的条件下创建每个Random类,因此将产生相同的数字,直到时间变​​化足以更改结果为止。

However, with the first example, there is only one Random class and in subsequent iterations of the loop, it knows to produce a different number, because it knows it just generated one in the last loop. 但是,在第一个示例中,只有一个Random类,并且在循环的后续迭代中,它知道产生一个不同的数字,因为它知道它只是在最后一个循环中产生了一个。

If your code is calling a function that declared a new Random object and then uses Random.Next then in the scope of your function, Random is a new object on each call. 如果您的代码正在调用一个声明了新的Random对象的函数,然后使用Random.Next,则在您的函数范围内,Random是每次调用的新对象。 Therefore, if your calls are within a few milliseconds, you'll get the same result. 因此,如果您的通话时间在几毫秒之内,您将获得相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM