Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 INTRO TO JAVA

2 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??

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

4 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)

5 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?

6 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.

7 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(); } }

8 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!"); }

9 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.

10 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.

11 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…


Download ppt "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."

Similar presentations


Ads by Google