





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























James Brucker
desktop mobile phones game box web server & web apps smart devices
You can use any editor to write the Java source code. For your first program, use a text editor (e.g. Notepad). Later you will use an Integrated Development Environment (IDE) such as Eclipse, IntelliJ, Netbeans, or VS Code. Or a beginner's IDE: BlueJ. An IDE makes you much more productive, but you need some practice... so for now, use a text editor.
To do these examples you must install the Java Development Kit (JDK). Useful to also install the Java documentation (Javadoc).
(^) Oracle Java: java.oracle.com (^) OpenJDK: AdoptOpenJdk.net (^) Stick with LTS versions: Java 8, 11, 15
(^) There are some suggestions at the end of these slides (^) You can also find videos on YouTube for help installing Java and writing your first program.
C:> java -version java version "11.0.5" 2019-10-15 LTS If it prints "command not found", then the Java "bin" directory is not on your terminal's "search path", specified by the PATH environment variable.
In this version we'll see how to read input and use a local variable. The easiest way to read input in Java is with a Scanner object. A Scanner parses input into strings, numbers, etc To create a Scanner object for reading the console use: import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner console = new Scanner( System.in ); System.in is a predefined variable for byte-by-byte input from the default input (the terminal) this declares a variable of type Scanner named "console". import tells the compiler where to find the Scanner class
Scanner has a method named nextLine( ) that reads the rest of the input line and converts it to a String. System.out.print("string") does not print a newline after the output. public static void main(String [] args) { Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String who = console.nextLine( ); System.out.println("Hello, " + who ); } }
Compile (javac) and run (java) the 2nd version: C:> javac Hello.java C:> java Hello What is your name? Nerd Hello, Nerd.
Most programs contain logic for decision making. We'll write a program that greets and shows the time. The logic is: get the current time if now is before 12:
else if now is before 18:00 then