Introduction to Java
What is Java
Developed in the early 1990s by Sun Meant to be highly portable so it could be embedded into microwave ovens, cell phones, remote controls, etc Java was written with the C language Has similar syntax to C, C++, and JavaScript (JavaScript is totally different than Java)
Java is Platform Independent
Java can be run on any platform or any processor that has a JVM built for it. Java is an interpreted language, meaning that there is a machine/processor-dependent interpreter called the JVM that translates the standard Java code into instructions for the specific processor
Java Architecture
Platform Independence
In C++, only the source code is platform independent; the compiler and executable are platform-dependent Microsoft.NET products are Platform Dependent but Language Independent to a degree
Garbage collection and the JVM
In addition to converting your common bytecode to machine-dependent instructions, the JVM provides memory cleanup known as garbage collection The JVM will clean up after a program once components are no longer needed (i.e. you dont need stuff like set x = nothing).
Java Libraries and Open Source
Java includes many reusable tools and objects that are part of the language Additional open-source tools and classes can be found on the web that have been built by the open source community and can be reused in your applications
Java is object-oriented
Concept Description Class Method Job Description Task Example Instructor doLecture()
Package Collection of classes itt.common Object Instance of a class russellDobda
All code must be written as classes! Java is Case Sensitive!
3 types of Java Programs
1.
Java Application
Standalone; run from command line Runs in a web browser Downloaded from server and run client-side Resides and runs on web server
2.
Java Applet
3.
Java Servlet
Java Servlets and JSP
System Development
Always plan before you code. Understand the problem and plan a solution. Always test. Test driven development encourages writing tests first and creating automated build scripts that run the tests Dont wait until the last minute! Count on building a program over several days. Assume you will be rethinking and refactoring as you go; just like writing a novel
Phases of the Software Development Life Cycle Needs Analysis: The Why Requirements Analysis: The What
Define the problem
Design: The How Development
Define the solution in terms of business requirements Consideration of Who and Where High Level and Detailed Design Flow charts, psuedocode
Construction of the Software and unit testing Unit Testing (Unit, Integration)
Testing: User Acceptance, System Testing Implementation
Programming in Java
Java Coding - Comments
//this is a comment
/* this is a multi-line comment */
/** this is javadoc documentation * that contains standard information * @author Russell Dobda */
Java Coding Class definitions
public class MyClass { //your code here } Every java program must contain at least one Usually, each .java file will contain a single class The filename must match the class name
Java Application main() method
public class MyClass{ public static void main(String args[]) { System.out.println(Hello World!); } }
Question: what are the inputs and outputs of the main method?
Standards
Java ignores whitespace, so you can use as much or as little as you want, but please maintain readibility!! Bracket positioning (two standards, use either)
Primitive data types
Java includes 8 primitive (non-object) data types. They begin with a lower-case
boolean
byte char short
int
long float double
See page 50 for detailed descriptions
Declaring variables in Java
data_type variable_name; Examples
float float float float balance; balance = 2000.0; balance, deposit, withdrawal; balance=2000.0, deposit=50.0;
You should always initialize your variables to at least null or 0
The String Class
Java class that has many built-in methods for replace, find, case, etc Concatenate strings with the + operator
String instructorName = Mr. Dobda; System.out.println(Your instructor is + instructorName);
Note that System.out.println allows you to write a line of text to the output console
Escape sequences for strings
Sequence Result Example \ Quotes value = He said \Hello\
\n
\\
New line Backslash
value = Are you sure\n you want to do this? Value = Go to c:\\My Documents
Final variables (aka constants)
These values cannot be modified Use all caps with underscores
public final String SCHOOL_LOCATION = ITT;
If public, it can be referenced from other classes
System.out.println(You go to + MyClass.SCHOOL_LOCATION);
Naming conventions
Class: Descriptive noun or noun phrases. Starts with caps and uses caps for each word Method: start with lower case, use caps for each subsequent word Variable: same as methods. Be descriptive! Use single letter variable names only for loop indexes Final variable: CAPS_WITH_UNDERSCORE
Java Operators
See page 57 for full list and precedence
! Logical not == Same as
Question: what is the precedence of the following:
!= Not same as && Conditional and
|| = Conditional or Assignment
fTemp = 9.0/5.0 * cTemp + 32.0;
Assignment operator (=)
//valid assignments a = 1; b = 2; a = b = c = 0; double x = 32.0; //invalid assignment 32 = z;
Casting Variables
You can cast variables from one type to another when direct assignment is not an option: float f = (float)9.8765
Java considers everything with a decimal to be a double. Since float takes up less memory and holds up to 7 decimal places, you can cast 9.8765 as a float. There are better real-world examples out there than this, especially when you get into objects
int i = (int) f //what is i?
Java Integer Arithmetic
int a = 16, b=3, c, d; c = a / b; d = a % b;
The result of 16/3 is 5 remainder of 1; therefore, c = 5, d = 1
Java Arithmetic of differing types
int a = 3; double b = 2.0, c; //note decimal! c = a / b; When doing this math, Java will promote the number with less precision to the higher precision (i.e. int to double) to get a result of the higher precision (see table 2.10, pg 66) c ends up as .75
Increment and Decrement Operators
These provide a quick way to add or subtract 1 from a variable i++ or ++i will add 1 to i i-- or --i will subtract 1 from i When assigning values, there is a difference between i++ and ++i (see table 2.12 p70)
Accumulation Operators
Operator Example += sum += x Result sum = sum + x
-= *= /=
sum -= x sum *= x sum /= x
sum = sum x sum = sum * x sum = sum / x
Note that you dont NEED to use accumulation operators, but they are a nice shorthand.