In today's class, we started by reviewing the homework, and then we learned about the following:
- Variables
Variables are handy for several reasons. They simplify code especially in repetition of one phrase (such as post.comment.comment1 which can be shortened with a variable down to "comment1").
To initialize a variable in Java, we use
<data type> <variable name> = <assigned value>;
For example:
int thisIsANumber = 39494;
If you don't know the value of the variable yet, you can alternatively use
<data type> <variable name>;
Which lets you assign a value to it later. Refer to https://www.lol-101.com/classrooms/introduction-to-java/2-2-how-to-declare-and-initialize-variables-for-computation for primitive types and their limitations as well as a slightly more in depth explanation.
- Classes
A class is where a program is saved. It's a good practice to name the class based on what it's for, such as BankBalance.
- Functions
A function is also known as a method. The first function called is always main(), because each Java program needs a place to start. As beginners we'll start by only coding in main, and once we get more familiar with functions, classes, and the keywords that go along with them, we'll start to use other functions and classes more and more.
For further reference on the above two you can visit https://drive.google.com/file/d/1zrjDYIQVM0fVDCNTCDRNH_3jeXXNX3rc/view.
- Naming conventions
Names must start with a letter or an underscore. *The others should be alphanumeric.
*Avoid special characters (or basically anything that doesn't fall under the category of "alphanumeric")
Spaces are not allowed.
Names in Java are case sensitive.
Reserved words such as "int" or "class" are not allowed.
*Class names should be nouns with the first letter capitalized, and methods/functions should be verbs written in camelcase. Variables should be nouns as well, and the first letter should be lowercase. These should not start with an underscore or a $.
*One character names should be avoided because they create confusion as to the purpose of the variable.
Anything with a * is technically allowed in Java and should compile fine. However, it is generally a bad practice and should be avoided. Some non-alphanumeric characters are allowed in Java as names, but you will rarely see these.
The solutions for this week's homework can be found here: https://replit.com/@offexe/9-22hw#Main.java