Understanding String, StringBuffer, and StringBuilder in JAVA

¡Supera tus tareas y exámenes ahora con Quizwiz!

what does multi threaded environment mean?

multi threaded environment means executing multiple threads independently but at the same time they are sharing data through the process.

what does single threaded environment mean?

single threaded means executing from beginning to end without interruptions. -executes entire code in a single thread without sharing any data. (no interruption)

Differences between StringBuilder and StringBuffer?

- StringBuilder is faster, it is not thread safe - StringBuffer is thread safe so it makes it slower. - StringBuilder runs in a single threaded environment - StringBuffer runs in a multithreaded environment.

What is String concatenation in Java, and how can we concatenate Strings?

String concatenation is forming a new String by combining multiple Strings. There are 2 ways of String concatenation in Java: 1. we can concatenate Strings by using + (plus) operator 2. we can use concat( ) method to concatenate Strings

TRUE OR FALSE: StringBuffer and StringBuilder are mutable types of Strings.

TRUE!!! both are mutable types of Strings

What happens when you created a String with 'new' keyword?

-Strings that are created with the new keyword will be stored in the heap with its own location also, It creates another object to store (in String pool) OVERALL : Strings that are created with the new keyword are stored in both String pool, and in heap. (Both stored their own separate locations)

What is mutable/immutable in Java?

Immutable: - it means once an object is created, value for the object cannot be changed like String. --> Immutable = unchangeable - Ex: String Mutable: - it means when an object is created, value for it can be changed like StringBuffer and StringBuilder. --> Mutable = changeable -- Ex: StringBuilder and StringBuffer

What is String in Java?

String is a final and immutable class : (Immutable - cannot be changed once initialized.) -String objects can be created to store a sequence of characters such as words, letters, symbols, etc. -String is an immutable data type --> Once you initialize the value of a String object it cannot be changed or altered. -String class contains many useful methods to manipulate data. --> useful methods such as: charAt(), indexOf() and soon

StringBuffer

StringBuffer is a mutable type String : - it is thread safe —> synchronized - runs in a multi threaded environment - StringBuffer is slower compared to StringBuilder bc it is thread safe. - can be converted to String by using toString() method ---------------------------------------------------------- - contains many useful methods to manipulate data like append(), insert(), delete(), reverse() and so on.

StringBuilder

StringBuilder is a mutable type String : - not thread safe —> not synchronized - run in a single threaded environment - is faster than StringBuffer (bc it's not thread safe) - can be converted to String by using toString() method ---------------------------------------------------------- - contains useful methods to manipulate data such as append(), insert(), delete(), reverse() and so on.


Conjuntos de estudio relacionados

HRM: The Dynamic Environment of HRM (Chapter 1)

View Set

Week 2: Diagnostic interviewing & Differential Diagnosis

View Set

B.2 Domain 1: Hardware, All Questions

View Set

OS Installation & Upgrade Methods Quiz

View Set

MONETARY POLICY; MONEY AND BANKING; FED RESERVE BANK

View Set

Wongs - Chapter 23: The Child with Fluid and Electrolyte Imbalance

View Set