Results 1 to 3 of 3
  1. #1
    Grisbas is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Use of variables in methods like charAt()

    Hi.
    I'm aware of creating methods like charAt() is one.

    Random
    Java Code:
    String s = "Random text";
    
    System.out.println(s.charAt(2));
    What I'm confused about how it is possible for charAt() to use the string "s"? What's the deal? Does it parse the string to the class? There's probably an easy answer to this, but I've yet to see how it works, which is why I'm asking for help. A link to a tutorial answering my question would be awesome.

    Thanks in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: Use of variables in methods like charAt()

    The string s is an object of type String. So like any other object, there are methods which are specific to the class which operate on the object. In this case, String is backed by a char[] array. So when you pass an argument to charAt() it returns the character at that location in the array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Grisbas is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Use of variables in methods like charAt()

    I love how everything makes sense now. You made it so simple. I get it now!

    Thanks!

Similar Threads

  1. How to keep variables through multiple methods
    By SethLasky in forum New To Java
    Replies: 11
    Last Post: 07-26-2012, 03:35 AM
  2. Passing variables to methods
    By KKorn429 in forum New To Java
    Replies: 3
    Last Post: 05-21-2012, 12:40 AM
  3. Using variables in different methods
    By squirmytoad in forum New To Java
    Replies: 2
    Last Post: 01-10-2012, 08:04 PM
  4. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 03:44 AM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •