1. What is a String in Java

String can be defined as a sequence of characters i.e ‘Hello World” can be called String because it is made by sequence of characters.

In Java, a string is an object that represents a sequence of characters. It is a fundamental data type used to store and manipulate textual data. Strings in Java are instances of the java.lang.String class, which is a part of the Java Standard Library.

2.How to Declare String in java

In java String can be declare by two ways

  1. By new Keyword
  2. By String literal

  By new Keywork

Java String is created by using a keyword “new”. With new keyword an Object of type String class is created inside memory.

Programming Assignment help

Explanation:

String s=new String (“Hello World”) in this line of code new keywork create object of class name String and Pass “Hello World” String to its constructor, a constructor create object in side heap memory and reference of that object is saved inside variable name s of type String.

Programming Assignment help

In above picture new keyword create String class object in heap memory area having initialized value is “Hellow World” and its memory address is stored inside String variable name s which is created on Stack Memory Area.

Programming Assignment help

Explanation:

String s= “Hello World” in this line of code a String “Hello World” is created on String pool area inside Heap Memory Area and its reference is saved inside variable name s of type String.

Programming Assignment help

In the above picture “Hello World” String is created inside String Pool Area of Heap Memory Area and its Reference is Stored inside variable name s of Type String which is created on Stack Memory Area.

String is immutable while StringBuffer and StringBuilder is mutable, when we say immutable than it’s mean we can not modify the Content of the Variables at run time.

Programming Assignment help

When this line String s=”Hello World”; execute a “Hello World” String created in String pool area inside Heap Memory Area and  reference is saved inside variable name s whose type is string, after this when s=”how are you”; line execute by compiler than a new string “how are you” is created on String pool area and its reference is saved inside s variable, now s variable is pointing out on newly created string “how are you” and old string “Hello World” is dereferenced and will clean out by garbage collector routine.

 

Mutable

In mutable every time when you modify the string there is no need to crate new string on Heap memory area, it’s just modified old content with new once.

Programming Assignment help

When StringBuffer sb=new StringBuffer(“Hello “); line is executed a String “Hello “ is created on Heap Memory Area and reference is stored inside variable sb of type StringBuffer, now after that when sb.append(“World”) line

 

executed than String “World” is concatenated with String Hello on Heap Memory Area.

Programming Assignment help

4.How to concatenate String in java

  • When we create String with new keywork using classes StringBuffer or StringBuilder than we use append method of these class to concatenate the two strings.
Programming Assignment help
  • When we create string using string literal then there are 2 methods to concatenate string
Programming Assignment help
Programming Assignment help

when we concatenate String using + operator or concate() function a new string  Hellow World” is created inside String pool Area and its reference is saved inside variable name s3 of type String created on Stack Memory Area.

 

 

 

5.What is the String class’s equals () method

equals () method belong to String class, which is used to compare the content of two string variables.

Programming Assignment help

In the above code s1 variable and s2 variable both containing String Hello and equals() method compare the contents of both variables in side if condition which is true because both contains String Hello, so if condition become true and “s1 contents are equal to s2” message printout on the console.

Share the Post:

Related Posts

Java Operators
Java Tutorial
wethecoders

Java Operators

Java Operators Operators are used to perform different operations on variables and values. In Java operators can be divided into the following groups. Arithmetic Operators

Read More »
Introduction to Java
Java Tutorial
wethecoders

Introduction to java

Introduction to java Java is the most popular programming language, which was created in 1995 by James Gosling at Sun Microsystems. Initially, java was called Oak,

Read More »
Introduction to OS and Linux - Learn More
Technical
wethecoders

Introduction to OS and Linux

Introduction to OS and Linux What is an Operating System? Introduction to OS and Linux: An operating system (OS) is a type of system software

Read More »

wethecoders.com is one of the leading online assignment help services. wethecoders.com always take step towards your satisfaction. We work hard so that you can get good grades. Our prime motto is to help you to get good grades in your assignment, project, and homework. Our team is professional and knowledgeable. We are here to help you 24/7. Try once to take advantage of our services and we assure you that you will get surely the best result.

We accept

Scroll to Top