INTRODUCTION TO JAVA. Introduction to Java Java programming language ◦ Object-Oriented Programming ◦ Compiled to byte code of JVM Java virtual machine.

Slides:



Advertisements
Similar presentations
@2012 Android1 Java Review. Java  Java is an object-oriented language, with a syntax similar to C Structured around objects and methods A method is an.
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Date: Subject:Distributed Data Processing Name:Maria Br ü ckner.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CMT Programming Software Applications
CS Programming Language - JAVA Fall 2004 Sept. 15th.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Java.
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
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Programming Robert Chatley William Lee
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
CS107 Introduction to Computer Science Java Basics.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
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.
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.
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.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Object Oriented Software Development 4. C# data types, objects and references.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CS Programming Language - JAVA Fall 2004 Sept. 15th.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
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.
Java Programming Language Lecture27- An Introduction.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 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.
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Java Primer 1: Types, Classes and Operators
Internet and Java Foundations, Programming and Practice
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
Java programming lecture one
Introduction Enosis Learning.
Programming Language Concepts (CIS 635)
Introduction Enosis Learning.
Java Programming Language
Java Programming Language
Computer Programming-1 CSC 111
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
Presentation transcript:

INTRODUCTION TO JAVA

Introduction to Java Java programming language ◦ Object-Oriented Programming ◦ Compiled to byte code of JVM Java virtual machine (JVM) ◦ Java interpreter – interpret the compiled byte code ◦ Software simulated CPU architecture ◦ Cross-platform: support Linux, Windows, PalmOS…etc. Java runtime environment (JRE) ◦ Predefined set of java classes available to use ◦ Core Java APIs – basic utilities, I/O, graphics, network…

Java is portable, As long as there is a JVM compiled for that particular processor and OS Typical program, like C or C++, is compiled for a particular processor architecture and OS. “Write once, run everywhere!” ◦ Sun’s motto for Java

Bottom line: slow but safe Not suitable for high-performance computation ◦ Scientific computation, games, OS kernel ◦ Compiled to byte codes of JVM, not native machine language instructions ◦ New release of Java is improving the speed a lot  Just-in-time (JIT) compiler: convert byte codes to native machine language on the fly Very safe ◦ No pointer ◦ Automatic garbage collection ◦ Check array access bound

Java Java is an object-oriented language, with a syntax similar to C ◦ Structured around objects and methods ◦ A method is an action or something you do with the object Avoid those overly complicated features of C++: ◦ Operator overloading, pointer, templates, etc.

Getting and using java J2SDK freely download from ( All text editors support java ◦ Notepad, wordpad ◦ Just save to.java file Have IDEs that comparable to Visual Studio ◦ EditPlus (simple) ◦ JCreator (simple) ◦ Eclipse (more complicated)

Compile and run an application Write java class Hello containing a main() method and save in file “Hello.java” ◦ The file name MUST be the same as class name Compile with: javac Hello.java Creates compiled.class file: Hello.class Run the program: java Hello ◦ Notice: use the class name directly, no.class!

Hello World! /* Our first Java program – Hello.java */ public class Hello { //main() public static void main ( String[] args ) { System.out.println( "hello world!" ); } File name: Hello.java Command line arguments Standard output, print with new line

About class Fundamental unit of Java program All java programs are classes Each class define a unique kind of object ( a new data type) Each class defines a set of fields, methods or other classes public: modifier. This class is publicly available and anyone can use it.

Things to notice Java is case sensitive whitespace doesn’t matter for compilation File name must be the same as one of the class names, including capitalization! At most one public class per file If there is one public class in the file, the filename must be the same as it Generally one class per file

What is an object? Object is a thing An object has state, behavior and identity ◦ Internal variable: store state ◦ Method: produce behavior ◦ Unique address in memory: identity An object is a manifestation of a class

What is class? Class introduces a new data type A class describes a set of objects that have identical characteristics (data elements) and behaviors (methods). ◦ Existing classes provided by JRE ◦ User defined classes Once a class is established, you can make as many objects of it as you like, or none.

Simple example: class Person A Person has some attributes The class defines these properties for all people Each person gets his own copy of the fields Attributes = properties = fields

Class Person: definition class Person { String name; int height; int weight; public void printInfo(){ System.out.println(name+" with height="+height+", weight="+weight); } class ClassName{ /* class body goes here */ } class: keyword

Class Person: usage Person p1; //declaration p1 = new Person(); //create an object of Person p1.name= “David”; //access its field Person p2 = new Person(); p2.name=“Michael”; p1.printInfo(); p2.printInfo(); // error here??

Class Person Name: David height: 0 weight: 0 Name: Michael height: 0 weight: 0 p1 p2

Class Person: variables Person x; x=p1; x.printInfo(); x=p2; x.printInfo(); This gives the same output as previous code !

Class Person: variables Name: David height: 0 weight: 0 Name: Michael height: 0 weight: 0 p1 p2 x referencesobjects

Reference We call x, as well as p1 and p2, “reference” to the object Handles to access an object Reference itself is not accessible/manipulable ◦ Different from C/C++, cannot increment/decrement it Implemented as pointer+ ◦ Java runtime is watching all assignment to references

Reference Person p1; //only created the reference, not an object. It points to nothing now (null). p1 = new Person(); //create the object (allocate storage in memory), and p1 is initialized. p1.name=“David”; //access the object through the reference

More on reference Have distinguished value null, meaning pointing to nothing ◦ if( x==null) { … } Multiple references can point to one object When no reference point to an object, that object is never accessible again.

Class Person: problem p1.weight = 150; // too bad, but possible p1.weight = -20; // problem!! Need to ensure the validity of value. p1.setWeight(150); // OK, now p1’s weight is 150 p1.setWeight(-10); ******** Error, weight must be positive number

Class Person: add method class Person {... void setWeight(int w){ if(w<=0) System.err.println("***** error, weight must be positive number! "); else weight = w; }

Class Person: new problem p1.setWeight(-10); ******** Error, weight must be positive number p1.weight = -20; ****** ? How about we forgot to use the set function? Or we just don’t want to? Solution: just make the variable inaccessible from outside!

Class Person: private variable class Person{ private String name; private int weight; private int height; public void setWeight(int w){ if(w<=0) System.err.println("***** error, weight must be positive number! "); else weight = w; } Keyword private: no one can access the element except itself Keyword public: everyone can access the element

Class Person class Hello{ public static void main ( String[] args ) { Person p1= new Person(); p1.weight = -20; } >javac Hello.java Hello.java:5: weight has private access in Person p1.weight = -20; ^ 1 error

Java Basics: primitive types One group of types get special treatment in Java Variable is not created by “new”, not a reference Variable holds the value directly

Primitive types Primitive typeSizeMinimumMaximumWrapper type boolean1-bit —— Boolean char16-bitUnicode 0Unicode Character byte8-bit Byte short16-bit Short int32-bit Integer long64-bit Long float32-bitIEEE754 Float double64-bitIEEE754 Double

Primitive types All numerical types are signed! ◦ No unsigned keyword in Java The “wrapper” class allow you to make a non-primitive object to represent the primitive one ◦ char c =‘a’; ◦ Character C = new Character(c); ◦ Character C = new Character(‘a’);

Primitive types - boolean boolean can never convert to or from other data type, not like C or C++ boolean is not a integer if(0) doesn’t work in java Have to explicitly state the comparison ◦ if( x ==0) {

Primitive types - char Char is unsigned type The Character wrapper class has several static methods to work with char, like isDigit(), toUpperCase() etc.

Default values for primitive members When a primitive type data is a member of a class, it’s guaranteed to get a default value even if you don’t initialize it. Not true for those local variables!! ◦ There will be compile error if you use it without initialization Primitive typeDefault booleanfalse char ‘ \u0000 ’ (null) byte(byte)0 short(short)0 int0 long0L float0.0f double0.0d

Example class Hello{ public static void main ( String[] args ) { int x; System.out.println(x); } >javac Hello.java Hello.java:5: variable x might not have been initialized System.out.println(x); ^ 1 error