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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

A Tutorial Introduction for C++ Programmers
Written by: Dr. JJ Shepherd
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
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.
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.
Lecture 5 Java Introduction CPE 401 / 601 Computer Network Systems slides are modified from Ricky Sethi.
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.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
1 Course Lectures Available on line:
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.
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.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
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
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.
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.
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.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
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
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
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)
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Chapter 3 Assignment Statement
C Basics.
An Introduction to Java – Part I
CS201: Data Structures and Discrete Mathematics I
Introduction to Java Programming
Presentation transcript:

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 support –error handling –no pointers! (garbage collection) –Threads are part of the language. –some support for common application level protocols (HTTP).

Netprog: Java Intro3 Java notes for C++ programmers Everything is an object. No code outside of class definition! Single inheritance –an additional kind of inheritance: interfaces All classes are defined in.java files –one top level public class per file

Netprog: Java Intro4 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: Java Intro5 First Program: Simp.java public class Simp { public static void main(String args[]) { System.out.println("Hello, Netprog"); } public class Simp { public static void main(String args[]) { System.out.println("Hello, Netprog"); }

Netprog: Java Intro6 Compiling and Running Simp.java javac Simp.java java Simp Simp.class compile run bytecode source code

Netprog: Java Intro7 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: Java Intro8 The Language Data types Operators Control Structures Classes and Objects Packages

Netprog: Java Intro9 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: Java Intro10 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: Java Intro11 Operators Assignment: =, +=, -=, *=, … Numeric: +, -, *, /, %, ++, --, … Relational: ==. !=,, =, … Boolean: &&, ||, ! Bitwise: &, |, ^, ~, >, … Just like C/C++!

Netprog: Java Intro12 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: Java Intro13 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: Java Intro14 Try/Catch/Finally try { // some code that can throw // an exception } catch (ExceptionType1 e1) { // code to handle the exception } catch (ExceptionType2 e2) { // code to handle the exception } finally { // code to run after the stuff in try // can handle other exception types }

Netprog: Java Intro15 Exceptions and Network Programming Exceptions take care of handling errors –instead of returning an error, some method calls will throw an exception. A little hard to get used to, but forces the programmer to be aware of what errors can occur and to deal with them.

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

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

Netprog: Java Intro18 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: Java Intro19 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: Java Intro20 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: Java Intro21 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: Java Intro22 Using objects Just like C++: –object.method() –object.field BUT, never like this (no pointers!) –object->method() –object->field

Netprog: Java Intro23 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: Java Intro24 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: Java Intro25 Notes on Arrays index starts at 0. arrays can’t shrink or grow. each element is initialized. array bounds checking (no overflow!) –ArrayIndexOutOfBoundsException Arrays have a.length

Netprog: Java Intro26 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: Java Intro27 Primitive vs. Reference Types int x=3; int y=x; Point p = new Point(2.3,4.2); Point t = p; There are two copies of the value 3 in memory There is only one Point object in memory!

Netprog: Java Intro28 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: Java Intro29 Packages You can organize a bunch of classes 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: Java Intro30 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.