Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network.

Slides:



Advertisements
Similar presentations
A Tutorial Introduction for C++ Programmers
Advertisements

Written by: Dr. JJ Shepherd
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
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.
Principles of Object-Oriented Software Development The language Java.
Java Syntax Primitive data types Operators Control statements.
1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Object-Oriented Programming Reflection (VRH 7. 3
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Lecture 5 Java Introduction CPE 401 / 601 Computer Network Systems slides are modified from Ricky Sethi.
Introduction to Java Programming Language Junji Zhi University of Toronto 1.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
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.
Java and C++, The Difference An introduction Unit - 00.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
May 9, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part I COMP346/ Operating Systems Revision 1.6 February 9, 2004.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
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.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
We will talking about story of JAVA language. By Kristsada Songpartom.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
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.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 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.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Object Oriented Programming Lecture 2: BallWorld.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Java Programming Language Lecture27- An Introduction.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Information and Computer Sciences University of Hawaii, Manoa
Concurrent Object-Oriented Programming Java (VRH 7.7,8.6)
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Internet and Java Foundations, Programming and Practice
Website: SSD1 Unit 2 Intro to Java Presentation 2.3 Website:
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Introduction to Java Programming
(Computer fundamental Lab)
Chap 1. Getting Started Objectives
Presentation transcript:

Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed Computing

Netprog 2002 Java Intro2 What is Java? A programming language. –As defined by Gosling, Joy, and Steele in the Java Language Specification A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware. A class library –Standard APIs for GUI, data storage, processing, I/O, and networking.

Netprog 2002 Java Intro3 Why Java? Network Programming in Java is very different than in C/C++ –much more language support –error handling –no pointers! (garbage collection) –threads are part of the language. –some support for common application level protocols (HTTP). –dynamic class loading and secure sandbox execution for remote code. –source code and bytecode-level portability.

Netprog 2002 Java Intro4 Java notes for C++ programmers Everything is an object. –Every object inherits from java.lang.Object No code outside of class definition! –No global variables. Single inheritance –an additional kind of inheritance: interfaces All classes are defined in.java files –one top level public class per file

Netprog 2002 Java Intro5 More for C++ folks Syntax is similar (control structures are very similar). Primitive data types similar –bool is not an int. To print to stdout: –System.out.println();

Netprog 2002 Java Intro6 First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World"); }

Netprog 2002 Java Intro7 Compiling and Running HelloWorld.java javac HelloWorld.java java HelloWorld HelloWorld.class compile run bytecode source code

Netprog 2002 Java Intro8 Java bytecode and interpreter bytecode is an intermediate representation of the program (class). The Java interpreter starts up a new “Virtual Machine”. The VM starts executing the users class by running it’s main() method.

Netprog 2002 Java Intro9 PATH and CLASSPATH The java_home/bin directory is in your $PATH If you are using any classes outside the java or javax package, their locations are included in your $CLASSPATH

Netprog 2002 Java Intro10 The Language Data types Operators Control Structures Classes and Objects Packages

Netprog 2002 Java Intro11 Java Data Types Primitive Data Types: –boolean true or false –char unicode! (16 bits) –byte signed 8 bit integer –short signed 16 bit integer –int signed 32 bit integer –long signed 64 bit integer –float, double IEEE 754 floating point not an int!

Netprog 2002 Java Intro12 Other Data Types Reference types (composite) –classes –arrays strings are supported by a built-in class named String string literals are supported by the language (as a special case).

Netprog 2002 Java Intro13 Type Conversions conversion between integer types and floating point types. –this includes char No automatic conversion from or to the type boolean ! You can force conversions with a cast – same syntax as C/C++. int i = (int) 1.345;

Netprog 2002 Java Intro14 Operators Assignment: =, +=, -=, *=, … Numeric: +, -, *, /, %, ++, --, … Relational: ==. !=,, =, … Boolean: &&, ||, ! Bitwise: &, |, ^, ~, >, … Just like C/C++!

Netprog 2002 Java Intro15 Control Structures More of what you expect: conditional: if, if else, switch loop: while, for, do break and continue (but a little different than with C/C++).

Netprog 2002 Java Intro16 Exceptions Terminology: –throw an exception: signal that some condition (possibly an error) has occurred. –catch an exception: deal with the error (or whatever). In Java, exception handling is necessary (forced by the compiler)!

Netprog 2002 Java Intro17 Try/Catch/Finally try { // code that can throw an exception } catch (ExceptionType1 e1) { // code to handle the exception } catch (ExceptionType2 e2) { // code to handle the exception } catch (Exception e) { // code to handle other exceptions } finally { // code to run after try or any catch }

Netprog 2002 Java Intro18 Exception Handling Exceptions take care of handling errors –instead of returning an error, some method calls will throw an exception. Can be dealt with at any point in the method invocation stack. Forces the programmer to be aware of what errors can occur and to deal with them.

Netprog 2002 Java Intro19 Concurrent Programming Java is multithreaded! –threads are easy to use. Two ways to create new threads: –Extend java.lang.Thread Overwrite “run()” method. –Implement Runnable interface Include a “run()” method in your class. Starting a thread –new MyThread().start(); –new Thread(runnable).start();

Netprog 2002 Java Intro20 The synchronized Statement Java is multithreaded! –threads are easy to use. Instead of mutex, use synchronized: synchronized ( object ) { // critical code here }

Netprog 2002 Java Intro21 synchronized as a modifier You can also declare a method as synchronized: synchronized int blah(String x) { // blah blah blah }

Netprog 2002 Java Intro22 Classes and Objects “All Java statements appear within methods, and all methods are defined within classes”. Java classes are very similar to C++ classes (same concepts). Instead of a “standard library”, Java provides a lot of Class implementations.

Netprog 2002 Java Intro23 Defining a Class One top level public class per.java file. –typically end up with many.java files for a single program. –One (at least) has a static public main() method. Class name must match the file name! –compiler/interpreter use class names to figure out what file name is.

Netprog 2002 Java Intro24 Sample Class (from Java in a Nutshell) public class Point { public double x,y; public Point(double x, double y) { this.x = x; this.y=y; } public double distanceFromOrigin(){ return Math.sqrt(x*x+y*y); }

Netprog 2002 Java Intro25 Objects and new You can declare a variable that can hold an object: Point p; but this doesn’t create the object! You have to use new: Point p = new Point(3.1,2.4); there are other ways to create objects…

Netprog 2002 Java Intro26 Using objects Just like C++: –object.method() –object.field BUT, never like this (no pointers!) –object->method() –object->field

Netprog 2002 Java Intro27 Strings are special You can initialize Strings like this: String blah = "I am a literal "; Or this ( + String operator): String foo = "I love " + "RPI";

Netprog 2002 Java Intro28 Arrays Arrays are supported as a second kind of reference type (objects are the other reference type). Although the way the language supports arrays is different than with C++, much of the syntax is compatible. –however, creating an array requires new

Netprog 2002 Java Intro29 Array Examples int x[] = new int[1000]; byte[] buff = new byte[256]; float[][] mvals = new float[10][10];

Netprog 2002 Java Intro30 Notes on Arrays index starts at 0. arrays can’t shrink or grow. –e.g., use Vector instead. each element is initialized. array bounds checking (no overflow!) –ArrayIndexOutOfBoundsException Arrays have a.length

Netprog 2002 Java Intro31 Array Example Code int[] values; int total=0; for (int i=0;i<value.length;i++) { total += values[i]; }

Netprog 2002 Java Intro32 Array Literals You can use array literals like C/C++: int[] foo = {1,2,3,4,5}; String[] names = {“Joe”, “Sam”};

Netprog 2002 Java Intro33 Reference Types Objects and Arrays are reference types Primitive types are stored as values. Reference type variables are stored as references (pointers that we can’t mess with). There are significant differences!

Netprog 2002 Java Intro34 Primitive vs. Reference Types int x=3; int y=x; Point p = new Point(2.3,4.2); Point t = p; Point p = new Point(2.3,4.2); Point t = new Point(2.3,4.2); There are two copies of the value 3 in memory There is only one Point object in memory!

Netprog 2002 Java Intro35 Passing arguments to methods Primitive types: the method gets a copy of the value. Changes won’t show up in the caller. Reference types: the method gets a copy of the reference, the method accesses the same object!

Netprog 2002 Java Intro36 Example int sum(int x, int y) { x=x+y; return x; } void increment(int[] a) { for (int i=0;i<a.length;i++) { a[i]++; }

Netprog 2002 Java Intro37 Comparing Reference Types Comparison using == means: –“are the references the same?” –(do they refer to the same object?) Sometimes you just want to know if two objects/arrays are identical copies. –use the.equals() method you need to write this for your own classes!

Netprog 2002 Java Intro38 Packages You can organize a bunch of classes and interfaces into a package. –defines a namespace that contains all the classes. You need to use some java packages in your programs –java.lang java.io, java.util

Netprog 2002 Java Intro39 Importing classes and packages Instead of #include, you use import You don’t have to import anything, but then you need to know the complete name (not just the class, the package). –if you import java.io.File you can use File objects. –If not – you need to use java.io.File objects.

Netprog 2002 Java Intro40 Sample Code Sum.java: reads command line args, converts to integer, sums them up and prints the result. Sum1.java: Same idea, this one creates a Sum1 object, the constructor then does the work (instead of main).

Netprog 2002 Java Intro41 More Samples Multiple Public classes: –need a file for each class. –Tell the compiler to compile the class with main(). automatically finds and compiles needed classes. Circle.java, CircleTest.java, Point.java, Threads.java, ExceptionTest.java