{"id":1075,"date":"2019-07-29T02:21:13","date_gmt":"2019-07-29T02:21:13","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=1075"},"modified":"2019-08-20T17:24:45","modified_gmt":"2019-08-20T17:24:45","slug":"character-in-java","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-java-tutorial\/java-basic-syntax\/primitive-data-types\/character-in-java\/","title":{"rendered":"Character in Java"},"content":{"rendered":"\n<p>If you are dealing with a single character value, you can store it in <strong>char<\/strong> variable. A single character value is a character inside two quotes. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">char mykey = 'a';<\/pre>\n\n\n\n<p>A char variable can accept <strong>ASCII<\/strong> and <strong>UNICODE<\/strong> character. The example above was the ascii character which their numeric value is a number between 0 two 255.  <\/p>\n\n\n\n<p>Save following content in a .java file called\u00a0TryChar<strong>.java<\/strong>. Note\u00a0that the file name is equivalent to your class name\u00a0TryChar.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class TryChar {\n  public static void main(String[] args) {\n     char tryChar1 = 'A';\n     System.out.print(\"tryChar1 char value: \");\n     System.out.println(tryChar1);\n     System.out.print(\"tryChar1 ascii code: \");\n     System.out.println((int) tryChar1);\n\n     char tryChar2 = '\u00df';\n     System.out.print(\"tryChar2 value: \");\n     System.out.println(tryChar2);\n     System.out.print(\"tryChar2 ascii code: \");\n     System.out.println((int) tryChar2);\n\n     char tryChar3 = 124;\n     System.out.print(\"tryChar3 value: \");\n     System.out.println(tryChar3);\n     System.out.print(\"tryChar3 ascii code: \");\n     System.out.println((int) tryChar3);\n\n     char tryChar4 = '\\u2142';\n     System.out.print(\"tryChar4 value: \");\n     System.out.println(tryChar4);\n     System.out.print(\"tryChar4 Unicode code: \");\n     System.out.println((int) tryChar4);\n  }\n}\n\n<\/pre>\n\n\n\n<p>Using\u00a0<strong>javac<\/strong>\u00a0command compile your .java file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ javac TryChar.java<\/pre>\n\n\n\n<p>And using\u00a0<strong>java<\/strong>\u00a0command run it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java TryChar\n tryChar1 char value: A\n tryChar1 ascii code: 65\n tryChar2 value: \u00df\n tryChar2 ascii code: 223\n tryChar3 value: |\n tryChar3 ascii code: 124\n tryChar4 value: \u2142\n tryChar4 Unicode code: 8514<\/pre>\n\n\n\n<p>In Java, a <strong>char<\/strong> variable is technically a two bytes (16 bits) variable type that you can assign a single unicode character via the character symbol inside two quotes like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> char tryChar2 = '\u00df';<\/pre>\n\n\n\n<p>or assign a number value like this that represent <strong>pipe<\/strong> | character:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">char tryChar3 = 124;<\/pre>\n\n\n\n<p>or you can assign a unicode character:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">char tryChar4 = '\\u2142';<\/pre>\n\n\n\n<p>That represents this character symbol <strong>\u2142<\/strong> with a decimal value of <strong>8514<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are dealing with a single character value, you can store it in char variable. A single character value is a character inside two quotes. Here is an example: char mykey = &#8216;a&#8217;; A char variable can accept ASCII and UNICODE character. The example above was the ascii character which their numeric value is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":951,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-1075","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":10,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1075\/revisions"}],"predecessor-version":[{"id":1089,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1075\/revisions\/1089"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/951"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}