英文:
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<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
因为您使用了<
运算符传递了一个布尔表达式,所以它返回一个布尔值。您的打印语句会看到它像这样64是否小于4?
因此,它将返回一个布尔值。
英文:
It is returning a boolean because you have passed a boolean expression by using the <
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
类,out
是java.io.PrintStream
类的字段,封装了println
方法,其中一个重载接受一个boolean
参数,该参数是在调用System.out.println(64<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<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 < 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("64 < 4");
This will print out 64 < 4
Or to include the result:
System.out.println("64 < 4 = " + (64 < 4));
which will print out
64 < 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 – 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 = "example");
The result of assigning a value to string is a String value, thus the text "example" is printed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论