Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Java's "final" for translation to C#

Tags:

java

c#

final

I am not a Java programmer. I read the documentation on "final", and understand it to mean "a variable's value may be set once and only once."

I am translating some Java to C#. The code does not execute as expected. I tried to figure out why, and found some uses of final that don't make sense.

Code snippet 1:

final int[] PRED = { 0, 0, 0 };
...
PRED[1] = 3;

Will PRED[1] be 0 or 3?

Code snippet 2:

final int[] PRED = new int[this.Nf];
for (int nComponent = 0; nComponent < this.Nf; nComponent++) {
    PRED[nComponent] = 0;
}
...
PRED[1] = 3;

Surely PRED[0] will remain as 0?

like image 757
IamIC Avatar asked May 02 '12 10:05

IamIC


People also ask

What does final mean in C#?

In c# there is no keyword like “final” but the same thing is achieved by keyword “sealed“ . A class which is marked by keyword sealed cannot be inherited. If you have ever noticed, structs are sealed. You cannot derive a class from a struct.

When we apply the final modifier sealed in C #) on a method it results in?

When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.

What does final method?

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .

Is final keyword useful?

The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier". You will learn more about these in the Java Modifiers Chapter.


2 Answers

I think you have a misunderstanding of the final keyword semantic when it is applied to arrays in Java.

In both Java examples the arrays will remain unchanged, but their elements may be changed. All your assignments will be executed correctly, and the values stored in the array will get changed. However, if you try

final int[] PRED = new int[this.Nf];
// some other code
PRED = new int[123]; // <<== Compile error

you are going to see a compile error.

When translating your code to C#, you may need to translate final either as sealed (when it is applied to a class), or as readonly (when it is applied to a member). The semantic of readonly arrays in C# and final arrays in Java are the same: your program cannot reassign the array, but it can freely modify its elements.

Finally, there is a Java-specific case when final is used where you wouldn't need it in C# at all: when you need to use a variable inside a method of an anonymous local class in Java, you must make that variable final. Since C# does not have anonymous local classes, you would need to translate that piece of code with something else, perhaps with anonymous delegates. Such delegates are not restricted to using readonly variables.

like image 120
Sergey Kalinichenko Avatar answered Sep 30 '22 12:09

Sergey Kalinichenko


The word you are looking for in C# is "const":

const int SOMETHING = 10;

Note that the SIZE should be constant as well.

Also, constants can only be of type int, bool, string, char, etc (basic types only).

So if you want something else, such as an array or a class, you can do:

static readonly int[] PRED = new int[this.Nf];

Static Readonly means exactly const, logically, except it is defined a little differently backstage, which allows you to play more freely with it. (When you CAN - you SHOULD define constants instead of static readonly's)

The array itself will not change during the run, so you will not be able to do this:

PRED = new int[3]; // Error
PRED = null; // Error
PRED = PRED; // Error

But you CAN change values INSIDE the PRED array:

PRED[0] = 123;

If you wish to have a readonly collection, you can use the ReadOnlyCollection object! If you also want that object to be constant, you can use the static-readonly combination (you can't use constant because it is a class), and you will get:

static readonly ReadOnlyCollection<int> PRED = new ReadOnlyCollection<int>(new[] {1, 2, 5, 6});

And then PRED will always be PRED, and will always be of size 4, and will always contain 1, 2, 5, 6.

PRED = PRED; // Error (static readonly)
PRED = null; // Error (static readonly)
PRED[1] = 0; // Error (ReadOnlyCollection enforces readonly on the elements)

int i = PRED[1]; // Still works, since you aren't changing the collection.
like image 28
SimpleVar Avatar answered Sep 30 '22 13:09

SimpleVar