Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Similar documents
Kickstart Intro to Java Part I

Getting started with Java

Introduction to Programming Using Java (98-388)

Introduction to Java

CS 11 java track: lecture 1

Introduction to Java

Lecture 1: Overview of Java

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

An overview of Java, Data types and variables

Computational Expression

Java Bytecode (binary file)

PROGRAMMING FUNDAMENTALS

Chapter 1 Getting Started

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Assumptions. History

Program Fundamentals

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS 61C: Great Ideas in Computer Architecture Introduction to C

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CS 231 Data Structures and Algorithms, Fall 2016

CSC 1214: Object-Oriented Programming

Lecture 2, September 4

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Array. Prepared By - Rifat Shahriyar

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Fall 2017 CISC124 9/16/2017

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

Programming Language Concepts: Lecture 2

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Variables and Java vs C++

Programming. Syntax and Semantics

Java Basic Programming Constructs

Java Programming. Atul Prakash

Computer Components. Software{ User Programs. Operating System. Hardware

a data type is Types

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

2 rd class Department of Programming. OOP with Java Programming

CSC Java Programming, Fall Java Data Types and Control Constructs

Array. Array Declaration:

CS260 Intro to Java & Android 03.Java Language Basics

CS Programming Language - JAVA. Fall 2004 Sept. 15th

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS 335 Lecture 02 Java Programming

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Intro to Computer Science II

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Page 1. Human-computer interaction. Lecture 1b: Design & Implementation. Building user interfaces. Mental & implementation models

Lecture Set 2: Starting Java

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Java is an objet-oriented programming language providing features that support

Lecture Set 2: Starting Java

Java Identifiers, Data Types & Variables

Lab5. Wooseok Kim

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Java Overview An introduction to the Java Programming Language

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Following is the general form of a typical decision making structure found in most of the programming languages:

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

1 Shyam sir JAVA Notes

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

VARIABLES AND TYPES CITS1001

Course information. Petr Hnětynka 2/2 Zk/Z

2. The object-oriented paradigm

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Course information. Petr Hnětynka 2/2 Zk/Z

The Java Series. Java Essentials Advanced Language Constructs. Java Essentials II. Advanced Language Constructs Slide 1

c) And last but not least, there are javadoc comments. See Weiss.

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CMSC131. Introduction to your Introduction to Java. Why Java?

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

About this exam review

Index COPYRIGHTED MATERIAL

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

COMP 202 Java in one week

Programming Language Concepts: Lecture 2

Chapter 1 Introduction to Java

Transcription:

Crash Course in Java Netprog: Java Intro 1 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 Intro 2 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 Intro 3 1

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 Intro 4 First Program: Simp.java public class Simp { public static void main(string args[]) { System.out.println("Hello, Netprog"); Netprog: Java Intro 5 Compiling and Running Simp.java javac Simp.java source code run compile java Simp Simp.class bytecode Netprog: Java Intro 6 2

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 Intro 7 Data types Operators Control Structures The Language Classes and Objects Packages Netprog: Java Intro 8 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 Intro 9 3

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 Intro 10 Operators Assignment: =, +=, -=, *=, Numeric: +, -, *, /, %, ++, --, Relational: ==.!=, <, >, <=, >=, Boolean: &&,,! Bitwise: &,, ^, ~, <<, >>, Just like C/C++! Netprog: Java Intro 11 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 Intro 12 4

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 Intro 13 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 Intro 14 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 Intro 15 5

The synchronized Statement Java is multithreaded! threads are easy to use. Instead of mutex, use synchronized: synchronized ( object ) { // critical code here Netprog: Java Intro 16 synchronized as a modifier You can also declare a method as synchronized: synchronized int blah(string x) { // blah blah blah Netprog: Java Intro 17 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 Intro 18 6

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 Intro 19 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 Intro 20 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 Intro 21 7

Using objects Just like C++: object.method() object.field BUT, never like this (no pointers!) object->method() object->field Netprog: Java Intro 22 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 Intro 23 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 Intro 24 8

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

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 Intro 28 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 Intro 29 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: Java Intro 30 10