• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Unable to utilize method on Character?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27463
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic