{"id":89388,"date":"2025-02-03T18:00:09","date_gmt":"2025-02-03T12:30:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89388"},"modified":"2025-02-03T11:05:33","modified_gmt":"2025-02-03T05:35:33","slug":"java-string-charat-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/","title":{"rendered":"Java String charAt() Method"},"content":{"rendered":"<p>We are going to discuss the String charAt() function in Java.<\/p>\n<p>The Java String returns the character in a string at the given position charAt(int index) function. The index value should fall between 0 and (length of string-1).<\/p>\n<h2>Explanation<\/h2>\n<ul>\n<li>The character at the given index in a string is returned by the charAt() function.<\/li>\n<li>The first character&#8217;s index is 0, the second character&#8217;s index is 1, and so forth.<\/li>\n<li>The charAt() method of the Java String class returns a character value at the specified index.<\/li>\n<li>The string&#8217;s length, n, is represented by the index number, which ranges from 0 to n-1.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public char charAt(int index)\r\n\r\nInternal implementation\r\npublic char charAt(int index) {    \r\n       If (index &gt;= value.length || (index 0))   \r\n           throw new StringIndexOutOfBoundsException(index);    \r\n       }    \r\n       return value[index];    \r\n   }<\/pre>\n<h3>Parameter<\/h3>\n<p><strong>Index &#8211;<\/strong> An int integer that represents the character&#8217;s index to return<\/p>\n<ul>\n<li>The procedure accepts the parameter index. The initial index is zero. It gives a character from a string at a given index position.<\/li>\n<li>If the index is bigger than this string length or a negative number, a StringIndexOutOfBoundsException is thrown.<\/li>\n<\/ul>\n<h3>Return Value<\/h3>\n<p><strong>Returns:<\/strong> The char value for this string at the given index.<br \/>\nIndex 0 contains the first character value.<\/p>\n<h4>Signature<\/h4>\n<ul>\n<li>The public method charAt() has an access modifier.<\/li>\n<li>It takes an int index as a parameter.<\/li>\n<li>Value returned &#8211; This function returns a char value in the String object at a specific index.<\/li>\n<\/ul>\n<h4>Specified by<\/h4>\n<p>CharSequence interface, located inside java.lang package.<\/p>\n<h4>Exceptions<\/h4>\n<ul>\n<li>If the index is negative or longer than the string&#8217;s length, an exception called StringIndexOutOfBounds is thrown.<\/li>\n<li>The StringIndexOutOfBoundsException exception is thrown when a negative integer or an out-of-bounds positive integer is given while calling Java&#8217;s charAt() method.<\/li>\n<\/ul>\n<h4>Diagram<\/h4>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/java-String-charAt-diagram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89502\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/java-String-charAt-diagram.webp\" alt=\"java String charAt() diagram\" width=\"400\" height=\"190\" data-wp-editing=\"1\" \/><\/a><\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CharAt{  \r\npublic static void main(String args[]){  \r\nString name=\"Greatest\";  \r\nchar ch=name.charAt(3);\r\nSystem.out.println(\"The Character is:\",ch);  \r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe Character is a.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CharAtExample{  \r\npublic static void main(String args[]){  \r\nString name=\"Tremendous\";  \r\nchar ch=name.charAt(10);\r\nSystem.out.println(ch);  \r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>String index out of range:<\/strong> 10<br \/>\nStringIndexOutOfBoundsException has occurred.<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CharAtExample3 {  \r\n    public static void main(String[] args) {  \r\n    String str = \"Great think takes time\";      \r\n    int strLength = str.length();      \r\n    System.out.println(\"First Character is: \"+ str.charAt(0));      \r\n    System.out.println(\"Last Character is: \"+ str.charAt(strLength-1));      \r\n    }  \r\n}<\/pre>\n<p><strong>Output :<\/strong><br \/>\n<strong>First Character is:<\/strong> G<br \/>\n<strong>Last Character is:<\/strong> e<\/p>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CharAt{  \r\n    public static void main(String[] args) {  \r\n        String str = \"Blue is our favourite colour\";  \r\n        int count = 0;  \r\n        for (int i=0; i&lt;=str.length()-1; i++) {  \r\n            if(str.charAt(i) == 'o') {  \r\n                count++;  \r\n            }  \r\n        }  \r\n        System.out.println(\"Frequency of o is: \"+count);  \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Frequency of o is:<\/strong> 3<\/p>\n<h3>Difference between charAt() and indexOf()<\/h3>\n<ul>\n<li>The &#8220;charAt()&#8221; method returns the character to a string&#8217;s given index.<\/li>\n<li>The &#8220;indexOf()&#8221; method returns the first occurrence of the specified character(s) or substring in the string.<\/li>\n<li>The latter method can also be tailored to start your search from the selected index.<\/li>\n<\/ul>\n<h3>Used to find<\/h3>\n<ul>\n<li>Print All Characters of the String<\/li>\n<li>Print the First and Last Characters of a String<\/li>\n<li>Print Characters Presented at Odd Positions<\/li>\n<li>Count the Number of Vowels in a String<\/li>\n<li>Count the Occurrence<\/li>\n<\/ul>\n<h3>Importance of String charAt()<\/h3>\n<ul>\n<li>The parameter for this Java method is always of the int type.<\/li>\n<li>This method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0.<\/li>\n<li>The IndexOutOfBounds Exception issue is raised if the index value is greater than the length of the string or negative.<\/li>\n<li>The string_length-1 to 0 range must be used as the index range.<\/li>\n<\/ul>\n<h3>Advantages of String charAt()<\/h3>\n<ul>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Indexing &#8211;<\/strong> It is used to specify the characters at a specific index.<\/span><\/li>\n<li><strong>Character access &#8211;<\/strong> It easily accesses the individual character within the string.<\/li>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Efficiency &#8211;<\/strong> It mainly accesses the character at a specific index of 0.<\/span><\/li>\n<\/ul>\n<h3>Disadvantages of String charAt()<\/h3>\n<ul>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Index error &#8211;\u00a0<\/strong>Sometimes, it provides an error when accessing the index.<\/span><\/li>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Limited function &#8211;<\/strong> It only accesses the character and does not provide other strings or libraries.<\/span><\/li>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Length depends &#8211;<\/strong>It only depends on the length of the string, and if handled incorrectly, it occurs in error.<\/span><\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We extensively learn the Java string CharAt() function method. It is used in various methods of function in characters.<\/p>\n<p>When a character value is searched for in a string, Java&#8217;s built-in charAt() function delivers the character value at the specified index number.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are going to discuss the String charAt() function in Java. The Java String returns the character in a string at the given position charAt(int index) function. The index value should fall between 0&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447268,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,182,5694,5695,327,5538,250,187,188,5696],"class_list":["post-89388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-charat","tag-java-string-charat-method","tag-java-string-charat-method-with-examples","tag-java-tutorials","tag-java-tutorials-for-beginners","tag-learn-java","tag-string-charat","tag-string-charat-in-java","tag-string-charat-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String charAt() Method - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java String charAt() method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String charAt() Method - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java String charAt() method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-03T12:30:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String charAt() Method - TechVidvan","description":"Java String charAt() method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String charAt() Method - TechVidvan","og_description":"Java String charAt() method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-02-03T12:30:09+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String charAt() Method","datePublished":"2025-02-03T12:30:09+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/"},"wordCount":587,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp","keywords":["java","java String charAt()","java string charAt() method","java string charAt() method with examples","java tutorials","java tutorials for beginners","Learn Java","String charAt()","String charAt() in java","string charAt() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/","name":"Java String charAt() Method - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp","datePublished":"2025-02-03T12:30:09+00:00","description":"Java String charAt() method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-string-charat-.webp","width":1200,"height":628,"caption":"java string charat ()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-charat-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String charAt() Method"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89388"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89388\/revisions"}],"predecessor-version":[{"id":447796,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89388\/revisions\/447796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447268"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}