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.
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.
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.
Edit create “*.java” file
Compile generate “*.class” file
Creating Your First Program
- 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.
- 2.Now create a Java project named MyFirstProgram, as shown in Figure 4-4. Click “Finish.”
- 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.”
- 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:
- 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.
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)”?
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.
Problems
- 1.What is the difference between the Something.java file and the Something.class file?
- (a)
A .java file is a much larger binary file and a .class file is a smaller compressed version.
- (b)
The .class file is for object-oriented programming and the .java file is for procedural programming.
- (c)
A .java file contains code written in the Java language, and a .class file contains code written in the C++ language.
- (d)
The programmer writes the .class file first, and the .java file is generated automatically later.
- (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.
- (a)
- 2.Which of the following method headers is correct?
- (a)
public static foo void[]
- (b)
public static void foo()
- (c)
public void static foo{}
- (d)
public void static foo()
- (e)
public static foo()
- (a)