The substring method in Java extracts part of a string using a start index and optional end index. substring(int beginIndex) returns from that index to the string’s end. substring(int beginIndex, int endIndex) returns characters up to, but not including, the end index. Both forms are zero-indexed.
The substring java method also throws StringIndexOutOfBoundsException if your indices are invalid — negative, beyond the string length, or if beginIndex exceeds endIndex. Understanding this method alongside charAt, type conversions, and the String/StringBuffer/StringBuilder distinction is essential for Java interviews and placement tests across India.
- substring() is zero-indexed and the end index is exclusive, so
"Hello".substring(1, 3)returns"el". - charAt(int index) returns a single
charat the given position; it is the fastest way to inspect one character. - String is immutable; StringBuffer is thread-safe and mutable; StringBuilder is mutable but not thread-safe, and it is faster for single-threaded code.
- The default value of a
Stringreference in Java isnull, not an empty string. - You can read a character from the console using
Scannerby callingnext().charAt(0).
How substring and charAt Work in Java
Java strings are zero-indexed, which catches beginners off guard every time. The character at position 0 is the first character, so "Java".charAt(0) returns 'J'. That is the core mechanic behind both substring and charAt in Java.
Java substring Examples
String s = "Programming";
s.substring(0, 4) gives "Prog". s.substring(4) gives "ramming". The two-argument form is more common in real code because it gives you precise control over which slice you extract. When you use substring in Java with a single argument, the method returns everything from that index to the last character.
One practical use case Indian students frequently encounter in placement tests at companies like TCS, Infosys, and Wipro: reverse a string character by character using substring inside a loop. It works, though using a StringBuilder and calling reverse() is faster and cleaner in production. This pattern also appears regularly in AMCAT and eLitmus assessments, which many Indian engineering graduates take before campus placements.
charAt Java Example
charAt(int index) returns a primitive char. That is an important detail because you can then cast it to an int to get its ASCII value. (int) 'A' gives 65, (int) 'a' gives 97. This is the standard way to work with ASCII values in Java without importing any extra library.
Character class methods like Character.isDigit(), Character.isLetter(), and Character.toUpperCase() pair naturally with charAt(). You pull the char out of the string with charAt, then pass it to one of these static methods for classification or transformation.
How to Convert String to Char Array in Java
Converting a String to a char array is a one-liner: char[] arr = "Hello".toCharArray();. Going the other direction, you pass the array into the String constructor: String s = new String(arr);. Both directions are O(n) in time and space.
This pattern comes up constantly in competitive programming on platforms like GeeksforGeeks, CodeChef, and HackerRank, as well as in technical interviews at Indian IT companies. If you are preparing for placements, knowing these core string skills is one of the top competencies colleges expect before graduation.
String vs StringBuffer vs StringBuilder: The Real Difference
Java gives you three classes for working with text, and picking the wrong one either breaks thread safety or kills performance. The distinction matters in any Java backend role.
Comparison at a Glance
| Feature | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safety | Yes (immutable) | Yes (synchronized) | No |
| Performance | Slow for concatenation | Moderate | Fastest |
| append() method | No (uses + operator) | Yes | Yes |
| Introduced in Java | Java 1.0 | Java 1.0 | Java 1.5 |
| Use case | Fixed text, constants | Multi-threaded apps | Single-threaded loops |
According to the Java SE 21 API Specification (Oracle, 2023), concatenating strings with the + operator inside a loop creates a new String object on every iteration, which puts pressure on the garbage collector. StringBuilder avoids this by modifying a single internal buffer.
The append() Method in Practice
The append method in Java is what makes StringBuilder and StringBuffer efficient. sb.append("text") adds content to the existing buffer without creating a new object. You can chain calls: sb.append("Hello").append(" ").append("World") is perfectly valid and very readable.
A JetBrains Developer Ecosystem Survey (2023) found that Java remains the most widely used JVM language among professional developers globally, used by 57% of respondents. String manipulation is one of the most-tested topic areas in Java interviews across India’s IT hiring market.
According to NASSCOM’s India Tech Talent Report (2024), Java is listed as a required skill in over 68% of software engineering job postings targeting fresh graduates in India, making string handling methods like substring java, charAt, and StringBuilder among the most practically valuable topics to master before your first interview.
When String Immutability Actually Helps You
Because String objects cannot be changed after creation, they are inherently safe to share across threads. That is why Java uses strings as keys in HashMap and as class names in reflection. The string pool, managed by the JVM, also means two string literals with the same content share the same memory reference by default.
If you are planning to build a career in Java backend development, understanding this distinction is non-negotiable. Check out these top online courses for high-salary tech jobs to see which Java-aligned paths pay the most in 2025.
Character Input, Default Values and ASCII in Java
How to Take Character Input in Java
Java’s Scanner class does not have a nextChar() method. The standard workaround is char c = sc.next().charAt(0);. This reads the next token as a string and picks the first character. It is the most reliable way to take char input in Java without dealing with BufferedReader complexity.
If you are reading from a file or need higher performance, BufferedReader with read() returns an int that you cast to char. Both approaches work fine in competitive programming. For beginners, stick with Scanner until you are comfortable with streams.
Default Values in Java
The default value of a String in Java is null. Not an empty string, not a space, but null. This is because String is a reference type, and uninitialized reference variables default to null in Java. Calling any method on a null string throws a NullPointerException, which is one of the most common runtime errors beginners hit.
The default value of a char primitive is '\u0000', the null character, which has an ASCII value of 0. Keep that in mind when you initialize char arrays and then check their contents.
Working with ASCII Values in Java
Getting the ASCII value of a character is as simple as casting: int ascii = (int) 'B'; gives 66. You can also do arithmetic with chars directly. 'a' + 1 gives 98, which corresponds to 'b'. This is useful for problems involving character frequency counts or Caesar cipher implementations, both common in Indian placement tests and GATE exam preparation.
A Stack Overflow Developer Survey (2024) finding worth knowing: string manipulation and array handling are consistently ranked among the top 5 most-tested skill areas in technical interviews at Indian IT companies, based on developer-reported interview experiences.
How to Use substring in Java for Type Conversions
Converting a string to a double uses Double.parseDouble(str). Converting to a date requires SimpleDateFormat or the newer LocalDate.parse() from java.time. Converting a single char to a string is done with String.valueOf(c) or Character.toString(c). You can also use the substring java method to isolate a numeric portion of a string before parsing it, for example extracting "2024" from "Year2024" with s.substring(4) before passing it to Integer.parseInt(). These small conversions come up constantly in real application code.
If you are just getting started and looking for free resources to practice these concepts, here is a guide to the best free online learning platforms for students in 2025 that covers Java and much more.
Frequently Asked Questions
How does substring work in Java?
substring(int beginIndex) returns a new string from the given index to the end of the original. substring(int beginIndex, int endIndex) returns characters from beginIndex up to, but not including, endIndex. Indexing starts at 0. Both forms of the substring java method throw StringIndexOutOfBoundsException if indices are negative, greater than the string length, or if beginIndex exceeds endIndex.
What does charAt do in Java?
charAt(int index) returns the primitive char at the specified position in a string. It is zero-indexed, so "Java".charAt(2) returns 'v'. If you pass a negative index or one greater than or equal to the string’s length, it throws a StringIndexOutOfBoundsException. It is frequently used alongside character class methods for validation and transformation.
What is the difference between String, StringBuffer and StringBuilder?
String is immutable; every modification creates a new object. StringBuffer is mutable and thread-safe because its methods are synchronized, making it suitable for multi-threaded applications. StringBuilder is mutable but not synchronized, so it is faster in single-threaded scenarios. For most loops and string-building tasks in single-threaded code, StringBuilder is the right choice.
How do you take character input in Java?
Java’s Scanner has no nextChar() method. The standard approach is char c = scanner.next().charAt(0);, which reads the next whitespace-delimited token and picks its first character. For reading from streams, use BufferedReader.read(), which returns an int that you cast to char. Both work well; Scanner is simpler for console input.
What is the default value of a String in Java?
The default value of a String in Java is null, not an empty string. Since String is a reference type, uninitialized instance variables of type String hold null by default. Calling any method on a null string reference causes a NullPointerException at runtime, so always check for null before operating on string variables.
How do you use substring in Java to extract part of a string?
Call str.substring(startIndex) to get everything from startIndex to the end, or str.substring(startIndex, endIndex) to get a specific slice. For example, "Hello World".substring(6) returns "World" and "Hello World".substring(0, 5) returns "Hello". Remember that the end index is exclusive and indexing starts at zero when you use substring in Java.
Your Next Steps This Week
Pick one method from this article, write five small programs around it, and run them. That is it. Do not read more tutorials; write code. Start with substring and charAt, move to StringBuilder with append, then practice reading character input with Scanner.
If you are thinking about where Java skills fit into a career plan, explore entry-level tech jobs that value strong Java fundamentals and read this guide on choosing the right career path after graduation in 2026. For a broader view of where tech careers are heading, check out the best career options in 2026 for students and professionals.
Java is a strong foundation, but the industry now expects developers to understand adjacent domains too. Cybersecurity, AI, Blockchain, and Web3 skills are increasingly listed alongside Java in job descriptions at companies hiring in India. At 3.0 University, you will find practical, industry-aligned certification courses in Ethical Hacking, Cybersecurity, AI, and Web3 built specifically for students, working professionals, and career switchers. Browse the full course catalogue at 3.0 University and find a programme that fits where you want to go next.
Last updated: September 2026. Reviewed by the 3University editorial team.