Question

Let's assume String a = "123"; String c = "123"; String b =  new String(a); Which of...

Let's assume

String a = "123";
String c = "123";
String b =  new String(a);

Which of the following expression is True

c == b

a.compareTo(c)>0

c.equals(b)

a == b

b.equals(a)

Homework Answers

Answer #1
  • The expression c.equals(b) and b.equals(a) is true because of the following reasons:
  • String is immutable(once it is created its state cannot be change)
  • when we create string a="123" string b="123" both a and b are pointing to same memory location
  • But when we create new object of string we assing new value to it and the refrance vaiable will point to new memory location(Heap memory)
  • But now when we use equals() method then this equals method compare content of the string rather than Heap refrance pointer hence we get True when we compare c.equals(b) and b.equals(a)
  • Whereas compare method will return integer value based on the unicode of the string thats why it is not True expression.
  • for better understandin below code i am sharing
  • in below code compiler print "Hii" if two string are equal else it will print "Hello"

public class Main {
   public static void main(String[] args) {
       String a = "123";
       String c = "123";
       String b = new String(a);
         
       if (c.equals(b))
       {
           System.out.println("hii");
       }
       else
       {
           System.out.println("hello");
       }
         
      
   }

}

Hope this code will help you

Thank You!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a/b x c/dy Let's design a class and explore more on object reference: A fraction with...
a/b x c/dy Let's design a class and explore more on object reference: A fraction with two private fields: a for representing numerator and b for representing denominator. Define a constructor Fraction() accept two integers to represent the numerator and denominator. Define a public instance method named printString() which returns a String in form of "a/b". Define a public instance method named multiplyBy() which accepts one argument of class type Fraction, calculates the multiplication between self and the receiving Fraction,...
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
Write a function clean(dna) that returns a new DNA string in which every character that is...
Write a function clean(dna) that returns a new DNA string in which every character that is not an A, C, G, or T is replaced with an N. For example, clean('goat') should return the string 'gnat'. You can assume dna is all lowercase, but don't assume anything about the nature of the wrong characters (e.g. they could even have been accidentally transcribed as numbers). No importing Must use for loop
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list =...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list = new ArrayList<String>(); b ArrayList<String> list = new String<String>​[]; c ArrayList<String> list = new ArrayList<String>; d ArrayList​[String] list = new ArrayList​[String]; 2-All instance variables? a Are assigned a default value if they are not initialized b Must be initialized as part of their declaration c Are assigned an arbitrary value if they are not initialized d Must be initialized by the constructor 3-An instance method...
Let's assume that there are two firms, G and C. Firm G has grown at the...
Let's assume that there are two firms, G and C. Firm G has grown at the rate of 30% over the past year. I mean its total assets has increased by 30% during the year 2018. On the other hand, firm C has contracted by 5%, in other words, its assets has declined by 5%. My question is which firm would you be more willing to invest in the stock market for 2019?
Let's assume that we have a stock that is priced at $47 a share. There is...
Let's assume that we have a stock that is priced at $47 a share. There is a call that expires in 5 months that has a strike price of $44.50. The stock has a standard deviation of .35 and the risk free rate is 2.25%. Let's find the price of this call using the BSOPM. What is the N of d2? A. .3688 B. .6541 C. .2731 D. .5677
Write a program in C that extracts the tokens from a string. Given a string as...
Write a program in C that extracts the tokens from a string. Given a string as input, the program should print on the screen each token on a new line. For example given the following string as input: “hello there my friends” the program should generate: I am a programmer
IN C#, which of the following statements correctly instantiates an array which is defined as a...
IN C#, which of the following statements correctly instantiates an array which is defined as a string type array with three elements: string[] myArray = new string[] {“1”, “Smith”, “3.5”}; string myArray = new string[3] {“1”, “Smith”, “3.5”}; string[] myArray = new string[3]; myArray[1] = “string1”; myArray[2] = “string2”; myArray[3] = “string3”; string myArray = new string[3] {1, “Smith”, 3.5}; None of the above
C++ program checks if an input string is a palindrome or not. Ignore case and remove...
C++ program checks if an input string is a palindrome or not. Ignore case and remove all non-alphanumeric characters in the input string. C++ program uses only one string (no extra-string or array). And also, use two indices to point from the beginning and ending positions. Sample Input 0 Race car Sample Output 0 TRUE Sample Input 1 7...8 Don't nod 78. Sample Output 1 FALSE
The code I have so far:    public static boolean validPass(String sPass)    {    boolean...
The code I have so far:    public static boolean validPass(String sPass)    {    boolean y=false;    if(sPass.length()>10)    {    int i,j=0,k=0;    char c;    for(i=0;i<sPass.length();i++)    {              c=sPass.charAt(i);    if(c>='A' && c<='Z')    {    j++;    }    else if(c>='a' && c<='z');    else if (!(sPass.contains("&") || sPass.contains("-")    || sPass.contains("%") || sPass.contains("_")    ||sPass.contains("^")||sPass.contains("@")));    else if(c>='0' && c<='9')    {    k++;    }    else    {   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT