Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Java print quotes, like "Hello"?

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("").

like image 679
Roy Avatar asked Oct 02 '10 06:10

Roy


People also ask

How do you print quotes in Java?

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.

How do you code quotes in Java?

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.

How do I print a printf quote?

Using \" escape sequence in printf, we can print the double quotes ("").

How do you print words in Java?

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!".


2 Answers

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:

  • Carriage return and newline: "\r" and "\n"
  • Backslash: "\\"
  • Single quote: "\'"
  • Horizontal tab and form feed: "\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 xs 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)

like image 51
Stephen C Avatar answered Sep 21 '22 14:09

Stephen C


char ch='"';  System.out.println(ch + "String" + ch); 

Or

System.out.println('"' + "ASHISH" + '"'); 
like image 22
Akmishra Avatar answered Sep 18 '22 14:09

Akmishra