Introduction to Java

Similar documents
Lecture 1: Overview of Java

CSE 421 Course Overview and Introduction to Java

CS 11 java track: lecture 1

CS 231 Data Structures and Algorithms, Fall 2016

Certified Core Java Developer VS-1036

Introduction to Java. Nihar Ranjan Roy.

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

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

CHAPTER 1. Introduction to JAVA Programming

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

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

Introduction to Programming (Java) 2/12

CS11 Java. Fall Lecture 1

Introduction to Programming Using Java (98-388)

Selected Questions from by Nageshwara Rao

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java


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

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Java Bytecode (binary file)

Pace University. Fundamental Concepts of CS121 1

Java Language. Programs. Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs.

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

Programming. Syntax and Semantics

CT 229. CT229 Lecture Notes. Labs. Tutorials. Lecture Notes. Programming II CT229. Objectives for CT229. IT Department NUI Galway

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

Answer1. Features of Java

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

From C++ to Java. Duke CPS

Assumptions. History

Kickstart Intro to Java Part I

Selected Java Topics

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

Outline. Java Compiler and Virtual Machine. Why Java? Naming Conventions. Classes. COMP9024: Data Structures and Algorithms

PROGRAMMING FUNDAMENTALS

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Java Programming. Manuel Oriol, March 22nd, 2007

Programming overview

S8352: Java From the Very Beginning Part I - Exercises

Lecture 1: Introduction to Java

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

Java Professional Certificate Day 1- Bridge Session

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

Question. 1 Features of Java.

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

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Getting started with Java

Class List. Java Must Have. Class Goals. Class Goals. Schedule

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

13 th Windsor Regional Secondary School Computer Programming Competition

History Introduction to Java Characteristics of Java Data types

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

NetBeans IDE Java Quick Start Tutorial

Advanced Object-Oriented Programming Introduction to OOP and Java

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Chapter 1 Introduction to Java

Introduction to Java

CS Exam 1 Review Suggestions

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Chapter 1 Introduction to Computers, Programs, and Java

Sri Vidya College of Engineering & Technology

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Course Outline. Introduction to java

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

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

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

Introduction to Java

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Special Topics: Programming Languages

1 Shyam sir JAVA Notes

Crash Course Review Only. Please use online Jasmit Singh 2

CSCI 355 LAB #2 Spring 2004

Chapter 6 Introduction to Defining Classes

CS 335 Lecture 02 Java Programming

Lecture 2, September 4

CSCI 355 Lab #2 Spring 2007

Building Java Programs. Introduction to Programming and Simple Java Programs

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

2 rd class Department of Programming. OOP with Java Programming

COMP 250 Winter 2011 Reading: Java background January 5, 2011

1. Introduction to Java for JAS

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

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

Programming Language Concepts: Lecture 2

STRUCTURING OF PROGRAM

CompSci 125 Lecture 02

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)

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

15CS45 : OBJECT ORIENTED CONCEPTS

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Chapter 1 Introduction to Java

Programming Language Basics

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

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

20 Most Important Java Programming Interview Questions. Powered by

Introduction Basic elements of Java

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Transcription:

Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1

Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn how to use the Java compiler (javac) and interpreter (java). o Practice: You will write, compile and execute your first Java program You will write your program using emacs You will call the Java compiler and interpreter through the command line of a Unix shell. 22/01/2010 EPL 233 2

What is Java? Programming language developed by Sun Microsystems o Simple o Object Oriented o Distributed o Robust o Secure o Architecture Neutral o Portable o Interpreted o High Performance o Multithreaded o Dynamic o Network savvy 22/01/2010 EPL 233 3

Why Java? o The Java Language has many good design features secure, safe (with respect to bugs), object-oriented, familiar (to C C++ and even Fortran programmers). o Good set of libraries: networking, multimedia, from graphics to math functions. o Best available electronic and paper training resources. o Children will learn Java as it is a social language with natural graphical hello world. 22/01/2010 EPL 233 4

Why Java? o Java is rapidly getting the better (the best!) Integrated Development Environments (IDEs) for programs. o Java is naturally integrated with network and universal machine supports potentially powerful write once-run anywhere model. o Easy to teach I use it to help students understand client/server programming and concurrency (dining philosophers). o There is a large and growing trained labour force. 22/01/2010 EPL 233 5

Java: Key Technical Ideas o A Better Language: Simplicity and C/C++ compatibility promote fluency; GC and Threads allow software components; Platform independence saves time; Strong typing catches errors up front; Declared exceptions forces coverage in code. o Scalable Applications; Threads for parallel speedup; patterns in the large. Dynamic linking allows simple apps to grow; Range of implementations from JavaCard to HotSpot. 22/01/2010 EPL 233 6

The Java platform o The execution environment of Java programs Java VM is platform dependent Java API is platform independent 22/01/2010 EPL 233 7

22/01/2010 EPL 233 8

Phase 1 Editor Disk Program is created in the editor and stored on disk. Phase 2 Phase 3 Compiler Class Loader Disk Primary Memory Compiler creates bytecodes and stores them on disk. Phase 4 Disk Bytecode Verifier... Primary Memory Class loader puts bytecodes in memory. Bytecode verifier confirms that all bytecodes are valid and do not violate Java s security restrictions.. Primary Interpreter reads Interpreter Memory Phase 5 bytecodes and translates them into a language that the computer can understand, possibly. storing data values as. the program executes. 22/01/2010 EPL 233 9...

Garbage collection o Automatic Garbage Collection Performed by the Java VM Objects that are not referenced are removed o To destruct an object manually make it equal to null. It will be collected by the Garbage Collector o The Garbage Collector releases memory allocated with the new keyword (constructor call) 22/01/2010 EPL 233 10

Classes and Packages o Class Defines an object The same as in C++ o Package Directory of classes As libraries in C++ 22/01/2010 EPL 233 11

Java Documentation o Can be downloaded from Sun and installed on the local computer o Can be found online at http://java.sun.com/ o Contain the properties and methods of every object as well as the inheritance tree 22/01/2010 EPL 233 12

Syntax & common commands o Java syntax is very similar to that of C/C++ o Very common commands: extends inherits from (no multiple inheritance) System.out.println(); print to standard output import include How import works: import java.awt.*; will include all subclasses of the java.awt package with their methods import java.awt.button; will include only the methods of the java.awt.button class 22/01/2010 EPL 233 13

Java Syntax Tips o Variables can be declared anywhere in the code. They have to be initialized before use Their scope is only within the code fragment enclosed by { } in which they were declared. Example: for(int i=0;i<10;i++) { scope of i } o Foo x; x = new Foo(); is equivalent to: Foo x = new Foo(); 22/01/2010 EPL 233 14

Program Syntax o General Java Syntax: o o Class declaration: public class mybutton extends Button{ //variables, constructors and methods go here } public, private static,abstract, final class mybutton extends Button implements interface Method declaration: public void changecolor(color x) { //code goes here } public, private void, int, Integer, String changecolor 22/01/2010 EPL 233 15

Program Syntax o o o Constructor declaration: public mybutton() { //code to initialize the button } No return type in the constructor. The constructor is usually public, but not necessarily In a Java application (not applet) the main method must be declared. This is the first method executed when the program starts The syntax of the main method cannot be changed: public static void main(string[] args) { } args is an array containing the command line arguments 22/01/2010 EPL 233 16

Program Syntax o General program syntax: import java.awt.*; public class mybutton extends Button{ int a; String str; public mybutton() { //constructor //construct the object } public void changecolor(){ //a method this.setcolor(color.red); } }//end class 22/01/2010 EPL 233 17

Primitive types o There are 8 primitive types in Java. int vs Integer float vs Float double vs Double long vs Long short vs Short char vs Character boolean vs Boolean byte vs Byte 22/01/2010 EPL 233 18

Creating a Java program o Required software (minimum): Java Development Kit for your platform (includes the Java compiler, Java Virtual Machine, Appletviewer, Libraries and other utilities) A text editor o Recommended software: An IDE (Integrated Development Environment) There are many IDEs available (e.g. NetBeans, Eclipse, etc.) We will use Eclipse o The CLASSPATH variable Tells the Java compiler and Java virtual machine where the libraries are stored It should include the current directory (.) Usually set in a batch file for easy execution (more on this in a later lab) 22/01/2010 EPL 233 19

Creating Your First Application - HelloWorldApp To create this program, you will: o Create a Java source file (*.java). o Compile the source file into a bytecode file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. o Run the program contained in the bytecode file. The Java VM is implemented by a Java interpreter, java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand. 22/01/2010 EPL 233 20

Create a Java Source File o If you are NOT USING an IDE: The Java files you create should be kept in a separate directory (with mkdir). Start a text editor. Type the code and store it in a file HelloWorldApp.java o If you are USING an IDE: Each IDE has its own procedures Generally you create some sort of project and you add your classes (code) under that project We will demonstrate this with Eclipse 22/01/2010 EPL 233 21

HelloWorldApp.java /** * The HelloWorldApp class implements * an application that simply displays * "Hello World!" to the standard output. */ public class HelloWorldApp { } public static void main(string[] args){ } System.out.println( Hello World! ); 22/01/2010 EPL 233 22

Compile the Source File o If you are NOT USING an IDE: javac HelloWorldApp.java If your prompt reappears without error messages, congratulations. You have successfully compiled your program. A HelloWorldApp.class file is created o If you are USING an IDE: Depends on your IDE Eclipse does background compiling as you write your code Possible to instruct full code rebuild Most (if not all) IDEs allow you to export your.java and.class files 22/01/2010 EPL 233 23

Run the Program (from a shell) o java HelloWorldApp 22/01/2010 EPL 233 24