JAVA state wh of the following is true or false. If false, explain the reason br
ID: 3915154 • Letter: J
Question
JAVA
state wh of the following is true or false. If false, explain the reason briefly egation of a fse complete, correct, and meaningful statement could be. Mere meaningful. taement does not necessarily make it complete, correct, or and hat the port is used to import all members of a class or interface so that they may e accessed without reference to their class names. 2. The number capacity of characters stored in a StringBuilder object may not exceed its 3. Strings are immutable objects in Java. 4. Strings are represented in Java in ASCII format. 5. Literal strings are stored in a String object as character arrays. 6. String objects may only be created using the keyword new. Se, you also use 7. No two String objects of the same content may be created in Java. 8. The expression SS compares the references to the strings S, and Sa to check whether they refer to the same object, not whether they have the same content.Explanation / Answer
1. True, In Java, static import (is introduced in 1.5 version). With the help of static import, we can access the static members of a class directly without class name or any object.
Example: we always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import we can access sqrt() method directly.
2. False, The capacity, which is returned by the capacity() method, is always greater than or equal to the length (usually greater than) and will automatically expand as necessary to accommodate additions to the string builder.
The logic behind the capacity function:
3. True, the String object is immutable, its reference variable is not. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified.
4. False, A Java String (before Java 9) is represented internally in the Java VM using bytes, encoded as UTF-16. UTF-16 uses 2 bytes to represent a single character. Thus, the characters of a Java String are represented using a char array.UTF is a character encoding that can represent characters from a lot of different languages (alphabets).
5. False, String in Java is an Object. This means when you instantiate a String object it will never change , while an array of characters can have the contents altered.
6.False, You can create String objects with String literal
Example: String str="Hellochegg!";
Or you can also create Using character array:
You could also convert character array into String here
Example:char ch[]={ 'H','e','l','l','o','c','h','e','g','g','!'};
String str1=new String(ch);
Or you can also create using Java String Class:
Java String Class represents character strings. The java.lang.String class provides a lot of methods to work on string.
7. True, When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string object. It's safe because String is immutable in Java.
String s1="Java";
String s2="Java";
System.out.println(s1== s2);
This gives result true because s1 and s2 points to the same object.
String Pool is the mechanism that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.
8.True, you can consider the example:-
String s1 = "ComputerScience";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.