CISP 401 Chapter 2 Introduction to Java Application
Keywords (sometimes called reserved words) are
reserved for use by Java and are always spelled with all lowercase letters
Every variable has a name, a type, a size (in bytes), and value
number1, integer, 47
equality operator
== !=
The System.out.printf method
Displays formatted data uses this method to output the strings
\"
Double quote. Used to print a double quote character
Common Programming Error: A public class must be placed in a file that has the same name as the class (in terms of both spelling and capitalization) plus the .java extension; otherwise, a compilation error occurs.
For example, public class Welcome must be placed in a file named Welcome.java
After the calculation, we display the sum
Format specifier %d is a placeholder for an int value "Sum is %d\n", sum);
\t
Horizontal tab. Move the screen cursor to the next tab stop
* / % + - =
Precedence of arithmetic operators
The types int, float, double, and char are called Primitive types boolean, bute, char, short, int, long, float, and double
Primitive-type names are keyword and must appear in all lowercase letters
import java.util.Scanner scanner input = new Scanner(System.in);
Program uses class scanner (uses to read data) Create a scanner to obtain input from the command window
Prompt
it directs users to take specific actions
redundant parentheses
to make an expression clearer y = (a * x * x) + (b * x) + c;
A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM). Later in this section we'll discuss how to compile and run a Java application. First we consider a simple application that displays a line of text.
// Text-printing program public class Welcome1 { // main method begins execution of Java application public static void main( String[] args ) { System.out.println( " Welcome to Java Programming!" ); } // end method main } // end class Welcome1 Output: Welcome to Java Programming!
Performing Output with: System.out.println
to print the string of characters contained between the double quotation marks (but not the quotation marks themselves).
Format specifier begin with a percent sign % followed by a character that represents the data type
System.out.printf ( "%s\n%s\n". "Welcome to". "Java Programming! ): Welcome to Java Programming!
relational operator
>< >= <=
By convention, class names begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName).
A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. Some valid identifiers are Welcome1, $value, _value, m_inputField1 and button7. The name 7button is not a valid identifier because it begins with a digit, and the name input field is not a valid identifier because it contains a space.
The comment begins with //, indicating that it is an end-of-line comment—it terminates at the end of the line on which the // appears.
An end-of-line comment need not begin a line; it also can begin in the middle of a line and continue until the end
Integer division yield an integer quotient
Any fractional part in integer division is simply discarded
\\
Backlash. Used to print a backlash character
\r
Carriage return. Position the screen cursor at the beginning of the current line - do not advanced to the next line
When attempting to compile a program, if you receive a message such as "bad command or filename," "javac: command not found" or "'javac' is not recognized as an internal or external command, operable program or batch file," then your Java software installation was not completed properly. If you're using the JDK, this indicates that the system's PATH environment variable was not set properly. Please carefully review the installation instructions in the Before You Begin section of this book. On some systems, after correcting the PATH, you may need to reboot your computer or open a new command window for these settings to take effect
Each syntax-error message contains the file name and line number where the error occurred. For example, Welcome1.java:6 indicates that an error occurred at line 6 in Welcome1.java. The rest of the message provides information about the syntax error. When attempting to run a Java program, if you receive a message such as "Exception in thread "main" java.lang.NoClassDefFoundError: Welcome1," your CLASSPATH environment variable has not been set properly. Please carefully review the installation instructions in the Before You Begin section of this book. On some systems, you may need to reboot your computer or open a new command window after configuring the CLASSPATH
public class Welcome1 -- Declaring a Class
Every Java program consists of at least one class that you (the programmer) define. The class keyword introduces a class declaration and is immediately followed by the class name (Welcome1).
Good Programming Practice: Some organizations require that every program begin with a comment that states the purpose of the program and the author, date and time when the program was last modified.
Good Programming Practice: Using Blank Lines. Blank lines, space characters and tabs make programs easier to read. Together, they're known as white space (or whitespace). The compiler ignores white space. Use blank lines and spaces to enhance program readability
Normally, an identifier that does not begin with a capital letter is not a class name. Java is case sensitive—uppercase and lowercase letters are distinct—so value and Value are different (but both valid) identifiers.
In Chapters 2-7, every class we define begins with the public keyword. For now, we simply require this keyword. For our application, the file name is Welcome1.java. You'll learn more about public and non-public classes in Chapter 8.
Portion of statements that contain calculations are called Expressions
In fact an Expression is any portion of a statement that has a value associated with
The System.out object is known as the standard output object.
It allows a Java applications to display information in the command window from which it executes
\n
Newline. Position the screen cursor at the beginning of the next line.
Assignment operator = gets the value of input.nextInt()
Operator = is called a binary operators because it has two operands
A string is sometimes called a character string or a string literal. White-space characters in strings are not ignored by the compiler.
Strings cannot span multiple lines of code, but as you'll see later, this does not restrict you from using long strings in your code.
Forgetting one of the delimiters of a traditional or Javadoc comment is a syntax error. A syntax error occurs when the compiler encounters code that violates Java's language rules (i.e., its syntax). These rules are similar to a natural language's grammar rules specifying sentence structure.
Syntax errors are also called compiler errors, compile-time errors or compilation errors, because the compiler detects them during the compilation phase. The compiler responds by issuing an error message and preventing your program from compiling
A single statement can display multiple lines by using newline characters, which indicate to System.out's print and println methods when to position the output cursor at the beginning of the next line in the command window
System.out.println ( "Welcome\nto\nJava\nProgram!" ); Welcome to Java Program
We insert comments to document programs and improve their readability.
The Java compiler ignores comments, so they do not cause the computer to perform any action when the program is run
Java provides comments of a third type, Javadoc comments. These are delimited by /** and */.
The compiler ignores all text between the delimiters. Javadoc comments enable you to embed program documentation directly in your programs. Such comments are the preferred Java documenting format in industry. The javadoc utility program (part of the Java SE Development Kit) reads Javadoc comments and uses them to prepare your program's documentation in HTML format.
Declaring a Method public static void main( String[] args )
The parentheses after the identifier main indicate that it's a program building block called a Method. Java class declarations normally contain one or more methods. For a Java application, one of the methods must be called Main and must be defined as shown in line 7; otherwise, the Java Virtual Machine (JVM) will not execute the application. Methods perform tasks and can return information when they complete their tasks. Keyword Void indicates that this method will not return any information.
The standard input object, System.in enables applications to read bytes of information typed by user
The scanner translates these bytes into types (like ints) that can be used in a program
Method System.out.println displays (or prints) a line of text in the command window.
The string in the parentheses in line 9 is the Argument to the method
Java also has traditional comments, which can be spread over several lines as in /* This is a traditional comment. It can be split over multiple lines */
These begin and end with delimiters, /* and */. The compiler ignores all text between the delimiters.
A great strength of Java is its rich set of predefined classes that you can Reuse rather than reinventing the wheel.
These classes are grouped into Packages - named groups of related classes and are collectively referred to as the Java Class Library or the Java Application Programming Interface (Java API)
Programsa remember numbers and other data in the computer memory and access that data through program elements called --- Location in the computer memory where a value can be stored for use later in a program
Variables
Condition
an expression can either be true or false
If the program contains no syntax errors, this command creates a new file called Welcome1.class (known as the Class File for Welcome1)
containing the platform-independent Java bytecodes that represent our application
Scanner variable should be initialized in its declaration with the result of the expression to the equal sign
new Scanner(System.in) This expression uses new keyword to create a scanner object that reads characters typed by the user keyboard
Welcome to Java Programming! can be displayed several ways. Class Welcome2
public class Welcome2 { // main method begins execution of Java application public static void main( String[] args ) { System.out.print( " Welcome to "); System.out.println ( " Java Programming!" ); } // end method main } // end class Welcome1 Output: Welcome to Java Programming!
The entire line 9, including System.out.println, the argument "Welcome to Java Programming!" in the parentheses and the semicolon (;), is called a ______
statement