Lessons
-
Lesson 27 - args main method
argsinpublic static void main(String[] args)Every Java program starts running from the
mainmethod. You’ve seen it many times:public static void main(String[] args)But what’s the purpose of
String[] args?It allows your program to receive input from the command line.
When you run your program from the terminal, you can pass extra data directly to it. Those pieces of data are stored in an array called
args.What exactly is
args?argsis a String array.- When you run a java program, each space-separated item typed after the program name becomes one element in that array.
For example, if you run:
java MyProgram hello world 1Then inside your program:
args[0]is"hello"args[1]is"world"args[2]is"1"args.lengthis3
Even though
"1"looks like a number, it is still a String, because all command-line arguments start as strings.How do you use these arguments?
Here’s a example that prints whatever the user typed:
public class Fruits { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("Argument " + i + ": " + args[i]); } } }If you run:
java Fruits apple banana cherry 1Output:
Argument 0: apple Argument 1: banana Argument 2: cherry Argument 3: 1Converting the arguments
Since every element of
argsis a String, you must convert values to numbers if needed:int x = Integer.parseInt(args[3]); double y = Double.parseDouble(args[3]);Exercise
Add two numbers from the command line
public class AddNums { public static void main(String[] args) { if (args.length < 2) { System.out.println("Please enter two numbers."); } int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; System.out.println("The sum is: " + sum); } }Running:
java AddNums 8 12Output:
The sum is: 20
-
Lesson 01 - Git
GitHub
Read the Git and GitHub documentation provided in the Tools section.
Git Commands: When to Use Them
git cloneWhen to use:
Use this the first time you want to copy a remote repository (e.g., from GitHub) to your computer.Command:
git clone <repository_ssh_url>Tip: Run this only once per project. After that, use git pull to update your local copy.
git pullWhen to use: Use this to bring in the latest changes from the remote repository to your local repository.
Command:
git pullTip: Always run this before starting new work to make sure your files are up to date.
git addWhen to use: Use this to stage (mark) new or modified files that you want to include in the next commit.
Command: For one file
git add <file_name>For all changesgit add .git commitWhen to use: Use this after git add to save a snapshot of your staged changes in the local repository.
Command:
git commit -am "Describe the change you made"Tip: Write clear and meaningful commit messages so you and others understand the change.
git pushWhen to use: To send your committed changes from your computer to the remote repository (e.g., GitHub).
Command:
git pushTypical Workflow
-
git pull: get the latest updates. -
Edit your code/files.
-
git add: if new files have been added or to stage your changes. -
git commit -am "message": save your changes locally. -
git push: upload your changes to GitHub.
-