-4

I created a class with 2 method, one can take 3 parameter while another one can take 4.. all the parameters declared as final..

then I wrote this

    if (b == "Select") {
        myobj.print(a, b, c, d);
    } else {
        myobj.print(a, b, c);
    }

it doesn't work..

Clayton Louden
  • 1,056
  • 2
  • 11
  • 28
cgpa2.17
  • 309
  • 1
  • 3
  • 12
  • 3
    First and foremost: You're using `==` instead of `.equals("Select")`. Second, could you clarify your question a little bit more? You can't overload the `String` class - it's final. What are you trying to accomplish? What is `myobj`? What is the signature for `myobj.print`? – Makoto Oct 15 '12 at 02:09
  • Use "Select".equals(b) instead of ==. Look at this reference for Java String pool: http://stackoverflow.com/questions/1881922/questions-about-javas-string-pool – Thinhbk Oct 15 '12 at 02:26

2 Answers2

2

String are immutable, you can't change them.

Unless you are using String literals, you should always use .equals() or .equalsIgnoreCase() instead of == to compare Strings/objects.

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
kosa
  • 65,990
  • 13
  • 130
  • 167
1

Yes, an overload of a method with an extra String argument is:

  • possible,
  • valid Java, and
  • it works.

My money is on the theory that your problem is nothing to do with overloading, and actually due to your mistake of using == to test if two strings are equal.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216