Summarize the differences between C # and Java writing

Preface

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.

Differences between C # and Java reserved words and implementation methods

Differences in reserved words

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"

Difference in mounting 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

Dive into the similarities and differences between C # and Java

We will delve into the three listed in "Differences in reserved words" and "Differences in implementation methods".

C # virtual and Java final

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". ..

Differences in how to use callbacks

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

Differences in constant declaration and setting method

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 #.

Referenced page

-Java (tm) Platform, Standard Edition 8 API specificationsJava final is dissected and all of the final is here !!

Finally

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

Summarize the differences between C # and Java writing
Differences in writing Java, C # and Javascript classes
Differences between "beginner" Java and Kotlin
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
Differences between Java and .NET Framework
Think about the differences between functions and methods (in Java)
Let's sort out the differences between Java substring and C # Substring, and how to port them.
[Java] Differences between instance variables and class variables
[Java] Understand the difference between List and Set
A note on the differences between interfaces and abstract classes in Java
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Java language from the perspective of Kotlin and C #
What is the difference between Java EE and Jakarta EE?
[Java] Difference between == and equals
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
C # and Java Overrides Story
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
I tried to summarize the basics of kotlin and java
Difference between Java and JavaScript (how to find the average)
[Java] Check the difference between orElse and orElseGet with IntStream
Differences in how to handle strings between Java and Perl
[Java] Difference between Hashmap and HashTable
A person writing C ++ tried writing Java
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
[Java] Relationship between H2DB and JDBC
Reading and writing Java basic files
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
Encrypt with Java and decrypt with C #
[Java] Difference between length, length () and size ()
The relationship between strict Java date checking and daylight savings time
About the relationship between the Java String equality operator (==) and initialization. Beginners
I tried to summarize the methods of Java String and StringBuilder
[Java] What is the difference between form, entity and dto? [Bean]
Conversion between Kotlin nullable and Java Optional
Relationship between kotlin and java access modifiers
Difference between final and Immutable in Java
java core: HotSpot compiler and C heap
Link Java and C ++ code with SWIG
Differences between preface and postfix of operators
The content of the return value of executeBatch is different between 11g and 12c
[For beginners] Difference between Java and Kotlin
Interpret the relationship between Java methods and arguments into a biochemical formula
[Java] Reading and writing files with OpenCSV
Get a rough idea of the differences between protocols, classes and structs!
[Note] Cooperation between Java and DB (basic)
[Java 7] Divide the Java list and execute the process
About the difference between irb and pry
The story of writing Java in Emacs
Differences between Ruby strings and symbols [Beginner]
[Java] Difference between Intstream range and rangeClosed
Understand the difference between int and Integer and BigInteger in java and float and double
[Java8] Search the directory and get the file
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
HashMap # putAll () behaves differently between Java 7 and Java 8