A Short Introduction to Writing Java Code Zoltán Majó
Outline Simple Application: Hello World Compiling Programs Manually Using an IDE Useful Resources
Outline Simple Application: Hello World Compiling Programs Manually Using an IDE Useful Resources
Hello World Learning a new programming language usually starts with Hello World: Using your favorite editor save as HelloWorld.java
About Text Editors Generally it is good to learn a feature-rich editor Our recommendation: Emacs You can use whatever editor you like (notepad, vim, gedit, jedit, etc.)
Where to Save? It is a good idea to save to NAS storage offered by ID, available in all student rooms Windows: mapped as drive T: Linux: mounted in /CIFS/ /<nethz_id> Personal data should be stored in private Service documentation: http://www.id.ethz.ch/services/list/comp_raum_s tud/homeverzeichnis/index_en
Outline Simple Application: Hello World Compiling Programs Manually Using an IDE Useful Resources
Compiling Change directory to that of the source file Compile with command: javac HelloWorld.java Classfile containing bytecode: HelloWorld.class Run the program: java HelloWorld
Using the Compiler Open Command Prompt (Windows) or Terminal (Linux) Windows: PATH=%PATH%;<path to jdk>\bin eg. PATH=%PATH%;c:\Program Files\Java\jdk1.6.0_11\bin Linux: export PATH=$PATH:<path to jdk>/bin
Using the Compiler Open Command Prompt (Windows) or Terminal (Linux) Windows: PATH=%PATH%;<path to jdk>\bin eg. PATH=%PATH%;c:\Program Files\Java\jdk1.6.0_11\bin Linux: export PATH=$PATH:<path to jdk>/bin
Using the Compiler Open Command Prompt (Windows) or Terminal (Linux) Windows: PATH=%PATH%;<path to jdk>\bin eg. PATH=%PATH%;c:\Program Files\Java\jdk1.6.0_11\bin Linux: export PATH=$PATH:<path to jdk>/bin
Some Words About Java Technology Why don t we run the executable directly, why invoke it within another program (java)? Java is compiled to Java bytecode, an intermediate binary format Then, the Java Virtual Machine executes the bytecode Source Code Bytecode Native code Java compiler (javac) Java Virtual Machine (java)
Some Words About Java Technology Why don t we run the executable directly, why invoke it within another program (java)? Java is compiled to Java bytecode, an intermediate binary format Then, the Java Virtual Machine executes the bytecode Source Code Bytecode Native code Java compiler (javac) Java Virtual Machine (java)
Some Words About Java Technology Why don t we run the executable directly, why invoke it within another program (java)? Java is compiled to Java bytecode, an intermediate binary format Then, the Java Virtual Machine executes the bytecode Source Code Bytecode Native code Java compiler (javac) Java Virtual Machine (java)
Some Words About Java Technology Why don t we run the executable directly, why invoke it within another program (java)? Java is compiled to Java bytecode, an intermediate binary format Then, the Java Virtual Machine executes the bytecode Source Code Bytecode Native code Java compiler (javac) Java Virtual Machine (java)
Organizing Project Good idea to separate sources, binary files, documentation Example layout: Compile with: javac d bin src\helloworld.java Execute with: java cp bin HelloWorld
Outline Simple Application: Hello World Compiling Programs Manually Using an IDE Useful Resources
Eclipse IDE Good news: Eclipse can do all these for you automatically Workflow: create workspace create project create main class run
Setting up the Workspace Advice: use your network share, because the format of the project directory is platformindependent
Creating a Project
Creating a Project Please note that separate source and class folders are used If using the same workspace on multiple machines, please make sure that the compliance level is the same you are using the same version of Eclipse
Creating a Project Please note that separate source and class folders are used If using the same workspace on multiple machines, please make sure that the compliance level is the same you are using the same version of Eclipse
Creating a Project Please note that separate source and class folders are used If using the same workspace on multiple machines, please make sure that the compliance level is the same you are using the same version of Eclipse
Creating the Main Class
Creating the Main Class
Creating the Main Class
Creating the Main Class
Writing the Program Add the call to System.out.println() to the method body
Running the Program Click on Ctrl + F11 to run the application or press Results should be observed below, in the console window
Useful Links Java: http://java.sun.com Eclipse: http://www.eclipse.org Emacs: http://www.gnu.org/software/emacs/ Java Tutorial: http://java.sun.com/docs/books/tutorial/ Java Code Conventions: http://java.sun.com/docs/codeconv/ Java API: http://java.sun.com/javase/6/docs/api/