Download Java Basics - Introduction to Computer Science - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Java Basics
"On the other hand, Java has already been a big win in
academic circles, where it has taken the place of Pascal as
the preferred tool for teaching the basics of good
programming…"
- The New Hacker's Dictionary version 4.3.
Agenda
• Brief History of Java and overview of language
• Solve a problem to demonstrate Java syntax
• Discuss coding issues and style via example
• Slides include more details on syntax
- may not cover everything in class, but you are
expected to know these
A brief history of Java
- "Java, whose original name was Oak, was developed as a part of the Green project at Sun. It was started in December '90 by Patrick Naughton, Mike Sheridan and James Gosling and was chartered to spend time trying to figure out what would be the "next wave" of computing and how we might catch it. They came to the conclusion that at least one of the waves was going to be the convergence of digitally controlled consumer devices and computers. "
- Applets and Applications
- "The team returned to work up a Java technology-based clone of Mosaic they named "WebRunner" (after the movie Blade Runner ), later to become officially known as the HotJavaTM browser. It was 1994. WebRunner was just a demo, but an impressive one: It brought to life, for the first time, animated, moving objects and dynamic executable content inside a Web browser. That had never been done. [At the TED conference.]"
How Java Works
- Java's platform independence is achieved by the use of the Java Virtual Machine
- A Java program consists of one or more files with a .java extension - these are plain old text files
- When a Java program is compiled the .java files are fed to a compiler which produces a .class file for each .java file
- The .class file contains Java bytecode.
- Bytecode is like machine language, but it is intended for the Java Virtual Machine not a specific chip such as a Pentium or PowerPC chip
A Picture is Worth…
The Interpreter's are sometimes referred to as the Java Virtual Machines
The output of the compiler is .class file
So What!
- The platform independence of Java may be a huge
marketing tool, but is actually of little use to people
learning Object Oriented Programming and Abstract
Data Types
- What is of use is the simplicity of the Java syntax and
programming concepts
- Java is a "pure" Object Oriented Language
- encapsulation, inheritance, and polymorphism
- all code must be contained in a class
- no free functions (functions that do not belong to some class) like C++, although someone who wants to write messy Java code certainly can
- Is OO the best programming paradigm?
More on Java Programs
- All code part of some class
public class Foo
{ //start of class Foo
/all code in here!/
} // end of class Foo
- The code for class Foo will be in a file named
Foo.java
- just a text file with the .java extension
- a class is a programmer defined data type
- A complete program will normally consist of
many different classes and thus many different files
What does 6,967 * 7,793 equal?
- Attendance Question
- A. 10,
- B. 23,756,
- C. 54,293,
- D. 2,147,483,
- E. - 2,147,483,
Example Problem
- Determine if a given integer is prime
- problem definition
- really naïve algorithm
- implementation
- testing
- a small improvement
- another improvement
- yet another improvement
- always another way ...
- what about really big numbers? (Discover AKS Primality Testing)
Error Types
- Syntax error / Compile errors
- caught at compile time.
- compiler did not understand or compiler does not allow
- Runtime error
- something “Bad” happens at runtime. Java breaks these into Errors and Exceptions
- Logic Error
- program compiles and runs, but does not do what you intended or want
Basic Features
- Data Types
- primitives
- classes / objects
- Expressions and operators
- Control Structures
- Arrays
- Methods
- Programming for correctness
- pre and post conditions
- assertions
Java Data Types
Data Types
- Primitive Data Types
- byte short int long float double boolean char
- stick with int for integers, double for real numbers
- Classes and Objects
- pre defined or user defined data types consisting of constructors, methods, and fields (constants and fields (variables) which may be primitives or objects.)
//dataType identifier;
int x;
int y = 10;
int z, zz;
double a = 12.0;
boolean done = false, prime = true;
char mi = 'D';
Java Primitive Data Types
Data Type
Characteristics (^) Range
byte (^) 8 bit signed integer -128 to 127 short (^) 16 bit signed integer -32768 to 32767
int (^) 32 bit signed integer -2,147,483,648 to 2,147,483, long (^) 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775, float (^) 32 bit floating point number
1.4E-45 to
3.4028235E+ double (^) 64 bit floating point number
4.9E-324 to
1.7976931348623157E+ boolean true or false (^) NA, note Java booleans cannot be converted to or from other types char (^) 16 bit, Unicode Unicode character, \u0000 to \uFFFF Can mix with integer types