Docsity

Java Class notes help, Lecture notes of Engineering Chemistry

Toggle sidebar
bg1
CS 307 Fundamentals of
Computer Science
Java Basics 1
Topic 2
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.1
www.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.html
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download Java Class notes help and more Lecture notes Engineering Chemistry in PDF only on Docsity!

CS 307 Fundamentals of Java Basics (^) 1

Topic 2

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. www.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.html

CS 307 Fundamentals of 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

CS 307 Fundamentals of Java Basics (^) 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. It was
  1. 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.]"

CS 307 Fundamentals of 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

CS 307 Fundamentals of 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

CS 307 Fundamentals of 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?

CS 307 Fundamentals of 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

CS 307 Fundamentals of

 - Java Basics 
  • Attendance Question
  • A. 10, What does 6,967 * 7,793 equal?
  • B. 23,756,
  • C. 54,293,
  • D. 2,147,483,
  • E. - 2,147,483,

CS 307 Fundamentals of 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)

CS 307 Fundamentals of 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

CS 307 Fundamentals of 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

CS 307 Fundamentals of Java Basics (^) 17

Java Data Types

CS 307 Fundamentals of 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';

CS 307 Fundamentals of Java Basics (^) 20 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