Final (Java): Difference between revisions

Content deleted Content added
Undid revision 1010925661 by Mouad404 (talk)
Line 193:
Further, because C and C++ expose pointers and references directly, there is a distinction between whether the pointer itself is constant, and whether the data pointed to by the pointer is constant. Applying <code>const</code> to a pointer itself, as in <code>SomeClass * const ptr</code>, means that the contents being referenced can be modified, but the reference itself cannot (without casting). This usage results in behaviour which mimics the behaviour of a <code>final</code> variable reference in Java. By contrast, when applying const to the referenced data only, as in <code>const SomeClass * ptr</code>, the contents cannot be modified (without casting), but the reference itself can. Both the reference and the contents being referenced can be declared as <code>const</code>.
 
==C# analoganalogs for final keyword ==
C# iscan be considered theas languagesimilar thatto isJava, tooin closeterms toof Java;its language features and basic syntax: Java has JVM, C# has .Net Framework; Java has bytecode, C# has MSIL; Java has no pointers (real memory) support, C# is the same.
Regarding the final keyword, which is the variable that is allowed to be assigned once, C# has two solutions:
 
Regarding the final keyword, C# has two related keywords:
For methods and classes use <code>sealed</code>, however for the variables use <code>readonly</code> <ref>[https://stackoverflow.com/questions/1327544/what-is-the-equivalent-of-javas-final-in-c What is the equivalent of Java's final in C#?]</ref>
# The equivalent keyword for methods and classes is <code>sealed</code>
 
For# methodsThe andequivalent classes use <code>sealed</code>, howeverkeyword for the variables useis <code>readonly</code> <ref>[https://stackoverflow.com/questions/1327544/what-is-the-equivalent-of-javas-final-in-c What is the equivalent of Java's final in C#?]</ref>
Note that the difference between <code>const</code> and <code>readonly</code> is that const is evaluated at compile time, however, readonly is evaluated at runtime, thus can has a runtime expression to be calculated and fixed at runtime.
 
Note that thea key difference between the C/C++ derived keyword <code>const</code> and the C# keyword <code>readonly</code> is that <code>const</code> is evaluated at compile time, however,while <code>readonly</code> is evaluated at runtime, and thus can has a runtime expression tothat beis only calculated and fixed later (at runtime).
 
==References==