Java for Everyone...Strings

Java for Everyone...Strings

Strings are a sequence of characters, used to represent textual data. In Java its an object provided by the java.lang.String class which is part of Java's core library.

Strings are immutable, once created values cannot be modified, any operation that appears to modify a string creates a new string.

?Key characteristics-

Ordered-Strings are made up of a sequence of characters, such as letters, digits, symbols, or spaces and these sequences of characters that have a unique position for each character.

Indexable -Individual characters within a string can be accessed using a numerical index.

Comparable-Strings can be compared to each other to determine their relative order or equality.

Immutable-In some programming languages, such as Java, Python, JavaScript, and C#, strings are immutable as mentioned above.

Enclosed by quotation marks-Strings are usually indicated by single or double quotation marks.

Creating Strings in Java

Using String Literals: All collection of characters surrounded by double quotes.

public class HelloWorld {
	public static void main(String[] args) {

		String s = "Hello World!";
		System.out.println(s);
	}

}        

In this case, "Hello world!" is a?string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a?String?object with its value—in this case,?Hello world!

Strings defined with double quotes are automatically placed in the string pool.

Using the new Keyword:

You can create?String?objects by using the?new?keyword and a constructor.

public class stringHelloWorld {
	public static void main(String[] args) {
        /*Strings defined with double quotes are automatically placed in the string pool.
         */
		String s1 = "Hello World!";	
		/*
		 * Creates a new string object in the heap,  even if the same value exists in the string pool
		 */
		String s2= new String("Hello World!");

		System.out.println(s1);
		System.out.println(s2);
	}

}        

Please note -Creates a new string object in the heap, even if the same value exists in the string pool.

The String class provides various methods for string manipulation…

1.????? length()->Returns the length of the string

2.????? charAt(int index)->Returns the character at the specified index

3.????? substring(int start, int end)->Extracts a substring from the string

4.????? contains(CharSequence seq)->Checks if the string contains the specified sequence

5.????? equals(Object obj)->Compares the string for content equality

6.????? equalsIgnoreCase(String str)->Compares strings, ignoring case differences

7.????? toUpperCase()->Converts all characters to uppercase

8.????? toLowerCase()->Converts all characters to lowercase

9.????? trim()->Removes leading and trailing spaces.

10. replace(char oldChar, char newChar)->Replaces occurrences of a character.

11. split(String regex)->Splits the string into an array based on the regex

?Special Characters in Strings -since string must be written within quotes, Java will misunderstand this string, and generate an error, to address this issue backslash is used as escape character.

Escape character??????????? Result Description

\'??????????? '???????????? ??????????? Single quote

\"?????????? "???????????? ???????????? Double quote

\\?????????? \???????????? ??????????????? Backslash

Some Examples-

1.????? String Length-length () is used to identify the length of the input string.

?public class StringLengthdemo {

	public static void main(String[] args) {

		String s = "This is my first string";
		System.out.println("Length of the string is :" + s.length());

	}
}        

?

2.????? Finding index of character in a String- The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string, this includes the whitespace as well.

?public class indexOf {

	public static void main(String[] args) {

		String s = "This is my first string";
		System.out.println("IndexOf input string is "+s.indexOf("m"));

	}
}        

?

3.????? String Concatenation - The + operator is used be used to combine two strings , called concatenation.

?public class StringConcat {
	public static void main(String[] args) {
		
		String FirstName= "Mr Arjun";
		String LastName= "M";
		System.out.println("Full Name is- "+FirstName+" "+LastName);
	}

}        

?

Please note- Java uses the + operator for both addition and concatenation, Numbers are added. Strings are concatenated

Food for thoughts-

·?????? How are strings stored in Java?

·?????? Is String a primitive data type in Java?

·?????? Why is String immutable in Java?

·?????? How is a String object created in Java?

·?????? What is the difference between String literal and new String()?

·?????? How does the JVM handle string literals?

·?????? What is the String Pool in Java?

·?????? Why is String immutable and final in Java?

·?????? Can a String contain null values?

·?????? How does the concat() method differ from using the + operator?

·?????? How does the equals() method differ from the == operator in strings?

·?????? How do immutable strings improve security in Java?

·?????? Are String objects thread-safe?

·?????? What happens if you compare a String with null?


/*

This is article is based on JDK 8, any changes on features, enhancement or deprecated options can be referred to official release notes-

https://www.oracle.com/java/technologies/javase/jdk-relnotes-index.html

https://dev.java/learn/

*/

?

要查看或添加评论,请登录

Arjun K.的更多文章

  • What is Pesticide Paradox in Software Testing?

    What is Pesticide Paradox in Software Testing?

    In Agriculture when insect infestation threatens a farmer’s harvest, he sprays pesticide to kill these pesky insects…

  • Java OOPs...Encapsulation!

    Java OOPs...Encapsulation!

    Consider a real-life example of your bank account, your account balance is private—you wouldn’t want anyone to directly…

  • Little’s Law in Performance Testing

    Little’s Law in Performance Testing

    In 1954, John Little published a paper in which he described the queueing theory by a new law. Later, it is named…

  • Performance Metrics- Throughput, latency and response time!

    Performance Metrics- Throughput, latency and response time!

    Throughput Throughput serves as a crucial performance metric for assessing system efficiency, identifying bottlenecks…

  • Application Performance Improvement using CAST SQG formerly AIP.

    Application Performance Improvement using CAST SQG formerly AIP.

    ??What is CAST Structural Quality Gate (SQG) formerly AIP ? CAST SQG draws on CAST’s extensive experience in deep…

  • Performance Test-An Overview!

    Performance Test-An Overview!

    Performance testing is a type of software testing that focuses on how well an application performs under various…

  • Software Performance Test - An Overview!

    Software Performance Test - An Overview!

    Performance testing is a type of software testing that focuses on how well an application performs under various…

  • Compile-time & Runtime Errors in Java..

    Compile-time & Runtime Errors in Java..

    Compile-time A compile-time error in Java refers to an error that occurs during the compilation phase, when the Java…

  • Java for Everyone...Encapsulation.

    Java for Everyone...Encapsulation.

    Consider a real-life example of your bank account. Your account balance is private—you wouldn’t want anyone to directly…

  • Java Collection Framework in Selenium

    Java Collection Framework in Selenium

    In Selenium WebDriver, Java collections play a crucial role in managing and manipulating data such as lists of web…

社区洞察

其他会员也浏览了