system.out

Why does `System.out.println(null);` give “The method println(char[]) is ambiguous for the type PrintStream error”?

本秂侑毒 提交于 2019-11-27 17:22:04
问题 I am using the code: System.out.println(null); It is showing the error: The method println(char[]) is ambiguous for the type PrintStream Why doesn't null represent Object ? 回答1: There are 3 println methods in PrintStream that accept a reference type - println(char x[]) , println(String x) , println(Object x) . When you pass null , all 3 are applicable. The method overloading rules prefer the method with the most specific argument types, so println(Object x) is not chosen. Then the compiler

Why is printing “B” dramatically slower than printing “#”?

江枫思渺然 提交于 2019-11-27 09:55:01
I generated two matrices of 1000 x 1000 : First Matrix: O and # . Second Matrix: O and B . Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if(r.nextInt(4) == 0) { System.out.print("O"); } else { System.out.print("#"); } } System.out.println(""); } With this code, the second matrix took 259.152 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if(r.nextInt(4) == 0) { System.out.print("O"); } else { System.out.print("B"

Why my method being called 3 times after System.in.read() in loop

一世执手 提交于 2019-11-27 08:16:00
问题 I have started to learn Java, wrote couple of very easy things, but there is a thing that I don't understand: public static void main(String[] args) throws java.io.IOException { char ch; do { System.out.println("Quess the letter"); ch = (char) System.in.read(); } while (ch != 'q'); } Why does the System.out.println prints "Quess the letter" three times after giving a wrong answer. Before giving any answer string is printed only once. Thanks in advance 回答1: Because when you print char and

What happens to “System.out.println()” in executable jar?

若如初见. 提交于 2019-11-27 07:39:24
问题 Suppose I've created an executable jar from a code where I have used System.out.println() When we run the executable jar, there is no console. So, what happens to this line? How does java handle this situation? EDIT 01 : NOTE : The situation is when I don't use a console to run the jar nor associate any console with it anyhow. EDIT 02 : Making things clearer: I know that nothing will be printed anywhere as there is no console..! I want to know how java handle this line in this case? Is this

how do I use System.out.printf?

梦想的初衷 提交于 2019-11-27 07:24:11
问题 My teacher wants us to display our values in the format method (at the very bottom) but the problem is we had a sub and she didn't show us how to use it and my teacher is being less than helpful. Any advice or help would be greatly appreciated. public class SphereCalculations { public static void main(String[] args) { //define variables double circumference = 0; double area = 0; double volume = 0; double surfacearea = 0; double radius = 0; Scanner scan = new Scanner (System.in); DecimalFormat

Why don't we close `System.out` Stream after using it?

巧了我就是萌 提交于 2019-11-26 23:28:02
问题 I just want to know, we usually close streams at the end, but why don't we close System.out PrintStream with System.out.close() ? 回答1: If you close it you will no longer be able to write to the console, so let's leave this task to the VM when the process terminates. You should only close streams that you own or have manually created. System.out is out of your control, so leave it to the creator to take care of. 回答2: because we didn't open it the VM did and it's his job to close it unless

Differences between System.out.println() and return in Java

大城市里の小女人 提交于 2019-11-26 23:02:23
I'm trying to understand the difference, and benefits of using System.out.println() vs. return blah in a method. It seems like System.out.println() is used to display static information, and return is a value returned from the method. Yet I'm seeing examples like the one below, where a function is used within the System.out.println() statement System.out.println(name.substring(1, 3)); When is it right to use System.out.println() and return . Is it that return can be used by another piece of code later, whereas System.out.println() cannot? Your last sentence is effectively correct, but the

How does System.out.print() work?

房东的猫 提交于 2019-11-26 20:22:31
I have worked with Java for a quite a long time, and I was wondering how the function System.out.print() works. Here is my doubt: Being a function, it has a declaration somewhere in the io package. But how did Java developers do that, since this function can take in any number of arguments and any argument types no matter how they are arranged? e.g: System.out.print("Hello World"); System.out.print("My name is" + foo); System.out.print("Sum of " + a + "and " + b + "is " + c); System.out.print("Total USD is " + usd); No matter what is the datatype of variables a, b, c, usd, foo or how they are

Why is printing “B” dramatically slower than printing “#”?

我是研究僧i 提交于 2019-11-26 14:57:02
问题 I generated two matrices of 1000 x 1000 : First Matrix: O and # . Second Matrix: O and B . Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if(r.nextInt(4) == 0) { System.out.print("O"); } else { System.out.print("#"); } } System.out.println(""); } With this code, the second matrix took 259.152 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for

How to color System.out.println output? [duplicate]

断了今生、忘了曾经 提交于 2019-11-26 07:53:00
问题 This question already has an answer here: How to print color in console using System.out.println? 10 answers How can I color Java output? For example in C and other languages I can use ANSI-escape like \\033[0m to do this. But in Java it doesn\'t work. public static void main(String[] x) { System.out.println(\"\\033[0m BLABLA \\033[0m\\n\"); } 回答1: No, but there are third party API's that can handle it http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html Edit: of course