In this article String in Java we give the information about String is an object representing a sequence of characters.
String in Java:
String is a series of characters or group of characters, double quotes ” ” are used to define the string in Java. String class is used to define string in Java. String is a non-primitive(i.e., there is size is not fixed) data types because it references a memory location where data is stored in the heap memory (or String Constant Pool) i.e., it references to a memory where an object is actually placed. And thus the variable of a non-primitive data types is also called reference data types or object reference variable. This object reference variable lives on the stack memory and the object to which it points always lives on the heap memory.
String is an object representing a sequence of characters. An array of characters functions similarly to a Java string. We use java.lang.String class to create a String object.
There are many java string methods used to perform different operations on strings, such as split(), compare(), equals(), substring(), concat(), replace(), intern(), compareTo( ) etc.
class StringTest
{
public static void main(String[] args)
{
String name = “Rajveer”;
System.out.println(name);
}
}
Output
javac StringTest.java
java StringTest
Rajveer
To create a String, there are three classes-
- String
- StringBuffer
- StringBuilder
How to Create a String Object in Java
There are two ways to create a string object:
- Using String Literal
- Using New Keyword
1. Using String Literal
Java string literals are created and represented using double quotes, and all characters are appended between these quotations. This method makes Java more memory efficient because new objects are not created in this method.
Every time we create a string literal, the Java Virtual Machine (JVM) checks the string constant pool. If a string exists in the pool, it references the pooled instance. However, if the string does not exist in the pool, it creates a new String instance and places it in the pool.
Example
String s=”hello”;
2. Using New Keyword
Another way to create a Java string is to use the new keyword. Just as a new keyword is used to create an instance of any other class, it also works to create an instance of the String class.
The primary difference between the two methods using string literals and using the new keyword is that when a string is created using the latter method, the JVM creates a new object of the string in heap memory (non-pool). And the pool is kept in the string constant.
We can create String class object by – String s=new String();
It will create an IMMUTABLE object.
Example
String s = new String(“hello”);
Regardless of whether the same valued strings exist in heap memory or not, string objects created using the new keyword are placed in heap memory, i.e., outside the string constant pool.
Syntax
String stringName = new String(“string_value”);
public class MyClass {
public static void main(String[] args) {
String text = “Hello World”;
System.out.println(text);
}
}
// Result: Hello World
Note:
- Using New Keyword you can create a two object one in Heap memory and other in String Constant Pool
- Using String Literal you can create only one object in String Constant Pool
- The String object present in SCP are not applicable for Garbage Collector because a reference variable internally is maintained by JVM.
Java String Methods
As we know that Java Strings is actually an object, which contains a lot of methods, such as length(), toUpperCase(), toLowerCase(), indexOf() etc.
- length() : To find the length of the String.
- toUpperCase() : To convert String to UPPERCASE
- toLowerCase() : To convert String to lowercase.
- indexOf(): To find the position of any character in the String.
public class MyClass {
public static void main(String[] args) {
String text = “HELLO World”;
System.out.println(text.length());
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
System.out.println(text.indexOf(‘W’));
}
}
11
HELLO WORLD
hello world
6
String Concatenation
Joining two or more strings is called Concatenation, Concatenation is done by the two methods given below.
- By using + operator.
- By using concet() method.
public class MyClass {
public static void main(String[] args) {
String text1 = “Ram”;
String text2 = “Singh”;
System.out.println(text1 + text2);
System.out.println(text1 + ” ” + text2);
System.out.println(text1.concat(text2));
System.out.println(text1 + ” “.concat(text2));
}
}
OUTPUT:
RamSingh
Ram Singh
RamSingh
Ram Singh
Array in Java:
In Java, Array is a data structure used to store similar types of data. In an array many items of the same type can be stored at the same time. The size of an Array is fixed, that is, you decide its size while declaring it, which cannot be changed later.
Features of Array:
- Fixed size: Once the size of the array is fixed, it cannot be changed.
- Same data type: An array can contain only one type of data, such as only integers, only floats, or only strings.
- Indexing: Each item in the array has an index, and the index starts from 0. The first element will be at index 0 and the last element will be at array.length – 1.
Declaring and initializing the Array:
- Declaration:
int[] arr; //This is the declaration of an integer array.
- Initialization:
arr = new int[5]; //This is initializing the array with 5 integer stores.
- Declaration and Initialization Together:
int[] arr = new int[5]; //This does declaration and initialization together.
- Assigning values while declaring the Array:
int[] arr = {10, 20, 30, 40, 50}; //This initializes the array with values directly.
Example:
public class ArrayExample {
public static void main(String[] args) {
// Declaring and Initializing an Array
int[] arr = {10, 20, 30, 40, 50};
// Accessing elements of an array
for (int i = 0; i < arr.length; i++) {
System.out.println(“Element at index ” + i + ” : ” + arr[i]);
}
}
}
Output:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
Multi-Dimensional Array:
You can also create multi-dimensional arrays in Java. The most common is a 2D array, which can be thought of like a matrix.
public class TwoDArrayExample {
public static void main(String[] args) {
// 2D Declaring and Initializing an Array
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing Elements of a 2D Array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + ” “);
}
System.out.println(); // New line after each row
}
}
}
Output:
1 2 3
4 5 6
7 8 9
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining