Flashcards
NumbersAndStrings
Cards in this deck (15)
- 1How do Strings work? (or) Brief us on stringsString is a sequence of characters. String objects are immutable which means a constant and cannot be changed once created. Also, it stored in a special memory area inside heap known as string constant pool.
String objects can be created as a literal or using new operators.
String name1 = “Java”;
The above statement creates a string object and places it in a string pool if it is not already present in the string pool and a reference is assigned to name1.
String name2 = new String(“newJava”);
The above statement creates a string object in heap memory and checks whether it is present in the string pool or not. If the ‘newJava’ is not present in the string pool, then it will place this string in the string pool else it will skip it. In this case, two objects are created that is one in heap memory and the other in the string pool. - 2What is the difference between String Buffer and String Builder?StringBuffer
StringBuffer is synchronized i.e., thread safe. It means two threads can't call the methods of StringBuffer simultaneously.
Faster than String class due to mutability but slower than StringBuilder class as it allows multiple threads simultaneous operations.
Syntax: StringBuffer var = new StringBuffer(str);
StringBuilder
StringBuilder is non-synchronized i.e., not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
Fastest among all as it allows mutability and does not allow multiple threads operating at the same time.
Syntax: StringBuilder var = new StringBuilder(str); - 3What classes are mutable strings?StringBuilder and StringBuffer
- 4Why Strings in Java are immutable?In the String constant pool, a String object is likely to have one or many references. If several references point to the same String without even knowing it, it would be bad if one of the references modified that String value. That's why String objects are immutable.
- 5How would you reverse a string of your name?StringBuilder or StringBuffer class has an in-build method reverse() to reverse the characters in the string.
- 6What are Wrapper classes?A Wrapper class is a class whose object wraps or contains primitive data types. Each of the 8 primitive types has corresponding wrapper classes.
Primitive Type Wrapper Class
byte Byte
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short - 7Why do we use wrapper classes? or What is the purpose of the Wrapper class in java?Java need wrapper classes to be a pure Object-Oriented Programming language, so Java needs everything to look like an object.
Classes in Collection framework & java.util package, such as ArrayList and Vector, store only objects (reference types) and not primitive types
Wrapper classes has lot of utility methods, it really helpful and easier. - 8What is autoboxing and unboxing?The process of converting a primitive value into an object of the corresponding wrapper class is called autoboxing.
The process of converting an object of a wrapper type to its corresponding primitive value is called as unboxing. - 9Differentiate between str1 == str2 and str1.equals(str2), where both str1 & str2 are String objects.In Java both the equals() method and the == operator compares the objects.
The == operator can be used for comparing references (addresses) and the .equals() method can be used to compare the content. == checks if the objects point to the same memory location, whereas .equals() compares the values of the objects. - 10How can two strings be compared in Java?String Equals Method: Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
String Equals Ignore Case: Two strings are compared without taking the case of the strings (lower or upper) into account.
Objects Equals Method: This is the Objects class method which returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned.
String Compare To Method: Compares two strings lexicographically based on the Unicode value of each character sequence represented by the String object being compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal - 11How do you convert a string to an integer and vice versa in Java?Integer class in the Java lang package provides different methods for converting strings to integers and vice versa. The parseInt() method allows you to convert a String into an integer and the toString() method allows you to convert an Integer into a String.
- 12What is the use of valueof() method?It converts the value of an argument into the relevant Number Object. The argument can be any numeric primitive data type or String.
However, if there is a radix mentioned in the argument, the respective data is converted into the base of the radix first and then converted to an Integer object. - 13Give the example for mutable and immutable objects in java?Mutable objects value can be changed. StringBuilder and StringBuffer are the examples of the mutable objects.
mmutable objects value cannot be changed once created. String is an immutable class in java. - 14What is the use of split () method in Java?The method split(String regex) to split the String into String array based on the provided regular expression
- 15Which class should we use among String,StringBuffer and StringBuilder when there are lot of String concatenation and String modification operations with thread-safe code?StringBuffer
Reason:
If we use String , with every modification and concatenation operation, a new String is formed as String is immutable. It will lead to the memory allocation issues.
StringBuilder cannot be used as it is not synchronized, i.e., thread-safe.
Information
- Description
- Java Numbers And Strings Interview Questions
- Created by
- Jerry Reeves Jr
- Views
- 1
- Rating