Java Refresher
This week I decided to give myself a refresher on Java since I haven't studied it heavily in a few months. I'm currently going through Java 11 For Complete Beginners by John Purcell on Udemy. So far in his lesson plan, he has talked about how Java compiles the code so that it can be used from computer to computer with no real issue. John has also ran through the very basics of how to print out different things.
System.out.println("Hello world!");
That's the famous first line of code that every beginner to coding learns. Although it may be slightly different depending on language, the concept is the same. The course is also teaching the fundamentals of code like creating integers and variables to be used throughout the code. They can incorporated into different kinds of arithmetic depending on my needs.
int cats = 7;
int dogs = 5;
int total = cats + dogs;
System.out.println(total);
This small block of code allows me to assign a number to the integers; cats, dogs, total. With this, I'll be able to later on use the integer names instead of inputting the number. This can be useful because in hundreds of lines of texts, I might not be able to remember how many "cats" I had.
In one of his later courses, he showcases how to utilize strings. Strings are basically objects within Java. In his example, he walks the viewers on how to create a small menu. This is important for me to know because within my app that I want to create, I will have a plethora of menus and I need to make sure to know how to properly implement them.
String prompt = "Select an option:";
String option1 = "Add an entry";
String option2 = "View the database";
String option3 = "Exit";
int value1 = 1;
int value2 = 2;
int value3 = 3;
String menu = "\t" + prompt + "\n";
menu += "\t\t" + value1 + ". " + option1 + "\n";
menu += "\t\t" + value2 + ". " + option2 + "\n";
menu += "\t\t" + value3 + ". " + option3 + "\n";
System.out.println(menu);
This will output a non-interactable menu that looks like:
Select an option:
1. Add an entry
2. View the database
3. Exit
If I were to change the text that is within the quotation m arks for the strings prompt, option1, option2, and option3, then that would change what the menu would say. The int values are there to work as a numbered list for the options on the menu. The "\n" is added to the end of each line and this is to tell java to go to the next line at the end of that line. If I were to input "\n" in the middle of the line, it would move to the next line wherever I place it. He places "\t\t" to create two tabbed spaces at the beginning of the line to create formats. Lastly, there has to be a way to output all of this information and because he created the string called "menu" and assigned all of the options to that string, he just needed to do a simple System.out.println(menu); and this will print out the whole menu.
Going through his course has been incredibly easy to follow along to and helpful. I feel like despite taking time off from Java, I am able to quickly remember older material and concepts and pick up the newer ones.
Much like riding a bike, you'll be back in the deep end before no time.
ReplyDelete