How can I make Java print "Hello"
?
When I type System.out.print("Hello");
the output will be Hello
. What I am looking for is "Hello"
with the quotes(""
).
You can print double quotes in java by escape double quotes using backslash character( \ ). When you print on console using System. out. println, you use double quotes and whatever value inside double quotes is printed.
However, note that in a Java program, a double quotation mark requires the backslash escape character. Example: -- a single quotation mark is the escape character -- for a single quotation mark VALUES 'Joe''s umbrella' -- in ij, you don't need to escape the double quotation marks VALUES 'He said, "hello!"' n = stmt.
Using \" escape sequence in printf, we can print the double quotes ("").
You can print any text you want with the command, as long as the command System. out. println("arbitrary text"); — i.e., System dot out dot println open parenthesis ( "the text" close parenthesis ) and semicolon ; remains unchanged. The command below will print the text "Hello there!".
System.out.print("\"Hello\"");
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:
"\r"
and "\n"
"\\"
"\'"
"\t"
and "\f"
The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.
It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx
where the x
s are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.
See also:
The Oracle Java Tutorial: Numbers and Strings - Characters
In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)
char ch='"'; System.out.println(ch + "String" + ch);
Or
System.out.println('"' + "ASHISH" + '"');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With