Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 More Syntax; Working with Objects.
Advertisements

Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Introduction to Java 2 Programming Lecture 5 Quick Recap; Error-handling.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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.
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Fundamental Programming Structures in Java: Strings.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java and C++, The Difference An introduction Unit - 00.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
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.
Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 7: Characters, Strings, and the StringBuilder.
Session 7 Methods Strings Constructors this Inheritance.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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++)
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Topic: Classes and Objects
Object Based Programming
Object Oriented Programming in java
Applying OO Concepts Using Java
Anatomy of a Java Program
String Class.
Chapter 7 Objects and Classes
Presentation transcript:

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools

Overview More Syntax –Constants, Arrays –Strings, the Main Method The Object Lifecycle Java Programming Tools Practical Exercises

More Syntax…Constants Unlike other languages, Java has no Const keyword Must use a combination of modifiers to make a constant –static – indicates a class variable. Its not owned by an individual object –final – to make a variable with a single value. Can be assigned to once –E.g. public final static int MY_CONSTANT = 0;

More Syntax…Arrays Fairly straightforward: –int[] myArray; –int[] myArray = {1, 2, 3}; –myArray[1]; –myArray[2] = 4; Arrays have a length property, which holds their size.

More Syntax…Strings Strings are objects The compiler automatically replaces any string literal with a String object –E.g. my String becomes new String(my string); Strings are immutable –Cant be changed once created… –..but can be concatenated (using +) to create new strings

More Syntax..Strings, StringBuffers Compiler automatically replaces String concatenation with a StringBuffer object –E.g. my String + other String becomes… –new StringBuffer(my String).append(other String).toString(); Take care with String concatenation –Explicitly using a StringBuffer is often more efficient –Can reuse buffer rather than discarding it

More Syntax…The main method To turn a class into an application, give it a main method: –public static void main(String[] args) Must be of this format Can then be invoked from the command-line Try to minimise the amount of code in the main method: –Create objects and invoke their behaviour

Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

Object Life-Cycle -- Creation Objects are created with the new operator –E.g. new String(my String); Causes a constructor to be invoked –Constructor is a special method, used to initialise an object –Class often specifies several constructors (for flexibility) –new operator chooses right constructor based on parameters (overloading)

Object Life-Cycle -- Creation public class MyClass { private int x; public MyClass(int a) { x = a; } public MyClass(String number) { x = Integer.parseInt(number); } Can then create an instance of MyClass as follows: MyClass object = new MyClass(5); //first constructor MyClass object2 = new MyClass(5); //second constructor

Object Life-Cycle -- Destruction No way to explicitly destroy an object –So no equivalent to C++ destructor Objects destroyed by the Garbage Collector –Once they go out of scope (I.e. no longer referenced by any variable) No way to reclaim memory, entirely under control of JVM –There is a finalize method, but its not guaranteed to be called (so pretty useless!) –Can request that the Garbage Collector can run, buts its free to ignore you

Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

Java Programming Tools The CLASSPATH –Common source of frustration! –Must set this for any of the java tools to work correctly. –Similar to PATH, but finds class files rather than executables –Basically a list of directories and Jar files in which the JVM will look for referenced classes –set CLASSPATH=%CLASSPATH%;c:\intro2java\bin Compiler and JVM executables –java, javac –Both found in %JAVA_HOME%\bin

Java Programming Tools -- Javadoc Javadoc –Automatically generates HTML documentation from Java source code –Extremely flexible. Can be customised in a number of different ways, including adding special tags –Very efficient way to produce development documentation for your application.

Java Programming Tools -- Javadoc /** * A simple Calculator * Leigh Dodds */ public class Calculator { /** * Adds two numbers together and returns the result */ public int plus(int x, int y) { return x + y; }

Java Programming Tools - Jar Java ARchive –Basically a zip file, used to package java classes –E.g. for delivery as an applet or application Manifest –An index of the contents of a jar file –Major benefit is indicating which class holds the main method –Allows application to be launched automatically from a jar file.

Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

The Calculator