Post on 01-Jul-2015
description
1
Synapse India Reviews
"On the other hand, Java has already been a big in academic circles, where it has taken the place of the preferred tool for teaching the basics of good programming…"
Java Basics 2
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
Java Basics 3
Brief History of Java and Overview of Language
4
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.
Java Basics 5
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
Java Basics 6
More on How Java Works To run a Java program the bytecode in a .class file
is fed to an interpreter which converts the byte code to machine code for a specific chip (IA-32, PowerPC)
Some people refer to the interpreter as "The Java Virtual Machine" (JVM)
The interpreter is platform specific because it takes the platform independent bytecode and produces machine language instructions for a particular chip
So a Java program could be run an any type of computer that has a JVM written for it.– PC, Mac, Unix, Linux, BeaOS, Sparc
Java Basics 7
A Picture is Worth…
The Interpreter's are sometimes referred to as the Java Virtual Machines
The output of the compiler is .class file
Java Basics 8
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?
Java Basics 9
HelloWorld.java
/** * A simple program */
public class HelloWorld{
public static void main(String[] args){
System.out.println("HELLO CS307!");}
}
Java Basics 10
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
Java Basics 11
Attendance Question 1What does 6,967 * 7,793 equal?
A. 10,000
B. 23,756,201
C. 54,293,831
D. 2,147,483,647
E. - 2,147,483,648
Java Basics 12
Attendance Question 2How many factors does 54,161,329 have?
A. 2
B. 3
C. 4
D. 6
E. more than 6
Bonus question. What are they?
Java Basics 13
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)
Java Basics 14
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
Java Basics 15
Java LanguageReview of Basic Features
Java Basics 16
Basic Features Data Types
– primitives – classes / objects
Expressions and operators Control Structures Arrays Methods Programming for correctness
– pre and post conditions– assertions
Java Basics 17
Java Data Types
Java Basics 18
Identifiers in Java letters, digits, _, and $ (don't use $. Can confuse
the runtime system) start with letter, _, or $ by convention:
1. start with a letter2. variables and method names, lowercase with internal
words capitalized e.g. honkingBigVariableName3. constants all caps with _ between internal words e.g.
ANOTHER_HONKING_BIG_INDENTIFIER4. classes start with capital letter, internal words
capitalized, all other lowercase e.g HonkingLongClassName
Why? To differentiate identifiers that refer to classes from those that refer to variables
Java Basics 19
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 Basics 20
Java Primitive Data TypesData 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,647
long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807
float 32 bit floating point number
+ 1.4E-45 to+ 3.4028235E+38
double 64 bit floating point number
+ 4.9E-324 to+ 1.7976931348623157E+308
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