我正在学习Java编程,我在下面的代码中有一个问题。

huangapple go评论136阅读模式
英文:

I am learing java programming I have a question in below code

问题

package Basics;

public class ch2_operators {
    public static void main(String[] args) {
        System.out.println(64 < 6);
    }
}
英文:
package Basics;

public class ch2_operators {
    public static void main(String[] args) {
        System.out.println(64&lt;6);
    }
}

Why does my print function give me output as "false" in the above code the task of giving output as true and false is with boolean datatype right?

I don't know am I right or wrong I just need someone to explain me this.

答案1

得分: 2

因为您使用了&lt;运算符传递了一个布尔表达式,所以它返回一个布尔值。您的打印语句会看到它像这样64是否小于4?

因此,它将返回一个布尔值。

英文:

It is returning a boolean because you have passed a boolean expression by using the &lt; operator. Your print statement will see it like this is 64 less than 4?

So it would return a boolean

答案2

得分: 1

根据Java编程语言文档中的java.lang.System类,outjava.io.PrintStream类的字段,封装了println方法,其中一个重载接受一个boolean参数,该参数是在调用System.out.println(64&lt;6)时传递的比较类型。

英文:

According to Java programming language documentation of class java.lang.System out is a field of type java.io.PrintStream class that encapsulates the println method with one of its overloads accepting a boolean argument that is the type of comparison passed in when calling System.out.println(64&lt;6)

答案3

得分: 1

不确定您试图实现什么,但您只是打印出了64 < 4的结果,结果是false,因为64不小于4。

我将假设您实际上想要打印出等式,而不是结果,如果是这样,您将不得不将它打印为一个String,方法是用双引号括起来,就像这样:

System.out.println("64 < 4");

这将打印出64 < 4

或者包括结果:

System.out.println("64 < 4 = " + (64 < 4));

这将打印出:

64 < 4 = false
英文:

Note sure what you were trying to achieve but you're simply printing out the result of 64 &lt; 4 which is false because 64 is not less than 4.

I'll assume that you're actually trying to print out the equation rather than the result in which case you'll have to print it out as a String by putting it in double quotes like this:

System.out.println(&quot;64 &lt; 4&quot;);

This will print out 64 &lt; 4

Or to include the result:

System.out.println(&quot;64 &lt; 4 = &quot; + (64 &lt; 4));

which will print out

64 &lt; 4 = false

答案4

得分: 0

Correct, a Boolean data-type is used for depicting true or false values.

The reason your code works is because the result of your statement produces a boolean value.

This is similar to typing the following.

String string;
System.out.println(string = "example");

The result of assigning a value to string is a String value, thus the text "example" is printed.

英文:

> '... the task of giving output as true and false is with boolean datatype right? ...'

Correct, a Boolean data-type is used for depicting true or false values.
Wikipedia &ndash; Boolean data type.

> '... Why does my print function give me output as "false" ...'

The reason your code works is because the result of your statement produces a boolean value.

This is similar to typing the following.

String string;
System.out.println(string = &quot;example&quot;);

The result of assigning a value to string is a String value, thus the text "example" is printed.

huangapple
  • 本文由 发表于 2023年7月10日 17:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定