User Tools

Site Tools


j1-1_strings

This is an old revision of the document!


J1-1 Strings

Welcome to the first course in Java!

Requirements:

  • IntelliJ IDEA CE

You can download IntelliJ IDEA CE from the JetBrains webpage: https://www.jetbrains.com.cn/en-us/idea/download

Scroll down to the second option, the “Community Edition”, which is free.

Entering The Program

Start a new project called “J1-1 Strings”. Then type the following code into the Main.java file:

Main.java

Main.java

public class Main {
    public static void main(String[] args) {

        // Code starts here:

        // Print a message.
        System.out.println("Hello, my name is Appledog!");

        // Create a Scanner object to read input
        Scanner scanner = new Scanner(System.in);

        // Prompt the user for their name
        System.out.print("What is your name? ");

        // Read the input from the user
        String name = scanner.nextLine();

        // Output a response
        System.out.println("Hello, " + name + "!");

        // Close the scanner
        scanner.close();
    }
}

Now try to run the program. On Mac it's Control-R. On windows, I think, it's Shift-F10. Get used to using a keyboard; it's faster.

Fixing any Bugs

Oh no! Your first error. Find the line with the red underlined word “Scanner”. This tells you that “Scanner” was not found. Click on this word and the IDE will suggest a fix if possible. Here, it can automatically fix the error by inserting a line of code at the top of the program:

import java.util.Scanner

Now, run the program again. What happens?

A: (Your answer).

Teacher may discuss the answers here.

Studying the Program

Here's an explanation of what each line of the program does. We will look at it like a computer looks at it.

import

The first thing we do is import the java.util.Scanner class. This Scanner class is inside the util library, which is inside the java library. That's it's location. What is it? It is a group of utility functions for doing things like, for example, reading strings.

public class Main

This is a declaration of a class. A class is a group of code and data; a specification, if you will. In this case, the specification for Main is special. Main comes from the entry point for C programs, which is called main. You may recall “int main(void){}” in C. Here, Java needs to know where the program starts. It looks for the main() function inside the Main{} class, and starts there.

So, why is “public” here? Public means that a class from “outside” can access what is inside. In this case it means that the system is allowed to start your program. Remember, the system is assuming there is a Main class and a main function. If the Main class was private, then it couldn't access the main function inside and it couldn't start the program. So it has to be marked public. The default is package-private. So Main, since it's not being accessed from another class in the same package – but by the system – has to be marked pubilc by default.

public static void main(String[] args)

We see public again; this is just so the main() function can be found by the system.

It's static, which means it runs without first making a named copy of itself. If you don't know what that means, it means that you can make as many copies of a class and run it at the same time as you want. But here, if you are trying to run a program, it just “runs the program” without making any special moves.

Taking a step Back

It might be better to say “don't worry about public, private, static” and so forth, but I wanted to point out that there is a reason for these words – but, it's actually not important for you to think about it too much. It will make much more sense later on. So for now, just consider this as the basic shell of a program, and all the code we write goes in the middle. Like this:

basic starter program

public class Main {
    public static void main(String[] args) {
        // Code starts here:
        System.out.println("Hello world!");
    }
}
j1-1_strings.1732753688.txt.gz · Last modified: 2024/11/28 00:28 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki