Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java objects For Each Loops

OK....Hello!

I've been learning and im really trying to get syntax and terminology of Java down. That being said I keep wondering if certain words are created (like an object) or if they are basic Java syntax.

I'll digress,

public int getTileCount(char letter) {
    int count = 0; 
    for (char tile :mHand.toCharArray()) {
      if (tile == letter) {
      count++;
      }
    }
    return count;
  }

In this method does "letter" have to be letter? I agree that its a practical name for the variable, but if i changed it to "match" wouldn't it still work?

In this line:

for (char tile :mHand.toCharArray()) {

Why do we have to use a ' : ' and 'to'? is 'toCharArray' concrete terminology? It's small things like that which cause me to second guess and over-complicate things. I find my self wanting to do it "the right" way but also wanting to be creative and make "my own" way. I definately get the idea that thinking outside of the box is promoted, but knowing what i can and cant change is holding me up.

Any advice is welcome, or even a link to read up.

thanx, Eric

I've edited your question to include some formatting and syntax highlighting for your code to make it easier to read. You can do that yourself like this:

How to Post Code on the Treehouse Forums

2 Answers

Hey Eric!

Awesome to here you came to these forums for clarification on this! I'll try my best to help out ;)

So, where we have this line:

public int getTileCount(char letter) {

char is the data type and letter is the name of the variable. You are completely right that we could rename letter to anything we want and the code will still work - it's just easy to refer to letter as it is, so we know what it's doing. If you wanted to, you could call it myLetter, theBestLetter or anything you want! match would also work!


Now for our for loop:

for (char tile :mHand.toCharArray()) {

The : is a funny one. I like to think of it as the word in. So, I would read this out as for every char which we'll refer to as tile in the mHand char array....

Now, toCharArray() is very good terminology and here's a few pieces of terminology we use in methods:

  • to - Generally used for conversion, changing one object to a different object.
  • get - We call these methods getters, when we want to get something (This could be a class, a String, boolean, you name it!)
  • set - We call these methods setters, when we want to set something (e.g: We may have a name variable stored in an AboutMe class, and we can set the name from any other class with this method).

Now, there's no "law" on naming your methods like this, just like there's no law in not writing in camel-case. Instead, it's convention that we do so and when working in a team on the same project, everyone follows these conventions to ensure consistency. Your code's not going to break by writing differently, it's just many people would frown upon not following these conventions.

Things like the : and = are required as part of the Java syntax. We can't ever write these differently, it just won't work! We'll get an error in our code if we try to. As you write more in Java, you will realise what you can and can't change. Like right now, you are probably already aware that you must use an = to define a variable, we can't use anything else as it's just against the syntax!

Hopefully this should explain these concepts for you but if you need anything explained a bit more, give me a shout and I'll be sure to help out :)

Very helpful! And both great answers.

I'm no Java expert, but I'll do my best to help clarify.

In your first example, letter is an argument name. You can use any Java-valid variable name for this, it doesn't have to be letter. Remember that if you change the argument name, you also need to change the reference to it in your if expression.

// When passing arguments to a function, you can call the variable whatever you want.
public int exampleFunction(int someArgumentName) {
}

The second part is a bit trickier because I'm not super familiar with the Java language. I believe that the colon is a necessary syntax that's part of the Java for each constructor. You're basically saying for each thing in things execute some block of code.

The toCharArray() is a method included in Java that converts a string to a new array of characters. So if your string is "Hello" you would get back ["H", "e", "l", "l", "o"] if you called this method on it. In this case, you're using it so that you can iterate over the characters and check to see if the specified tile is in a given hand.

// For each character tile in the array of characters obtained from String mHand, check to see if it matches.
for (char tile :mHand.toCharArray()) {
}

Hopefully that helps. Please let me know if anything is still unclear, and I'll do my best to help you work it out. Happy coding! :)

I see....and the code syntax being highlighted in the forum like that helps as well. Its clear the 'toCharArray()' is a method. It makes me wonder what methods are coded in java.