Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compared to C#, Java's final is similar to const or readonly [duplicate]

Tags:

java

c#

Wondering if compared to C#, java's final is more similar to which? const or readonly?

like image 763
user705414 Avatar asked May 01 '11 02:05

user705414


People also ask

Is there a compare function in C?

In the C Programming Language, the strcmp function returns a negative, zero, or positive integer depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.

Can I use == to compare strings in C?

You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.

How do you compare sentences in C?

We compare the strings by using the strcmp() function, i.e., strcmp(str1,str2). This function will compare both the strings str1 and str2. If the function returns 0 value means that both the strings are same, otherwise the strings are not equal.

How do I compare strings in C?

The strcmp() function, is used to compare the strings (str1,str2). The strings str1 and str2 will be compared using this function. If the function returns a value 0, it signifies that the strings are equal otherwise, strings are not equal.


1 Answers

Interesting question,

Java's final keyword implies a few things:

  • You can only assign the value once
  • You must assign the variable in the constructor, or as the part of the declaration

C#'s readonly keyword applies basically the same restrictions.

For completeness - let's look at const:

  • You can only assign to it once as part of the declaration
  • It is inherently static -- there is only one of those for all N instances of your class

So -- I'd say final is more similar to readonly.

-- Dan

like image 153
debracey Avatar answered Sep 26 '22 13:09

debracey