在c#中var关键字的一种用法是隐式类型声明。Java中var的等效语法是什么?
当前回答
我知道这是旧的,但为什么不创建一个var类,创建不同类型的构造函数,根据调用的构造函数,你会得到不同类型的var。您甚至可以内置方法将一种类型转换为另一种类型。
其他回答
一个简单的解决方案(假设你使用的是一个不错的IDE)是在所有地方输入'int',然后让它为你设置类型。
实际上我只是添加了一个名为var的类,所以我不需要输入不同的东西。
代码仍然过于冗长,但至少您不必键入它!
没有。唉,你必须输入完整的类型名。
编辑:在发布7年后,Java 10中添加了局部变量的类型推断(使用var)。
编辑:在发布6年之后,收集下面的一些评论:
The reason C# has the var keyword is because it's possible to have Types that have no name in .NET. Eg: var myData = new { a = 1, b = "2" }; In this case, it would be impossible to give a proper type to myData. 6 years ago, this was impossible in Java (all Types had names, even if they were extremely verbose and unweildy). I do not know if this has changed in the mean time. var is not the same as dynamic. variables are still 100% statically typed. This will not compile: var myString = "foo"; myString = 3; var is also useful when the type is obvious from context. For example: var currentUser = User.GetCurrent(); I can say that in any code that I am responsible for, currentUser has a User or derived class in it. Obviously, if your implementation of User.GetCurrent return an int, then maybe this is a detriment to you. This has nothing to do with var, but if you have weird inheritance hierarchies where you shadow methods with other methods (eg new public void DoAThing()), don't forget that non-virtual methods are affected by the Type they are cast as. I can't imagine a real world scenario where this is indicative of good design, but this may not work as you expect: class Foo { public void Non() {} public virtual void Virt() {} } class Bar : Foo { public new void Non() {} public override void Virt() {} } class Baz { public static Foo GetFoo() { return new Bar(); } } var foo = Baz.GetFoo(); foo.Non(); // <- Foo.Non, not Bar.Non foo.Virt(); // <- Bar.Virt var bar = (Bar)foo; bar.Non(); // <- Bar.Non, not Foo.Non bar.Virt(); // <- Still Bar.Virt As indicated, virtual methods are not affected by this. No, there is no non-clumsy way to initialize a var without an actual variable. var foo1 = "bar"; //good var foo2; //bad, what type? var foo3 = null; //bad, null doesn't have a type var foo4 = default(var); //what? var foo5 = (object)null; //legal, but go home, you're drunk In this case, just do it the old fashioned way: object foo6;
你可以通过JetBrains查看Kotlin,但它是val。不是var。
JEP - JDK增强提案
http://openjdk.java.net/jeps/286
JEP 286:局部变量类型推断
作者Brian Goetz
// Goals:
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>
一般来说,你可以为任何类型使用Object类,但是你以后必须做类型强制转换!
如:-
Object object = 12;
Object object1 = "Aditya";
Object object2 = 12.12;
System.out.println(Integer.parseInt(object.toString()) + 2);
System.out.println(object1.toString() + " Kumar");
System.out.println(Double.parseDouble(object2.toString()) + 2.12);
推荐文章
- 套接字的连接超时和读超时之间的区别是什么?
- Java整数到字节数组
- 如何设置Windows环境下Java的环境变量
- Java Swing revalidate() vs repaint()
- Java中文件中的行数
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 对于一个布尔字段,它的getter/setter的命名约定是什么?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 如何计算一个元素在列表中出现的次数
- c++中类似于java的instanceof
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何POST表单数据与Spring RestTemplate?
- Mockito中检测到未完成的存根
- 我应该如何复制字符串在Java?