Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Compare Java's 'var' with C#'s 'var'.

Compare Java's 'var' with C#'s 'var'.

for Oracle Dev Tour Japan in Osaka at 2018.05.14

50a1ae3d2f81998c69a579daeb59f055?s=128

masanori_msl

May 14, 2018
Tweet

Transcript

  1. Compare Java's 'var' With C#'s 'var' 2018.05.14 @Oracle Dev Tour

    Japan in Osaka by masanori_msl
  2. • 'Local-Variable Type Inference' (JEP286). http://openjdk.java.net/jeps/286 • It infers the

    type of the local variable from the right side value. About Java's 'var' 1
  3. About Java's 'var' 2 • It must be inferred the

    type from the right side value. // Compile error. var nullSample = null; // Compile error. var lambdaSample = () => { // Do something. }; // Compile error. var arraySample = {0, 2};
  4. About Java's 'var' 3 • It shouldn’t use with the

    diamond operators. var sampleList = new ArrayList<>(); ↓ var sampleList = new ArrayList<Object>();
  5. About Java's 'var' 4 • The type is inferred as

    implemented class. // List<String>. List<String> texts = new ArrayList<String>(); // ArrayList<String>. var texts2 = new ArrayList<String>();
  6. About Java's 'var' 5 • The variable of anonymous class

    can be declared with it. var person = new Object() { privateString name; public String getName(){ return name; } public void setName(String name){ this.name = name; } }; person.setName("masanori"); System.out.println(person.getName());
  7. • C#'s 'var' was implemented in version 3.0. • Most

    of the features are as same as Java's. About C#'s 'var'
  8. I have felt C# programmers are more actively seeking to

    use 'var' than Java programmers. Where does the difference come from? and should we use 'var'? Question
  9. • It improve readability. • It induces better naming. •

    It makes me be able to treat the type ambiguous. Merits of using 'var' 1
  10. Merits of using 'var' 2 public static void main( String[]

    args ){ // Even if the return value type is changed, // this code doesn't need modifications. for (var name: getNames()) { System.out.println(name); } } private static List<String> getNames(){ return new ArrayList<String>(); }
  11. Demerits of using 'var' 1 • It reduces readability. //

    There is no infomations(´・ω・`). var v = c.m(); But you should not name the variable with the type name. // Don’t do this. var stringNames = c.m();
  12. Demerits of using 'var' 2 • It risks type mismatch.

    // if the return value type is changed from float to int …? var point = getPoint(); So if the variable needs the precise type, you should write type explicitly.
  13. • These situations are not much different between Java and

    C#. • C#'s 'var' has been implemented since over 10 years ago. • I don’t know if the situation of Java also will become as same as C#'s. Where does the difference come from?
  14. I think we should use ‘var’ if there is no

    special reason. Because keeping code simple makes us to think other important parts of code. And the most important reason of my opinion is JetBrains ReSharper and Rider suggest using 'var' : ). So when I write C# code, I want to follow them. Should we use ‘var’ ?
  15. • Because using ‘var’ has some merits and demerits, so

    please use it properly. • If the variable needs the precise type, please write the type explicitly. • If your team has coding rules, please follow them or update them first. Summary
  16. Reference 1 ▪Java • Style Guidelines for Local Variable Type

    Inference in Java http://openjdk.java.net/projects/amber/LVTIstyle.html • [Java] Style Guidelines for Local Variable Type Inference in Java - Oracle Blogs 日本語のまとめ https://orablogs-jp.blogspot.jp/2018/03/style-guidelines-for-local-variable.html • JEP 286: Local-Variable Type Inference http://openjdk.java.net/jeps/286 • Episode 72. A very deep dive on Var, and unmodifiable collections with Stuart Marks (@stuartmarks) himself! http://www.javapubhouse.com/2018/04/episode-72-very-deep-dive-on-var-and.html • Java10のvarをどう使うか - きしだのはてな http://d.hatena.ne.jp/nowokay/20180326 • 数カ月後の自分へ。僕がどういうときにJava10のvarを使いたいと思っているか。 - Mitsuyuki.Shiiba http://bufferings.hatenablog.com/entry/2018/04/03/004050 • Java varメモ - Hishidama's Java var Memo http://www.ne.jp/asahi/hishidama/home/tech/java/var.html • 【JDK 10】varを使った型推論 - Qiita https://qiita.com/chooyan_eng/items/f01c336359bc08cfa4b0
  17. Reference 2 ▪C# • C# Coding Conventions (C# Programming Guide)

    - Microsoft Docs https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions • Effective C# https://play.google.com/store/books/details/Bill_Wagner_Effective_C_Covers_C_6_0_includes_Cont?id=pz2cDQAAQBAJ • [雑記] 型推論の是非 - C# によるプログラミング入門 - ++C++; // 未確認飛行 C http://ufcpp.net/study/csharp/sp3_var.html • C# Debate: When Should You Use var? - InfoQ https://www.infoq.com/news/2008/05/CSharp-var • 第3回 varによる変数宣言とコレクション初期化子(1/4) - @IT http://www.atmarkit.co.jp/fdotnet/csharp30/csharp30_03/csharp30_03_01.html • c# - Why does ReSharper want to use 'var' for everything? - Stack Overflow https://stackoverflow.com/questions/1873873/why-does-resharper-want-to-use-var-for-everything • Misuse of the 'var' keyword in C# - Brad Smith's Coding Blog https://www.brad-smith.info/blog/archives/336
  18. About me • Name:Masui Masanori • Work:Creating applications with Unity

    etc. • twitter:https://twitter.com/masanori_msl • blog:http://mslgt.hatenablog.com/