-3

The following codes are based on Java.
Here, the famous method "toUpperCase()", can be invoked like this:

String s = new String("abc");
String ss = s.toUpperCase();
//result: ss = "ABC"

For this: s.toUpperCase()
We didn't pass a value to the method, so this method cannot has a meaningful return value.
To explain what is called a not-meaningful return value, here is an example:

//not pass a value to the method
String toUpperCase(){
...
return ???;
}

As above, the return value has no relationship with that object called "s". (the invoking of a non-parameter method is often independent from the objects). If I invoke this non-parameter method: String ss = s.toUpperCase(); how can that return a value that has any relationship with the object "s"

And the following is called meaningful:

//pass a value to the method
static String toUpperCase(String str){
....  //to change the "str" into uppercase
return str;
}

Now I can invoke the method toUpperCase() like this:
String ss = String. toUpperCase(s);
Since I pass the "s" (its address) to the method, I can do anything I wish to change the object and return a meaningful return value.


Based on the above, I have a reasonable doubt about this method s.toUpperCase(). since it cannot return a meaningful return value.
  • 2
    Do you understand the `this` reference? _"the invoking of a non-parameter method is often independent from the objects"_ -- that is 100% false. You are thinking of `static` methods. – Jim Garrison Dec 10 '17 at 02:10
  • `s` is an instance of `String`, which contains a series of `char`s which represent your text. `String#toUpperCase` is a instance method which acts upon the text of an instance of `String`, so it returns the contents of `s`, but converted to upper case. It's kind of a core fundamental of object oriented programming – MadProgrammer Dec 10 '17 at 02:15
  • 3
    No. It is not *reasonable* doubt. It is completely *unreasonable* doubt. It is a plain fact that `toUpperCase()` actually works for **millions** of Java programmers. So, if you have a *theoretical* argument that it shouldn't work, then the *reasonable* conclusion is that your argument is incorrect ... because there is something about Java that you don't understand. – Stephen C Dec 10 '17 at 02:24
  • What do you mean by "We didn't pass a value to the method"? Is your concern that `s.toUpperCase()` has no information about what instance it was invoked on (here held by `s` reference variable)? If that is the case then you need to know that this code will be compiled in something close to `String.toUpperCase(s);` (first argument for non-static method is reserved for instance of class which method belongs to, so `toUpperCase()` method is behind the scene more like `toUpperCase(String this){...}`) and inside it you can communicate with object represented by `s` via `this`). – Pshemo Dec 10 '17 at 03:08
  • In other words, `s.someMethod()` doesn't pass `s` explicitly, but behind the scenes it is compiled as `someMethod(s)`. Similarly `a.someMethod(b)` will be compiled as `someMethod(a,b)`. Maybe this will be helpful: https://stackoverflow.com/a/44095738 – Pshemo Dec 10 '17 at 03:16

1 Answers1

1

First, terminology. s is an instance.

how can that return a value that has any relationship with the object "s"

Do you understand this? For example, how does dog.getName() return a "meaningful value" of the name of that dog instance?

The char[] of the String object named s is stored within that instance, much like the String name of a Dog object.

class Dog {
    String name;

    public String getName() { return name; } // or 'return this.name;'
}

Bonus: Source code of toUpperCase() implementation, which does make a call to Character.toUpperCase(c) because char primitives to not have a toUpperCase method themselves.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245