Chapter 3 Study Notes.pdf
Document Details
Uploaded by Deleted User
Full Transcript
PDF-Information A Java application is a package with at least one class that contains a main() method. A Statement is a set of instructions. A semicolon is placed at the end of a statement. While curly braces are placed at the start and end of instructions, similar to how and are used in HTML. p...
PDF-Information A Java application is a package with at least one class that contains a main() method. A Statement is a set of instructions. A semicolon is placed at the end of a statement. While curly braces are placed at the start and end of instructions, similar to how and are used in HTML. package firstApplication declares the package name. Packages without a declared name are given the name Default. public class Greeting declares the class name. “public” means that the class is available to anyone. An application's controlling class contains the main()method. The controlling class is placed at the starting point. public static void main(String[] args) defines the main() method. A method is a named set of statements to perform a single task. The main() method is placed in the controlling class. Comments have no effect on the program. The purpose of a comment is to describe the purpose of each line, and helps avoid mistakes when used properly. /* *\ used to enclose single or multi line comments. These comments are typically placed at the start of a program to describe the application. // used for adding a comment at the end of a statement or to create a single line comment. Useful for debugging a program. /** *\ used for documentation. Mainly to describe a class or method. Multi line comments that describe a program, class, or method are sometimes referred to as comment blocks. Java code typed by a programmer is called source code. For source code to run it needs to be translated into a code that computers understand. This process is called compiling. Source code files have the extension.java. Compiled code files have the extension.class. Compiled Java source code is called bytecode. Bytecode is interpreted with a Java Virtual Machine(Java VM). The interpreter runs the bytecode instructions as it is read. Java VM can reside on any computer. Although bytecode is versatile, programs compiled in machine code run faster. Machine Code consists of just 1s and 0s and is different depending on the computer platform. A just-in-time compiler(JIT) converts bytecode to machine code. A program with syntax errors will not compile. Syntax errors happen in statements that violate the rules of Java. An example being that all Java statements must end with a semicolon. Output stream sends data to an output device. When displaying data to the standard output stream, one needs to use System.out.out contains print() and println() methods. The difference between the methods is how they control output. print() method displays data and leaves the insertion point at the end of the output. println() method moves the insertion point to the next line after displaying output. The print() and println() methods require arguments. An argument is data passed to a method for processing. The print() and println() arguments are strings. A string is a set of characters, which are enclosed by quotation marks. An escape sequence is a backslash(\). Escape sequences are used to display special characters. For example: \n newline \t tab(8 spaces) \\ backslash \” double quotation mark Format() method can be used in place of print() or println(). Format() method arguments include a format string and an argument list. A format string specifier takes the form: %[alignment][width]s At the start of specifiers skip for right alignment and include a minus sign(-) for left alignment. If [width] is greater than the number of characters in the string argument, then spaces fill out the output. A string longer than [width] characters is displayed, but any strings to the right are moved over. Code conventions are a set of guidelines for writing an application. Guidelines are about commenting, rules for naming methods, classes, and packages, and statement formatting. The code conventions introduced in this chapter are: An introductory comment should begin a program. This will include your name, class name, the date, and a brief statement about the program. Package names should begin with lowercase letters and then an uppercase letter should begin each word within the name. The name cannot contain spaces. Class names should be nouns and begin with an uppercase letter and an uppercase letter should begin each word within the name. The name cannot contain spaces. A comment block should be included before each class and method. Not typically placed before the main() method. Comments should not repeat what is written from the code. Statements in a method should be indented Open curly brace({) should be placed on the same line as the class or method declaration. Closing curly brace(}) should be on a separate line and aligned with the class or method declaration. (Same purpose as … in HTML) Java is an object oriented programming language. A Java application contains at least one class. An application is contained in a package.