© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_4

4. Start Playing with Java

Ron Dai1 
(1)
Seattle, WA, USA
 

Download and install a Java runtime environment (choose the right version based on your computer’s operating system): https://www.java.com/en/download/manual.jsp

Download and install Eclipse (look for the most recent stable version): http://www.eclipse.org/downloads/

After installation, you should see an icon as shown—the “Neon” version—as an example on your desktop.

../images/485723_1_En_4_Chapter/485723_1_En_4_Figa_HTML.jpg
Once you launch Eclipse, you will need to specify the Workspace (Figure 4-1).
../images/485723_1_En_4_Chapter/485723_1_En_4_Fig1_HTML.jpg
Figure 4-1

Specifying the workspace

What Is the Difference Between the JRE and the JDK?

The JRE is the “Java Runtime Environment.” It is where your Java programs run. The JDK is the “Java Development Kit,” which is the full-featured software development kit for Java, including JRE, the compiler, and tools (e.g., JavaDoc, Java debugger) to create and compile programs.

When you only want to run Java programs on your browser or computer, you will install the JRE. But if you want to do some Java programming, you will also need to install the JDK.

Figure 4-2 shows the clear relationship between the JRE and JDK, as well as their basic feature areas.
../images/485723_1_En_4_Chapter/485723_1_En_4_Fig2_HTML.png
Figure 4-2

JDK and JRE compared

What Are a Workspace, Source, and Package?

Workspace” is used to group a set of related projects together. Usually these projects will make up an application.

Source” means source code, that is, the Java program and related code.

Package” indicates a collection of files.

What Are Edit, Compile, and Execute?

“Edit” writes code in a Java language.

“Compile” converts Java source code to Java bytecode.

“Execute” runs the program.
  • Edit ../images/485723_1_En_4_Chapter/485723_1_En_4_Figb_HTML.gif create “*.java” file

  • Compile ../images/485723_1_En_4_Chapter/485723_1_En_4_Figb_HTML.gif generate “*.class” file

Creating Your First Program

Let’s get started:
  1. 1.
    Once Eclipse is launched, left-click on “File” in the top menu bar and left-click on “New” in the drop-down menu. Then select “Java Project” from another drop-down menu as shown in Figure 4-3.
    ../images/485723_1_En_4_Chapter/485723_1_En_4_Fig3_HTML.jpg
    Figure 4-3

    Before we create a Java project or a Java class

     
  2. 2.
    Now create a Java project named MyFirstProgram, as shown in Figure 4-4. Click “Finish.”
    ../images/485723_1_En_4_Chapter/485723_1_En_4_Fig4_HTML.jpg
    Figure 4-4

    Creating a Java project

     
  3. 3.
    Select File ➤ New ➤ Class to create a Java class (name: “Welcome”), as shown in Figure 4-5. Make sure you select “public static void main(String[] args).” Click on “Finish.”
    ../images/485723_1_En_4_Chapter/485723_1_En_4_Fig5_HTML.jpg
    Figure 4-5

    Creating a Java class

     
  4. 4.

    The Welcome class and the public static void main(String[] args) methods are automatically created, as shown in Figure 4-6. Then manually add the following output line:

     
System.out.println("Hello, friend, you are welcome!");
  1. 5.
    Click on “Run” from the top menu bar, and then click on “Run” from its drop-down menu, we will see output text in the Console window as shown in Figure 4-6.
    ../images/485723_1_En_4_Chapter/485723_1_En_4_Fig6_HTML.jpg
    Figure 4-6

    Running the application

     

Exploring Class and main( )

As you saw in Chapter 3, a “class” is a template that describes the behavior that an object is supposed to show. You can create individual objects from the class. This is called “class instantiation .” A class has local variables, instance variables, class variables, and a number of methods.

main() is a method name. When your Java program is executed, the runtime starts your program by calling its main() method first. The main() method is an entry point of your Java program.

Why Is It “public static void main(String[ ] args)”?

This is a convention designed by Java language and JVM (don’t worry if some of this doesn’t make sense, we’ll come back to it later in the book).
  • main is the name of the method;

  • String[] args is the main() method input parameters as String array data type; the string values passed into the main() method are called arguments; they can be used as optional values to send to the program when it is started;

  • void means there is no return data from the main() method call;

  • public means the main() method is available for the JVM to call in order to start the execution of the whole program;

  • static indicates that the main() method cannot be called with an object instance; in other words, the JVM can call it directly and does not have to create extra structures to call it.

If you change public to private, you will see the following error during runtime.
Error: Main method not found in class <your class name>, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Problems

  1. 1.
    What is the difference between the Something.java file and the Something.class file?
    1. (a)

      A .java file is a much larger binary file and a .class file is a smaller compressed version.

       
    2. (b)

      The .class file is for object-oriented programming and the .java file is for procedural programming.

       
    3. (c)

      A .java file contains code written in the Java language, and a .class file contains code written in the C++ language.

       
    4. (d)

      The programmer writes the .class file first, and the .java file is generated automatically later.

       
    5. (e)

      Something.java is a source code file typed by the programmer, and Something.class is a compiled executable class file that is run by the computer.

       
     
  2. 2.
    Which of the following method headers is correct?
    1. (a)

      public static foo void[]

       
    2. (b)

      public static void foo()

       
    3. (c)

      public void static foo{}

       
    4. (d)

      public void static foo()

       
    5. (e)

      public static foo()

       
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156