如何在java中打印void

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18760941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:49:50  来源:igfitidea点击:

how to print void in java

javavoid

提问by upog

The below statement returns void

下面的语句返回 void

Pattern lazy = Pattern.compile("X??");
Matcher lazyMatcher = lazy.matcher("X");
if (lazyMatcher.matches()) {
  System.out.println(lazyMatcher.group());
}

Is there way to print void in java.

有没有办法在java中打印void。

I tried below 2 statements , but does not help

我尝试了以下 2 条语句,但无济于事

System.out.println((String)lazyMatcher.group());
System.out.println(lazyMatcher.group().toString());

============================================================================= Updating

================================================== ============================ 更新

why am i getting String when i call below

为什么我在下面打电话时得到 String

System.out.println(lazyMatcher.group().getClass())  // returns string
System.out.println(lazyMatcher.group()) // returns void

采纳答案by Adam Arold

There is the Void.classyou can use. If you try Void.class.toString()it will return

Void.class你可以使用的。如果你尝试Void.class.toString()它会返回

class java.lang.Void

回答by Ben Green

void is not an object. All the void return type states is that nothing will be returned from the method. So the answer is no, there is no way to print void, as there is nothing to print.

void 不是一个对象。所有 void 返回类型状态是该方法不会返回任何内容。所以答案是否定的,没有办法打印 void,因为没有什么可打印的。

With regards to your edited question, you are getting a String because Matcher.group() returns a string not void. See the documentation

关于您编辑的问题,您得到一个字符串,因为 Matcher.group() 返回一个不为空的字符串。查看文档

回答by smerlung

void is nothing. You don't print it. If you wan't to print "void" you can do

空是什么。你不打印。如果你不想打印“void”,你可以做

System.out.println("void");

回答by Aneesh

You cant print something that isn't there.

你不能打印不存在的东西。