在c#中var关键字的一种用法是隐式类型声明。Java中var的等效语法是什么?
当前回答
这个特性现在在Java SE 10中可用。静态的、类型安全的变量终于进入了java世界:)
来源:https://www.oracle.com/corporate/pressrelease/java - 10 - 032018. - html
其他回答
JDK 10将支持它。甚至可以在早期访问版本中看到它的作用。
JEP 286:
增强Java语言,将类型推断扩展到带有初始化式的局部变量声明。
所以现在不要写:
List<> list = new ArrayList<String>();
Stream<> stream = myStream();
你写的:
var list = new ArrayList<String>();
var stream = myStream();
注:
Var现在是一个保留类型名 Java仍然致力于静态类型! 它只能在局部变量声明中使用
如果你想尝试一下,而不是在本地系统上安装Java,我创建了一个安装了JDK 10的Docker镜像:
$ docker run -it marounbassam/ubuntu-java10 bash
root@299d86f1c39a:/# jdk-10/bin/jshell
Mar 30, 2018 9:07:07 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
| Welcome to JShell -- Version 10
| For an introduction type: /help intro
jshell> var list = new ArrayList<String>();
list ==> []
在Java 10中,等价的是……var。
Lombok支持var,但仍处于实验阶段:
import lombok.experimental.var;
var number = 1; // Inferred type: int
number = 2; // Legal reassign since var is not final
number = "Hi"; // Compilation error since a string cannot be assigned to an int variable
System.out.println(number);
这里有一个在IntelliJ IDEA中使用它时要避免的陷阱。它似乎工作如预期,但包括自动完成和一切。除非有一个“非黑客”的解决方案(例如,由于JEP 286:局部变量类型推断),这可能是您目前最好的选择。
注意,在不修改或创建Lombok .config的情况下,Lombok也支持val。
我知道这是旧的,但为什么不创建一个var类,创建不同类型的构造函数,根据调用的构造函数,你会得到不同类型的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;
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?