Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Programming Languages and Paradigms The C Programming Language.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Lab#1 (14/3/1431h) Introduction To java programming cs425
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
CS102 Data Types in Java CS 102 Java’s Central Casting.
A Brief Introduction to Java Programming the World Wide Web.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
String Escape Sequences
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Java and C++, The Difference An introduction Unit - 00.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CIS 270—Application Development II Chapter 13—Exception Handling.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Java Java Translation Program Structure
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Types in programming languages1 What are types, and why do we need them?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Introduction to Exceptions in Java CS201, SW Development Methods.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Object Oriented Programming Lecture 2: BallWorld.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Data types and variables
Exceptions 10-Nov-18.
Java Programming Language
Introduction to Java Programming
Chapter 1: Computer Systems
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Exceptions 10-May-19.
JAVA BASICS.
Exceptions 5-Jul-19.
Presentation transcript:

Java

Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent (except for J++) –this makes it great for Web programming It’s secure It isn’t C++

Applets and applications An applet is a program designed to be embedded in a Web page Applets run in a sandbox with numerous restrictions; for example, they can’t read files An application is a conventional program Java isn't a baby language anymore!

Comments are almost like C++ /* This kind of comment can span multiple lines */ // This kind is to the end of the line /** * This kind of comment is a special * ‘javadoc’ style comment */

Primitive data types are like C Main data types are int, double, boolean, char Also have byte, short, long, float boolean has values true and false Declarations look like C, for example, –double x, y; –int count = 0;

Expressions are like C Assignment statements mostly look like those in C; you can use =, +=, *= etc. Arithmetic uses the familiar + - * / % Java also has ++ and -- Java has boolean operators && || ! Java has comparisons = > Java does not have pointers or pointer arithmetic

Control statements are like C if (x < y) smaller = x; if (x < y) { smaller = x; sum += x; } else { smaller = y; sum += y; } while (x < y) { y = y - x; } do { y = y - x; } while (x < y) for (int i = 0; i < max; i++) sum += i; BUT: conditions must be boolean !

Control statements II Java also introduces the try statement, about which more later switch (n + 1) { case 0: m = n - 1; break; case 1: m = n + 1; case 3: m = m * n; break; default: m = -n; break; }

Java isn't C! In C, almost everything is in functions In Java, almost everything is in classes Typically, there is only one class per file There must be only one public class per file The file name must be the same as the name of the public class, but with a.java extension

Java program layout A typical Java file looks like: import java.awt.*; import java.util.*; public class Something { // field and method definitions go here... } The file must be named Something.java !

What is a class? Early languages had only arrays –all elements had to be of the same type Then languages introduced structures (called records, or structs ) –allowed different data types to be grouped Then Abstract Data Types (ADTs) became popular –grouped operations along with the data

So, what is a class? A class consists of –a collection of fields, or variables, very like the named fields of a struct –all the operations (called methods) that can be performed on those fields A class is like a type; it describes objects The objects are like values of that type

Name conventions Java is case-sensitive; maxval, maxVal, and MaxVal are three different names Class names begin with a capital letter All other names begin with a lowercase letter Subsequent words are capitalized: theBigOne Underscores are only used in constants Constants use capitals and underscores: BIT_SIZE These are very strong conventions!

The class hierarchy Classes are arranged in a hierarchy The root, or topmost, class is Object Every class but Object has a (one) superclass A class may have subclasses Each class inherits all the fields and methods of its superclasses

An example of a class class Person { String name; int age; void birthday ( ) { age++; System.out.println (name + " is now " + age); } } Person extends (is a subclass of) Object

Another example of a class class Driver extends Person { long driversLicenseNumber; Date expirationDate; }

Creating and using an object Person john; john = new Person ( ); john.name = "John Smith"; john.age = 37; Person mary = new Person ( ); mary.name = "Mary Brown"; mary.age = 33; mary.birthday ( );

Arrays are objects Person mary = new Person ( ); int myArray[ ] = new int[5]; –or: int myArray[ ] = {1, 4, 9, 16, 25}; –but not int myArray[5]; // syntax error –because the type of an array doesn't include its length String languages [ ] = {"Prolog", "Java"};

Errors are objects In most other languages, errors just "happen" In Java, when something goes wrong, an Error object is created and "thrown" You can "catch" the error if you wish, but if you don't, the program will halt with an error message

Exceptions are like Errors An Error is a bug in the program; for example, going outside the bounds of an array An Exception is a problem outside the program; for example, a missing You don't have to do anything about an Error But wherever an Exception might be thrown, you must handle it Java provides special syntax for this

try - catch try { // code that could throw an Exception } catch (SomeSpecificException e) { // code to deal with that Exception } catch (SomeMoreGeneralException e) { // code to deal with that Exception }

catch clauses The most general Exception is Exception catch clauses are tried in order, therefore –if catch (Exception e) {...} comes before catch (IOException e) {...}, then the latter will never be reached In the above, e looks like a parameter because it is a parameter--it's the Exception Exceptions have fields and methods

try - catch - finally try { // code that could throw an Exception } catch (SomeSpecificException e) { // code to deal with that Exception } catch (SomeMoreGeneralException e) { // code to deal with that Exception } finally { // code that always gets done }

A minimal exception handler try { line = myFile.readLine ( ); } catch (Exception e) { e.printStackTrace ( ); }

The End