INTRO TO JAVA. WHAT THIS LECTURE IS & ISN'T It ISN'T A complete how-to of everything Java We'll see new features of Java all semester It IS Enough to.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Lecture 2: Object Oriented Programming I
Object Oriented Programming Teguh Sutanto Si | STIKOM Surabaya
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSC3170 Introduction to Database Systems
Java and C++, The Difference An introduction Unit - 00.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CIT 590 Intro to Programming First lecture on Java.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
File IO Basics By Dan Fleck Coming up: Data Streams.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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?
Lecture 5 I/O and Parsing
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
RUBY by Ryan Chase.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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++)
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Written by: Dr. JJ Shepherd
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Computer Organization and Design Pointers, Arrays and Strings in C
Objects as a programming concept
Intro to ETEC Java.
Crash course in the Java Programming Language
3 Introduction to Classes and Objects.
CSE 374 Programming Concepts & Tools
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Objects as a programming concept
CISC124 Labs start this week in JEFF 155: Meet your TA.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Lecture Note Set 1 Thursday 12-May-05
Programming Language Concepts (CIS 635)
Java Review: Reference Types
Primitive Types Vs. Reference Types, Strings, Enumerations
Tutorial C#.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Starting JavaProgramming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Variables Title slide variables.
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Java Programming Review 1
Session 2: Introduction to Object Oriented Programming
Introduction to Data Structure
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Java Basics Data Types in Java.
Web Design & Development Lecture 8
In this class, we will cover:
Java’s Central Casting
LCC 6310 Computation as an Expressive Medium
Chapter 3 Introduction to Classes, Objects Methods and Strings
ECE 122 April 14, 2005.
String Class.
Presentation transcript:

INTRO TO JAVA

WHAT THIS LECTURE IS & ISN'T It ISN'T A complete how-to of everything Java We'll see new features of Java all semester It IS Enough to get you through the first lab (hopefully) If I neglect to cover something…ask! Give you a taste of some major concepts, especially the OOP-nature of Java Basic I/O (including Files) Enough to show you the pace / style of lectures How to survive it Follow along If you're a fast typist, you can do it with me. Or just watch and download the project afterwards. Ask lots of questions Make observations Esp. comparisons to other languages you know (Python, MathPiper) Caffeine??

OUTLINE 1.Hello World 2.Variables 3.Console I/O 4.File I/O 5.Arrays 6.Intro to OOP

1. HELLO WORLD Do it on the computer ( example01a_hello_world ) Observations? Java is very "wordy" You have to create a class (Java is very OOP-based)

2. PRIMITIVES AND OBJECTS Java uses explicit declaration of variables. Unlike Python (and MathPiper?) In Java, most things are objects but there are a few primitives Primitives: long, int, char, byte, float, double and boolean Can assign literals to them (without new) More efficient internally There are Object-versions of each: Object-types: Long, Int, Char, Byte, Float, Double, Boolean Must allocate memory for them. The variable is a reference to that memory. Objects all have methods. Objects are derived from the Object base class Comparison (==) doesn’t always work as you’d expect. Java normally auto-converts between the two. Strings are kind of a hybrid Has methods Internally a fixed-size char array. Can assign literals to it. Do example ( example01b_variables ) Observations?

3. CONSOLE I/O Do it on the computer ( example01c_console_io ) Observations? Input is based on the Iterator pattern We'll see this other places as well. Not always very intuitive. There are lots more ways to get input.

4. FILE I/O - INPUT Lots of variants – I'll show you two. [With a Scanner] [With a BufferedReader (and a tweaked try)] Scanner s = Scanner(new File("blarg.txt")); // Now treat s just as before. try (BufferedReader br = new BufferedReader(new FileReader("blarg.txt")) { String line; line = br.readLine(); while (line != null) { // Process line // Get next line (if there is one) line = br.readLine(); } }

4. FILE I/O - OUTPUT I like the BufferedWriter approach try { File f= new File("new_blarg.txt"); if (!f.exists()) f.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("This is the first line!\n"); bw.write("This is the second!\n"); bw.close(); // IMPORTANT – usually } catch (IOException e) { System.out.println("Error creating new_blarg.txt!"); }

5. ARRAYS IN JAVA Do an example on computer ( example01d_arrays ) Observations? Java only supports Homogeneous arrays We'll see with objects + inheritance you can side-step this limit. You have to declare size at creation time. No automatic support for dynamic arrays Linked Lists (our first data structure) have this. Creating / copy expanding lists is a costly operation.

6. INTRODUCING CLASSES Terminology A class is a blueprint for a new type An object is an instance (variable) of that type A class contains a description of the type: instance variables : each instance of the class has their own copy of each of these. instance methods : each instance can call these methods (using dot operator) If any instance variables are used, they belong to the calling instance. class variables : The variable is associated with the class. There is only one copy. Identified with static class methods : Like an instance method, but can only access class variables. Can be called through an instance or through the class.

6. EXAMPLE Do example an example ( example01e_classes_intro ) Observations? Access modifiers (public, private, protected, or [None]) Limit who can access it. For now: Make classes public Make all callable methods public Make all instance / class variables protected (or private) Packages Static vs. Instances members Can be a bit confusing…