I usually develop in C #, but I haven't got an interface for C # in the API to use yet, so I started developing in Java. I haven't touched Java so much, so I tried to summarize "xxxx" in C # as "xxxx" in Java. It focuses on reserved words and implementation methods that I often use. Therefore, not all differences are covered.
I tried to summarize the differences between reserved words in C # and Java, and the points that do not change. Let's compare the same and similar items side by side.
difference | C# | Java | Remarks |
---|---|---|---|
namespace | package | ||
using | import | ||
None | class | class | |
: | extends | Class inheritance | |
None | interface | interface | |
None | abstract | abstract | |
virtual | None | If you want the function to be overrideable at the inheritance destination, C#はvirtualを付与、JavaはNone | |
override | @Override | C#Is included in the definition, but Java is an annotation(C#Attribute, Attribute) | |
readonly | final | Read-only field | |
const | final | constant | |
sealed | final | Class inheritance prohibited, Java also used as function override prohibited | |
None | Enum | Enum | Enum |
struct | None | Structure |
Java didn't have "struct", "virtual". There seems to be no problem because "struct" can be substituted with class like that, but it seems to be a little troublesome without "virtual". For "virtual", dig into the end of the page with the title below. -"C # virtual and Java final"
Also, Java's "final" contained multiple C # features. Not only that, but there is only Java usage, so I'll dig into this difference at the end of the page with the title below. ・ "Differences in constant declaration and setting method"
I tried to summarize the implementation method in C #, the implementation method in Java, and the implementation method that can only be found in either. Let's compare the same items that I thought were similar side by side.
Thing you want to do | C# | Java |
---|---|---|
Attribute self-made | Attribute inheritance | @Class definition in interface |
Extension method | public static class | None |
Default implementation of interface | ~~None~~ C#8.0 (.NET Core 3.0)Add with |
Inherit interface and define members by default |
Automatic release of resources | using(Stream stream = new Stream()) | try-with-resources |
Type parameter constraints | where T: int | <T extends Number> |
Above example)void Test<T>(T arg) | void Test<T>(T arg) where T : int | <T extends Number> void Test(T arg) |
Nullable type declaration | Nullable<T>,T? | Optional<T> |
Lambda expression | ()=>{ } | ()->{ } |
No callback arguments | Action | Runnable |
Same as above 1 argument | Action<T> | Consumer<T> |
Same as above 2 arguments | Action<T1,T2> | BiConsumer<T,U>(Up to 2 arguments) |
Same as above There is a return value | Function<R> | Supplier<T> ~~API specificationFunctional interfaceNo limit to see~~ |
Same as above, 1 argument, with return value | Function<T,R> | Function<T,R> |
Same as above, 2 arguments, with return value | Function<T1,T2,R> | BiFunction<T,U,R>(Up to 2 arguments) |
Regarding the functions listed this time, it was found that both C # and Java have the same functions, although the usability is different.
Also, as you pointed out in the comment section, the default implementation function of interface has been added to C # since C # 8.0. I was confused about abstract and interface and implemented it in the latter, but I'm sure there are people who have the same implementation in the inheritance destination. ~~ I am ~~ I definitely want to make use of this.
Also, the usability of "Action" and "Function" in C #, so-called callbacks, seems to be quite different. For "callbacks", dig into the end of the page with the title below. ・ Differences in how to use callbacks
We will delve into the three listed in "Differences in reserved words" and "Differences in implementation methods".
In C #, "virtual" enables override (implemented as a virtual method to be exact), and Java prohibits override with "final". In other words, if you do not add "final" to a Java function, it will always be implemented in "virtual" in the sense of C #. It has been overridden before I knew it! ?? If you don't want to be in such a situation, you should definitely add "final". ..
The difference between C # and Java callbacks is roughly like this. If you understand this difference, you won't be confused by C # and Java.
--Java callbacks have an upper limit on the number of arguments, and C # can take up to 16 arguments.
--Java callbacks can't take numeric types as arguments, but C # can take all types as arguments. (* 1)
※1 The Java callbacks mentioned in this article can only take reference types as arguments. Therefore, unlike C #, Java cannot take a numeric type as an argument. ~~ I was confused because I couldn't set the value ~~ Instead, there is a callback for the numeric type, which is defined by the name below, so use this. ・ "Numeric type" Consumer ・ Obj "Numeric type" Consumer ・ "Numerical Function" ・ To "Numeric type" Function ・ To "Numerical type" BiFuction
We have summarized the differences when using C # "const", "readonly", and Java "final" as constants and read-only.
Constant declaration, assignment method | const | readonly | final |
---|---|---|---|
Declaration in a constant field | 〇 | 〇 | 〇 |
Assignment to a constant field other than the declaration location | × | 〇 ※1 | × |
Declaration in a local variable | 〇 | × | 〇 |
Assignment other than the declaration location in a local variable | × | × | 〇 |
Grant to argument | × | × | 〇 |
Type restrictions at the time of declaration | Yes * 2 | None | None |
Other than the above, the function of "final" is prohibited from reassignment as pointed out in the comment. C # basically doesn't have a corresponding feature because it can only be assigned in the declared location. If you force it, is it something like "read only" that can be declared even with a local variable? Of course, there is no such function, so I definitely want it in C #.
-Java (tm) Platform, Standard Edition 8 API specifications ・ Java final is dissected and all of the final is here !!
I've heard that C # and Java are similar because they have the same language base. As I researched, I felt that the reserved words were often the same, and that they were certainly similar. In addition, there were differences that I wouldn't notice unless I actually wrote the code (small differences such as reserved words coming and going), so I felt the importance of not only accumulating knowledge but also outputting. However, although it is written in a similar way, it may behave differently, so I would like to avoid fitting it when an error occurs.
Also, above all, information updates. .. Thank you to everyone who pointed out.
Recommended Posts