Chp 2 Flashcards
A JAVA APPLICATION
Executes when you use teh java command to launch JVM
A COMMENT LINE BEGINS WITH…
//
Which is an end-of-line comment
*It terminates at the end of the line on which it appears
** JAVA WILL NOT READ THESE COMMENTS
Example:
// empty statement
TRADITIONAL COMMENTS
PG. 39
CAN SPREAD OVER SEVERAL LINES AND ARE DELIMITED BY /* AND */
THESE COMMENTS ARE NOT READ BY JAVA EITHER
JAVADOC
COMMENTS
PG. 39
DELIMITED BY /** AND */
ENABLE YOU TO EMBED PROGRAM DOCUMENTATION IN YOUR CODE.
GENTERATES HTML PAGES BASED ON THESE COMMENTS
COMPILER INGNORES ALL TEXT BETWEEN DELIMITERS
SYNTAX ERROR
PG. 39
AKA COMPILER ERROR, COMPILE TIME ERROR, COMPILATION ERROR
OCCURS WHEN THE COMPILER ENCOUNTERS CODE THAT VIOLATES JAVA’S LANGUAGE RULES
SIMILAR TO A GRAMMER ERROR IN NATURAL LANGUAGE
THE COMPILER RESPONDS BY ISSUING AN ERROR AND PREVENTING YOUR PROGRAM FROM COMPILING
BLANK LINES
SPACE CHARACTERS
TAB CHARACTERS
PG. 39
KNOWN AS WHITE SPACE
WHITE SPACE MAKES PROGRAMS EAISER TO READ AND IS IGNORED BY THE COMPILER
** IN A PROGRAM IT WOULD JUST BE A SKIPPED LINE
KEYWORDS
PG. 40
RESERVED FOR USE BY JAVA AND ARE ALWAYS SPELLED WITH ALL LOWERCASE LETTERS
KEYWORD CLASS
PG 40
INTRODUCES A CLASS DECLARATION
EX:
public class Welcome1
- public class being the keyword class*
- Welcome1 being the class name*
CLASS NAMES
BY CONVENTION ALL CLASS NAMES IN JAVA BEGIN WITH A CAPITAL LETTER AND CAPITALIZE THE FIRST LETTER OF EACH WORD THEY INCLUDE
E.G.
SampleClassName
JAVA CLASS NAME
- SERIES OF CHARACTERS CONSISTING OF LETTERS, DIGITS, UNDERSCORES (_) AND DOLLAR SIGNS ($)
- DOES NOT BEGIN WITH A DIGIT AND DOES NOT CONTAIN SPACES
- JavaClassNameSample
IS JAVA CASE SENSITIVE?
- YES
- UPPER AND LOWERCASE LETTERS ARE DISTINCT
THE BODY OF EVERY CLASS DECLARATION
PG. 40
- IS DELIMITED BY BRACES
- { AND }
A PUBLIC CLASS DECLARATION MUST BE SAVED IN….
- A FILE WITH THE SAME NAME AS THE CLASS FOLLOWED BY THE “.java” FILE NAME EXTENSION
- E.G public class Welcome1, would be saved as Welcome1.java
METHOD MAIN
PG.41
- STARTING POINT OF EVERY JAVA APPLICATION
- MUST BEGIN WITH public static void main( String [] args)
- otherwise the JVM will not execute
Ex:
{
// main method begins execution of java application
public static main void main ( String [] args )
}
METHODS
PG.41
- PERFORM TASKS AND RETURN INFO WHEN THEY COMPLETE THEM
- KEYWORD void, INDICATES THAT A METHOD WILL PERFORM A TASK BUT RETURN NO INFORMATION
public static void main ( String [] args )
*The parentheses after the identtifirer main indicate that its’s a program building block called method.
STATEMENTS
- INSTRUCT THE COMPUTER TO PERFORM ACTIONS
A STRING
PG.41
- IN DOUBLE QUOTES
- CALLED A CHARACTER STRING OR STRING LITERAL
- CAN NOT SPAN MULTIPLE LINES OF CODE
EX:
System.out.println( ” Welcome to Java Programming!” )
STANDARD OUTPUT OBJECT
PG.41
- DISPLAYS CHARACTERS IN THE COMMAND WINDOW
- System.out
Method
System.out.println
- DISPLAYS THE ARGUMENT IN THE COMMAND WINDOW FOLLOWED BY A NEW LINE CHARACTER TO POSITION THE OUTPUT CURSOR TO THE BEGINING OF THE NEXT LINE
TO COMPILE A PROGRAM
- YOU COMPILE A PROGRAM WITH THE COMMAND javac.
- IF THE PROGRAM CONTAINS NO SYTAX ERRORS, A CLASS FILE CONTAINING JAVA BYTECODES THAT REPRESENT THE APPLICATION CREATED.
- THE BYTECODES ARE INTERPRETED BY THE JVM WHEN YOU EXECUTE THE PROGRAM
- Open Command Window
- Change directory where program is stored using command cd to change directory
- once directory is found type javac ClasName.java
TO RUN AN APPLICATION
- TYPE JAVAc
- FOLLOWED BY THE NAME OF THE CLASS THAT CONTAINS MAIN
System.out.print
not
System.out.println
pg.44
- DISPLAYS ITS ARGUMENT AND POSITIONS THE OUTPUT CURSOR IMMEDIATELY AFTER THE LAST CHARACTER DISPLAYED
- println vs print (println positions at the next line)
BACKLASH
( \ )
PG.45
- A BACKLASH IN A STRING IS AN ESCAPE CHARACTER
- JAVA COMBINES IT WITH THE NEXT CHARACTER TO FORM AN ESCAPE SEQUENCE
- THE ESCAPE SEQUENCE \n represents the newline character
Ex:
{
public static void main ( String [] args)
{
System.out.println ( "Welcome **\n** to **\n** Java **\n** Programing!" ); }
}
Will Display on seperate lines
Welcome to java progaming!
System.out.printf
Method
pg.46
- Syestem.out.printf
- MEANS “FORMATTED”
- DISPLAYS FORMATTED DATA
- FIRST ARGUMENT IS A FORMAT STRING CONTAINING FIXED TEXT AND / OR FORMAT SPECIFIERS
- EACH FORMAT SPECIFIER INDICATES THE TYPE OF DATA TO OUTPUT AND IS PLACE HOLDERFOR A CORRESPONDING ARGUMENT THAT APPEARS AFTER THE FORMAT STRING
FORMAT SPECIFIERS
- BEGIN WITH % ADN FOLLOWED BY A CHARACTER THAT REPRESENTS THE DATY TYPE
- THE FORMAT SPECIFER % IS A PLACE HOLDER FOR A STRING
DISPLAYING MULTIPLE LINES WITH
System.out.printf
{
System.out.printf( “%s \ n%s \ n”,
"Welcome to", "Java Programing!" );
}
* Will Display
Welcome to Java Programing!
PACKAGES
PG.48
- NAMED GROUPS OF RELATED CLASSES
- COLLECTIVELY REFFERED TO AS THE JAVA CLASS LIBRARY OR JAVA APPLICATION PROGRAMING INTERFACE
- PREDEFINED CLASSES THAT YOU CAN REUSE
IMPORT DECLARATION
PG 48
- HELPS THE COMPILER LOCATE CLASS THAT’S USED IN A PROGRAM
USUALLY AT BEGINGING AFTER TITLE
import java.util.Scanner; // program uses Class Scanner
VARIABLE
- IS A LOCATION IN THE COMPUTERS MEMORY WHERE A VALUE CAN BE STORED FOR USE LATER IN A PROGRAM. ALL VARIABLES MUST BE DECLARED WITH A NAME AND TYPE BEFORE THEY CAN BE USED
**VARIABLE DECLARATION STATEMENT: ** Scanner input = new Scanner ( System.in );
- The = indicates that the Scanner variable input should be inititalized
- Variables should be initialized to prepare for use
VARIABLE NAMES
PG. 52
CORRESPOND TO LOCATIONS IN THE COMPUTERS MEMORY. EVERY VARIABLE HAS THE NAME, A TYPE, A SIZE, AND A VALUE.
A VALUE THAT’S PLACED IN A MEMORY LOCATION REPLACES THE LOCATIONS PERVIOUS VALUE, WHICH IS LOST.
ARITHMETIC OPERATORS
PG. 53
- + FOR ADDITION
- ** -** FOR SUBTRACTION
- * FOR MULTIPLICATION
- / FOR DIVISION
- % FOR REMAINDER
INTEGER DIVISION
PG 53
- YIELDS AN INTERGER QUOTIENT
- 7 / 4 evaluates to 1
- 17 / 5 evaluates to 3
- no rounding occurs
% yeilds the remainder
7 % 4 yields 3
17 % 5 yields 2
ARITHMETIC EXPRESSIONS
- MUST BE WRITTEN IN STRAIGHT LINE FORM
- EX: A OVER B must be written a / b
IF AN EXPRESSION CONTAINS NESTED PARENTHESES (A + B) * 2
- THE INTERMOST SET IS EVALUATED FIRST
RULES OF OPERATOR PRECEDENCE
- MULTIPLICATION, DIVISION, AND REMAINDER OPERATIONS ARE APPLIED FIRST
- ADDITION AND SUBTRACTION OPERATORS APPLIED NEXT
WHEN WE SAY THE OPERATORS ARE APPLIED FROM LEFT TO RIGHT, WE’RE REFFERING TO THEIR ASSOCIATIVITY.
SOME OPERATORS ASSOCIATE FROM RIGHT TO LEFT
REDUNDANT PARENTHESES
CAN MAKE AN EXPRESSION CLEARER
THE “IF” STATEMENT
PG.56
- MAKES A DECISION BASED ON A CONDITIONS VALUE ( TRUE OR FALSE)
EQUALITY OPERATORS
- ** (X == Y)** x is equal to y
- ( x != y ) x is not equal to y
RELATIONAL OPERATORS
- ( X > Y ) x is greater than y
- ( x < y ) x is less than y
- ( x >= y ) x is greater than or equal to y
- ( x <= y ) x is less or equal to y
EMPTY STATEMENT
IS A STATEMENT THAT DOES NOT PERFORM A TASK
A ______ BEGINS THE BODY OF EVERY METHOD, AND A _______ ENDS THE BODY OF EVERY METHOD
LEFT BRACE {
&
RIGHT BRACE }
THE ________ STATEMENT IS USED TO MAKE DECISIONS
IF STATEMENT
_______ BEGINS AN END-OF-LINE COMMENT
//
______, _____, AND ______ ARE CALLED WHITE SPACE
SPACE CHARACTERS
NEW LINES
TABS
______ ARE RESERVED FOR JAVA USE
KEY WORDS
JAVA APPLICATIONS BEGIN EXECUTION AT METHOD ________
METHOD MAIN
METHODS _____, ______, ______, DISPLAY INFO IN A COMMAND WINDOW
System.out.print
System.out.println
System.out.printf
COMMENTS CAUSE THE COMPUTER TO PRINT THE TEXT AFTER THE // ON THE SCREEN WHEN THE PROGRAM EXECUTES
TRUE OR FALSE
FALSE
COMMENTS DO NOT CAUSE ANY ACTION TO BE PERFORMED WHEN THE PROGRAM EXECUTES.
THEY ARE USED TO DOCUMENT PROGRAMS AND IMPROVE THE READIBILITY
ALL VARIABLES MUST BE GIVEN A TYPE WHEN THEY ARE DECLARED….
TRUE OR FALSE
TRUE
JAVA CONSIDERS THE VARIABLES NUMBER AND NuMber TO BE IDENTICAL
TRUE OR FALSE
FALSE
JAVA IS CASE SENSITIVE
SO THESE VAIRABLES ARE DISTINCT
THE REMAINDER OPERATOR ( % ) CAN BE USED ONLY WITH INTERGER OPERANDS
FALSE
THE REMAINDER OPERATOR CAN ALSO BE USED WITH NONINTERGER OPERANDS IN JAVA
THE ARITHMETIC OPERATORS * / % + AND - ALL HAVE THE SAME LEVEL OF PRECEDENCE
FALSE
THE OPERATORS * AND / AND % ARE HIGHER THAN OPERATORS + AND -
DECLARE VARIABLES
C, thisIsAVariable, q76354 and number
TO BE OF TYPE INT.
int c, thisIsAVariable, q76356, number;
PROMPT
THE USER TO ENTER AN INTEGER
System.out.print ( “Enter an integer” ) ;
INPUT AN INTEGER AND ASSIGN THE RESULTS TO INT VARIABLE VALUE.
ASSUME SCANNER VARIABLE INPUT CAN BE USED TO READ A VALUE FROM THE KEYBOARD
value = input.nextInt();
PRINT “THIS IS A JAVA PROGRAM”
ON ONE LINE IN THE COMMAND WINDOW.
USE METHOD System.out.println
System.out.println ( “This is a Java Program!” ) ;
“THIS IS A JAVA PROGRAM”
ON 2 LINES IN COMMAND WINDOW
FISRT LINE SHOULD END WITH JAVA
USE METHOD System.out.println
System.out.println ( “This is a Java \n Program” ) ;
“THIS A JAVA PROGRAM”
ON TWO LINES FIRST LINE ENDING WITH JAVA
USE METHOD System.out.printf & two %s format specifiers
System.out.printf ( “%s \ n%s \ n”, “this is a java”, “program” ) ;
IF THE VARIABLE NUMBER IS NOT EQUAL TO 7, DISPLAY “THE VARIABLE NUMBER IS NOT EQUAL TO 7”