• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to utilize method on Character?  RSS feed

 
Neko Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, new to Java.

I'm trying to improve an example code from my book and running into a perplexing issue.



Receiving a:
cannot find symbol
symbol: method toUpperCase()

http://download.oracle.com/javase/6/docs/api/java/lang/Character.html#toUpperCase(int)

I thought this was a valid method for Character data types.

Me -> <- Java
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no instance method toUpperCase(). There are only two static methods toUpperCase(char) and toUpperCase(int).

You can achieve the same without using instances of Character:
 
Neko Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I'm seeing the difference in the documentation now, when I compare it to:
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#toUpperCase()

I was beating my head, trying to understand why it needed a char or int. I even was trying it as:



That was throwing an error as well. I'm not sure if this can really be answered, but I'm not understanding why there wouldn't be an instance method. I'm also failing to understand how I could have determined that I needed to use:
Character.toUpperCase(grade).
 
Neko Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was talking to a co-worker and I realized that when I was trying:



I had grade set as a char, not a Character at the time.

Character is an object, that has methods
char is a primative data type.

Cool

Thanks again.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neko Singh wrote:I'm also failing to understand how I could have determined that I needed to use:
Character.toUpperCase(grade).

Because the API says the method is static. That means you should use the class name to call the method, not an instance.
 
Neko Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Neko Singh wrote:I'm also failing to understand how I could have determined that I needed to use:
Character.toUpperCase(grade).

Because the API says the method is static. That means you should use the class name to call the method, not an instance.


Rob, thanks again for taking your time with me.

I was discussing this yesterday with a co-worker and I was wondering if you could answer a question for me.

Besides it being an overall design of Java, I was wondering if making Methods static also saved in memory usage. If these methods were not static, and were instead instantiated with each instance, would the objects then take up more memory?

I feel like this is a "no duh" conclusion, but I just wanted confirmation. =)

Thank you.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods do not require an instance to be called. So if you only needed an instance to call one method that does not depend on the instance's state, then yes, you will save memory by using a static method.

Let's take a modified version your example, and assume that auto boxing does not use cached values*.
This code creates object "grade"** only to call the method. This method does not use any internal fields of the instance, and afterwards the instance is ignored. So that instance is created needlessly, and takes up unnecessary memory***.


* It does though, for all values between 0 and 127 (int values), a.k.a. those that are in the upper table at http://www.asciitable.com.
** OK, in real life it doesn't because of the caching of 'C' which is below 127.
*** You probably already figured out this instance is created anyway because of the caching.
 
Neko Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean. You're discussing the difference between my code needlessly using Character, rather than using char. Creating the object "grade" would take up more memory than creating a char. Correct?

I was probably asking a silly question. I was asking about the overall design aspect of Java in its use of static methods vs instance methods.

Essentially, would an object instance based on a class with static methods take up less memory than if Java design didn't include the existance of "static methods" and had all methods be instance based?

This was the "no duh" question I was referring to.

You're being very kind in answering my questions. I feel my questions are foolish.
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neko Singh wrote:Essentially, would an object instance based on a class with static methods take up less memory than if Java design didn't include the existance of "static methods" and had all methods be instance based?


No. An object instance doesn't contain any methods when it's stored in memory. It doesn't contain static methods or instance methods either.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!