Ch. 1 Getting Started W - Java
Ch. 1 Getting Started W - Java
•Package – consists of one or more classes that are stored in the same
directory
•Identifiers – words that you can/will use to name your classes as well as
other elements(methods, variables) of the Java language. The following
rules apply:
•First character cannot be a digit: 5dollars, 10days
•Only: letters, digits, underscores, $ may follow the initial character
•No blank space: Month One,
•Cannot be a reserved word (page 26)
•Comment – anything that follows the two forward slash symbols “//”
//this is a comment
Chapter 1.4 - System Class, and print() method
•System– a class that provides a number of methods. Among those methods we
have methods to send data to the screen (output) and methods to accept data
from the user via keyboard (input)
•To print our output, such as “Hello World” , on the computer screen, we use:
System.out.print(“Hello World”);
out – is the name of the “standard output stream” object created from the
“System” class.
•\n – these two characters together are called a “newline escape sequence”. It
will cause “Hello” and “Word” to be printed on two separate lines on your screen.
•println() is like print() with an \n at the end.
Chapter 1.5 – javax.swing Package
•Dialog – a box that presents the user with output and requires some input. See
page 36-37 for types of dialog boxes and their messages
Chapter 1.5 – javax.swing Package
import javax.swing.*;
public class DisplayADialog
{
public static void main (String[] args)
{
JOptionPane.showMessageDialog(null, “Hello World”,
“Sample”, JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
How to get "Java JDK "
• The JDK consists of a set of programs for developing & testing Java programs
• https://www.oracle.com/java/technologies/javase-downloads.html
• Accept License Agreement, download from the appropriate link. For windows
chose Windows x64 installer. For Mac choose macOS Installer.
• Open the downloaded file and follow the "typical" installation instructions.
• For more information, please see the videos posted in Week 1 or Week 2
Using “javac” and “java”
• The commands "javac" and "java" are not system commands, but they are Java
commands(programs) located in the Java JDK bin directory. You need to set up the PATH
variable for Windows to know where to find javac and java (see the videos I have).
• Unless you set up PATH, if you are out of the C:\Program Files\Java\jdk\bin directory, the
computer won't know what the(javac, java) commands mean (or where the programs are).
• Suppose I want to run the program HelloWorldApp.java, which by the way is just a text
file containing the following java code:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
•HelloWorld.java is located in C:\new. First I need to navigate to that folder with the
command prompt (StartRun”command”)…see next page for DOS commands
•If I just type javac HelloWorldApp.java in the command prompt instead, it will give me the
following error message 'JAVAC' is not recognized as an internal or external
command,operable program or batch file.
•I must type C:\Progra~1\Java\jdk\bin\javac HelloWorldApp.java for no error. Upon
success, the HelloWorldApp.class is created.
•Now if I type C:\Progra~1\Java\jdk\bin\java HelloWorldApp, the command prompt will
print “Hello World” on the screen. See next page to visualize all this.
What I just described as seen/done in the command prompt:
C:\DOCUME~1\AGADOR~1>cd..
C:\DOCUME~1>cd..
C:\>cd new
C:\NEW>dir
Volume in drive C has no label.
Volume Serial Number is 38F2-2C3E
Directory of C:\NEW
C:\NEW>javac HelloWorldApp.java
'JAVAC' is not recognized as an internal or external command,
operable program or batch file.
C:\NEW>C:\Progra~1\Java\jdk\bin\javac HelloWorldApp.java
C:\NEW>C:\Progra~1\Java\jdk\bin\java HelloWorldApp
Hello World!
Then hit “Enter” to copy. Paste that at the end of the program you are turning in.