Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- Figure 1-2.
- Listing 1-1.
- Figure 1-3.
- How It Works
1-3. Testing Java
Problem You want to compile Java from the command line. Solution Use the little application shown in Listing 1-1 . You can use a smart editor like Notepad++ or a different text editor in this example. Figure 1-2. Testing Java version ChApteR 1 GettInG StARted wIth JAvA 17 6 To create the source code for your first Java program, you must do the following. 1. Declare a class named Hello. 2. Declare public static void main(String args[]) or String... args as the main method. 3. Enter the System.out.println(“Hello World”) command to display “Hello World” in the command prompt window. Listing 1-1. Hello.java public class Hello { public static void main(String args[]){ System.out.println("Hello World"); } } Now, save the file as Hello.java in your working folder. Open the command window. After changing to your work directory, enter the following to compile the application. javac Hello.java It should return the prompt without saying anything. It also means that you have correctly updated the Path system variable. If you want to know more about what the javac compiler is doing, type -verbose between javac and the name of the file (javac -verbose Hello.java). You see a file named Hello.class in your work directory. Enter the following in the command prompt to run the application (see Figure 1-3 ). java Hello Figure 1-3. Testing a Java class ChApteR 1 GettInG StARted wIth JAvA 17 7 Note All the code described in this book is available for download from github .com/apress/java17-recipes. In this case, you can simply type the code; in others, you don’t need to retype it. It is important that you improve your programming skills using the eclipse environment, which is a famous free and open source Java Ide, containing a workspace for developing applications, that you can download at www.eclipse.org . You can find the examples in folders with the same names as the corresponding chapters. How It Works Java is an object-oriented programming language based on classes and objects with attributes and methods. A class is necessary for creating objects. To create a Java program (i.e., a .class file), you must create a .java file. A .java file is a readable text file, while a .class file is a binary file, which is a file containing Java bytecode that can be executed on the Java Virtual Machine (JVM), and that is produced by a Java compiler from the .java file. Tip You must compile source code. Source code is kept in files with a .java suffix, so your operating system’s file and directory path notation are appropriate. One executes a class. A class is an abstract concept in the language, so its dot- notation becomes appropriate (i.e., it is necessary to write a dot (.) after the name of the instance of a class followed by the method to be used). Keep this distinction in mind to help yourself remember when to use which notation. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling