Java - chapter 1
Java keywords
50 words abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native short static strictfp super switch synchronized this throw throws transient try void volatile while
How do you create a block of code?
A block of code is started with a { and ends with a }
How do you create a multiline comment
A multiline comment begins with /* and ends with */
How do you create a single line comment
A single line comment beings with // and ends at the end of the line
What is a variable?
A variable is a named memory location. The contents of a variable can be changed during the execution of a program.
Static
Allows main() to be called before an object of the class has been created. This is necessary becasue main() is called by the JVM before any objects are made.
System.out
An object that encapsulates console output
if (w < h) { v = w * h w = 0 ]
Block starts at { and ends with } If w is less than h, both statements will be executed.
What is bytecode and why is it important to Java's use for Internet programming?
Bytecode is a highly optimized set of instructions that is exectued by the Java Virtual Machine. Bytecode helps Java achieve portability and security.
Println()
Can be used to display information
Source file
Compilatoin unit A text file with one or more classifications Java compiler requires that a source file use the .java filename extension. The code must reside inside a class. The name of the main class should match the name of the file that holds the program.
Loop example int count; for(count = 0; count < 5; count = count+1)
Count is the loop control variable Sets to zero in initalization Each iteration does the condtional test count < 5 If true, the loop continues increasing by 1 until it reaches 5.
App steps
Create a IDE project Add code to generate the source file Compile the source file into a .class file Run the program
var2 = var1 / 2;
Divides var1 by 2 then stores the result in var2. Could also do addition, subtraction, multiplication, and division.
Three main principles of object oriented programming?
Encapsulation Polymorphism Inheritence
==
Equal to
>
Greater than
Java platform
Has the java virtual machine and the java application programming interface JVM - bae for the java platform
++
Increases operand by 1 count++; instead of count + 1
Indentation practices
Indent one level after each opening brace and move out one level after closing a brace.
Console I/O
Is used mostly for simple utility programs ans server side code
print()
Is used to display the string on the same line
Deployment technology
JDK software provides standard mechanisms like Java web start software and Java plug in software to deploy your apps to end users.
Integration libraries
Java IDL API, JDBC, API, Java Naming and Directory Interface API, Java RMI, Java Remote Method, etc. enable database access and manipulation of remote objects.
Where do java programs begin execution?
Java programs begin execution at main()
Main () method
Java subroutine is a method. It's the line the program is executing. All java apps begin execution by calling main().
User interface toolkits
JavaFX, Swing, Java 2D toolkits allow you to create Graphical user interfaces
<
Less than
<=
Less than or equal to
Identifiers
Name given to user defined items like methods and variables. Can start with any letter, underscore, or a dollar sign. Then a letter, digit, dollar sign, or underscore. Underscore is used to make it easier to read Can't start with a digit or java keywords Name should reflect the meaning or usage of the item
!=
Not equal
--
Operand decreases by 1
Stystem
Predefined class that provides access to the system
Private
Prevents a member from being used by code defined outside of its class
Development tools
Provide everything you need. Main tools are javac compiler and javadoc documentation tool
Application programming interface
Provides core functionality of the Java programming language. Offers a wide array of ready to use classes. Is very large.
Single line comment
Starts with // and ends at the end of hte line Usually programmers use this for brief line by line descriptions
In main() there is only one parameter
String args[] Declars a parameter named args This is an array of objects of type string
string
String objects store sequences of characters
Public keyword
The access modifier, determines how other parts of the program can access the members of teh class. Public mean the member can be accessed by code outside the class in which it is declared.
var1 = 1024; //
The assignment operator is the equal sign. It copies the value on its right side into the variable on its left
Parameters
The main() is the method called when a Java pp begins. Any information you need to pass a method is received by variables specified within the parentheses following the name of the method. These are called parameters. if there are no paramenters, include empty parentheses.
System.out.println("var1 contains " + var1) ;
The plus sign causes the value of var2 to be displayed after the string that proceeds it.
int var1; // this declares a variable
This declares a variable called var2 of type interger
Fractional components
Two floating point types Float and double for single and double precision values Double is used most often
After comments is class
Uses keyword class to declasre a new class is being identified. Begins with { and ends with } In between the braces are members of the class.
Comments
You can enter comments if you'd like. Usually it explains the what the program does. There are three styles of comments. Multiline begin with /* and end in */. Compiler ignores whatever is in between those.
Loop
You can repeatedly execute a sequence of code by creating a loop. There are various loop constructs.
System
a class predefined by java
The java compiler creates
a file that contains the bytecode versoin of the program. Bytecode must be executed by teh Java virtual machine.
Code block
a grouping of two or more statements Enclosed in curly braces Becomes a logical unit that can be used in any place single statement can. Link statement together for clarity and efficiency
args
arguments
[ signals the start of main()'s
body All the code in a method will occur between the method's opening curly brace and closing curly brace
The + operator
chains together items within a single println() statement
Arrays
collections of similar objects
Variable names cannot start with a
digit
Moon's gravity is 17 percent of earth's. Write a program for your weight on the moon.
double moonweight; double earthweight; earthweight = 165; moonweight = earthweight * 0.17; System.out.println(earthweight + " earth-pounds is equivalent to " + moonweight + " moon-pounds.");
for loop
for(initialization; condition; iteration) statement; in the most common form of loops is the for loop. initialization sets a loop control variable to an initial value. The conditon is a Boolean expressoin that tests the loop control variable. If true, the for loop continues to iterate. If false, the loop terminates. Iteration - how the loop control variable is changed each time the loop iterates.
General form of the for
for(initialization; condition; iteration) statement; initialization is the assignment of value to a variable. Condtion to check for true or false Iteration - what to do if true
>=
greater than or equal to
General form of if statement
if(condition) statement;
All java statements end
in a semicolon
Can declare multiple variables using the same declaration statement
int var1, var2; // both declared using one statement Separate names b commas
Variable
is a named memor ylocation that can be assigned a value. Can be changed during the execution of a program.
All variables must
must be declared before they are used The type of values the variable can old must also be specified
System.out.print("var2 contains var1 / 2: "); System.out.println(var2);
print() displays the string "var2 contains var1 /2:" The next output will start on the same line
Print a blank line
println() with no arguments
When you execute Java interpreter, you are
specifiying the name of hte class. It will search for a file by that name with a .class extension then execute the code in the specified class.
Void
tells the computer that main() does not return a value
;
terminates statements Ends a logical entity NOT used to end a block Can write multiple statements on the same line if they're separated with a ; Ex. x=y; y=y+1; System.out.println(x + " " + y); Can also break really long lines up to make them more readable.
When java source code is compiled, each class is put into its own output file named after
the class using hte .class extension Give java source file the same name as the class the contain so the source file name will match the name of hte .class file.
Name of main class should match
the name of hte file that holds the program. Make sure the capitalization matches as well, it's case sensitive.
Out
the output system stream that is connected to the console
Declare a variable
type var-name Type is the type of variable being declared var-name is the name of the variable
Integer values
whole number values, precede the name of a variable with the keyword int
All source code is first
written into plain text files ending in .java. Those source files are then compiled into .class files by javac compiler, this contains bytecodes. The java launcher tool then runs your application with the Java virtual machine.
The if statement
you can selectively execute part of a program through if. if(condition) statement; If true, it's executed If false, it's skipped