The '=' sign is important in Java, and x = y is used when you're changing the value of x to the value of y. You'd use x == y when you're comparing the value of the two variables (must be primitive variables). For instance, you'd use:
if (x == y) {
x += 1;
}
If you want to see if two things are UNequal, you'd use != such as in:
if (x != y) {
x -= 1;
}
Any interesting thing, however, is when you try to compare Strings. you must use .equals(). For example:
String var = "Jose";
if ("Jose".equals(var)) {
var = "Joes";
}
Here's a personal example of how .equals() is used, as well as "==":
If you don't know what's going on here, I'm comparing a string in a list (names) to the variable "search."
The following are some of the important ideas from this chapter:
- Methods can have parameters, such as setTeamScore(team, score) has the team and score parameters. You could say setTeamScore(home, 45).
- Methods can accept literal values like "3" or 3, but can also take variables like glove or baseball, if glove = "glove" and baseball = 3, for example.
- If a method is declared as boolean, it must return a boolean answer. You'd call it "void" if it didn't need a response.
Java:
Python:
As you'll notice, Python is so much simpler and easier to understand.
No comments:
Post a Comment