123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Technology,-Gadget-and-Science >> View Article

All About String In Java

Profile Picture
By Author: jatin
Total Articles: 66
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

String in Java is immutable and stored in memory pool. Learn about StringBuffer, StringBuilder, and string manipulation techniques in Java.
In Java, String represents a sequence of characters. It is an immutable object, meaning once it is created, its value cannot be modified. If a string is updated, a new String object is created with the same reference. This behavior makes string manipulation in Java memory-efficient and secure.
Let's start with a basic example:
String str = "India";
System.out.println(str); // Output: India
String ctr = new String("India");
String ptr = "India";
String ktr = str;
str = str + "n"; // Now str points to a new object
System.out.println(str); // Output: Indian
How many objects and references?
Number of references: 4 (str, ctr, ptr, ktr)
Number of objects: 3
Ways to Create a String
By string literal:
String str = "India";
By using new keyword:
String ptr = new String("India");
Java String Pool
The Java String Pool is a memory area inside the heap where string literals are stored. It ...
... helps in memory optimization by avoiding the creation of duplicate strings.
Common Java String Methods
Java provides many built-in string methods in Java that help in manipulating and analyzing string data. These are part of the java.lang.String class.
String str = "Welcome to Cyberinfomines.com";
System.out.println(str);
Frequently Used Java String Functions:
length() - Returns the length of the string.
int size = str.length();
System.out.println("Length: " + size); // 29
charAt(int index) - Returns character at specific index.
substring(int beginIndex) - Extracts substring from beginIndex.
substring(int begin, int end) - Extracts substring from begin to end-1.
indexOf(String s) - Returns first index of given substring.
contains(CharSequence s) - Checks if the string contains a specific sequence.
toUpperCase(), toLowerCase() - Converts string to upper/lowercase.
trim() - Removes leading/trailing whitespace.
split(String regex) - Splits string into array using regex.
concat(String str) - Appends given string.
equals(), equalsIgnoreCase() - Compares strings.
compareTo(), compareToIgnoreCase() - Lexicographic comparison.
hashCode() - Returns hash code.
isEmpty() - Checks if string length is 0.
lastIndexOf() - Finds last index of a character or substring.
valueOf() - Converts different types (boolean, char, int, etc.) to string.
These java string methods are widely used in interview questions and real-world development scenarios.
Use Case: Short Name Generation
public class StringBasic {
 public static String getShortName(String firstName, String lastName, String surName) {
 String shortName = firstName.toUpperCase().charAt(0) + "." +
 lastName.toUpperCase().charAt(0) + "." +
 surName.toUpperCase().charAt(0) + surName.toLowerCase().substring(1);
 return shortName;
 }
 public static void main(String[] args) {
 System.out.println(getShortName("Sanjay", "kumar", "Chandel"));
 System.out.println(getShortName("sanjay", "kumar", "chandel"));
 System.out.println(getShortName("SANJAY", "KUMAR", "CHANDEL"));
 }
}
Please visit our website to know more:-https://cyberinfomines.com/blog-details/all-about-string-in-java

Total Views: 21Word Count: 339See All articles From Author

Add Comment

Technology, Gadget and Science Articles

1. How Mobile App Development Service Enhances Customer Experience Across Industries
Author: michaeljohnson

2. How Erp Solutions Help Manufacturers Increase Operational Efficiency
Author: Focus Softnet

3. Real-time Tim Hortons Menu And Price Tracking
Author: Real Data API

4. Industrial Automation In Europe: Strong 8.5% Cagr Through 2032
Author: Suvarna

5. Doordash Pricing Data Scraping Reveals Key Price Trends
Author: Retail Scrape

6. Retail Market Trends Shaped By Online Liquor Price Scraping
Author: Web Data Crawler

7. Benefits Of Less Common Laptop And Smartphone Brands
Author: John Rame

8. Finops In Practice: Controlling Costs In Cloud Migration Projects
Author: Matthew Hinge

9. How An Enterprise Ai Development Company Enhances Data-driven Decision Making
Author: michaeljohnson

10. Web Scraping Pet Food Data For Competitive Pricing
Author: Den Rediant

11. Top Mobile App Development Trends For Staying Ahead In The Digital Era
Author: Sdreatech

12. Real Estate Ai Agent For Investors: Data-driven Decision-making In Real Estate
Author: michaeljohnson

13. Mastering Java Operators
Author: jatin

14. How To Perform Audible Audiobook Data Extraction
Author: Real Data API

15. Plan, Control, Excel In Production Planning
Author: Focus Softnet

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: