Intro to ETEC2101 + Java.

Slides:



Advertisements
Similar presentations
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Advertisements

1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed.
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 CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network.
Introduction to Java Programming Language Junji Zhi University of Toronto 1.
CSE 252 Principles of Programming Languages Lab. Section
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 Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
DAT602 Database Application Development Lecture 5 JAVA Review.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
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.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
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?
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
2. Java language basics (1)
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
JAVA MULTIPLE CHOICE QUESTION.
Chapter No. : 1 Introduction to Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Website: SSD1 Unit 2 Intro to Java Presentation 2.3 Website:
Java package classes Java package classes.
User input We’ve seen how to use the standard output buffer
CompSci 230 Software Construction
Java Virtual Machine Complete subject details are available at:
Java Programming: From Problem Analysis to Program Design, 4e
Session 1: Introduction to Java
An Introduction to Java – Part I
Starting JavaProgramming
An Introduction to Java – Part I, language basics
Sridhar Narayan Java Basics Sridhar Narayan
Differences between Java and JavaScript
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Java’s Central Casting
Introduction to java Part I By Shenglan Zhang.
Review of Java Fundamentals
Presentation transcript:

Intro to ETEC2101 + Java

Pre-Requisites JDK (Java Development Kit) JRE (Java Runtime Engine) Several executables (javac.exe, jar.exe, etc.) Platform specific A ton of pre-build classes (4000+) Many of these are data structures! JRE (Java Runtime Engine) A virtual machine that runs java programs. The VM is platform-specific But…Java programs (byte-code) are completely cross-platform! Can be installed separately, but comes with JDK An IDE (Integrated Development Environment)? [recommended] NetBeans + JDK bundle Others: IntelliJ, Eclipse Or you could go command-line… Solid programming background (+ OOP experience)

Language Comparisons Python C Java ca. 1991 ca. 1978 ca. 1990 widely used TIOBE #4 #2 #1 (May 2017) interpreted compiles to machine code compiles to byte-code slow as fast as possible ~95% as fast as C very descriptive run-time errors cryptic run-time errors depends (in IDE vs. running through JVM) tons of packages very lean

Java Hello World Create a NetBeans Project Source: Main Class: hello_world.HelloWorld hello_world is the package HelloWorld is the class. Source: package hello_world; public class HelloWorld { public static void main(String[] args) System.out.println("Hello, World!"); }

Hello World Observations Java’s pretty wordy Everything must go in a class Can’t have “stuff” outside a class (global variables) Main must have the signature… public static void main(String[] args) static means it can be called through the class (rather than an instance) public means that everyone can see it. args is an array of strings (command-line arguments) System is a class out is an instance within System (the console) println is a method of the out object.

Build process Step 1: Compilation Step 2: Linking / Running Calls javac on each .java file in project Equivalent to: javac –d build\classes src\hello_world\HelloWorld.java Creates a .class file (bytecode) in folders by package Step 2: Linking / Running Both collect multiple .class files Option1: Link (create a .jar file [kind of like an exe]) Later? Option2: Run Running invokes a main method in one class. Equivalent to java –classpath build\classes hello_world.HelloWorld java.exe is the virtual machine

Better hello world (keyboard) Input! There is a System.in, but it just gives raw data. Scanners are a high-level alternative package hello_world; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) System.out.println("Hello, World!"); System.out.print("Enter your name: "); Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println("Nice to meet you " + name); }

observations Import statement: grants access to part of another package. Alternate syntax: import java.util.*; System.out.print function. Variables: In Java, you must declare variables before using them. Python uses implicit declarations Java uses explicit declarations Some (primitive) variables are just containers for values Other (instance) variables must be allocated as well. Scanner is one instance of this. sc is just a reference (pointer in C/C++) to a memory location

Primitive Variables Primitve Type Size (in bytes) Value Range boolean 1/8 (1 bit)? true, false byte 1 -128…+127 char 0…255 ‘a’, ‘Z’, ‘1’ (not strings!) short 2 -32767…32768 int 4 -231…+231-1 long 8 -263…+263-1 float ±3.403e38f…±1.402e-45f double ±1.8e308d…±4.49e-324d integer types floating-point types

Conversion Java is very persnickety about types. Sometimes we must tell it we’re sure we want a value. e.g. float f = 0.0; // An error We could fix it (here) by changing 0.0 (a double constant) to 0.0f; Or…use a cast operator. float f = (float)0.0; There are times where you must cast to the desired type.

Memory Diagrams (very) loosely our OS process in memory Three areas: short x = 15; double d = 3.14d; boolean b = true; String s = “abcdefg”; int y = 99; (very) loosely our OS process in memory Three areas: Stack memory Heap memory Program byte-code (not usually shown) Stack and Heap share the same memory StackOverflow error… addr var_name contents Notes x 15 2 d 3.14d 10 b 1 bools are converted to ints. 14 s “abcdefg” (each char is one byte + null-char) 22 y 99 … (nothing in heap) 40000 37 (java byte-code ops, values, etc.) 40004 Stack Heap Program Code

Java References A reference is a pointer to heap memory. The variable doesn’t hold data; it holds an address The memory must be allocated using new before using. Otherwise you’ll get a NullReference error! addr var_name contents Notes x 15 2 sc 40000 On 64-bit JDK, references are 8-bytes (a long) 10 y 99 … <Scanner obj> No name! We must use sc to access it! short x = 15; Scanner sc = new Scanner(System.in); short y = 99;

Decisions / Repetitions If statements should look familiar While Loops too For loops are new… if (x < 15) { // do something } else if (x > 99) // do something else else // all other cases. int i = 10; while (i < 20) { // do something // increment counter i++; } for (int i = 10; i < 20; i++) { // do something. }

Arrays in Java Like lists in other languages Example Fixed-size (can’t change without re-allocating) Homogeneous data only (there is a workaround…) Example (do memory diagram on board) int[] ilist = new int[10]; for (int i = 0; i < 10; i++) ilist[i] = i * 5 + 50; //ilist[10] = 1000; // Try it...I dare ya‘ System.out.println(ilist.length); // 10 ilist = null; // Triggers garbage collection

(static) functions in Java A static function is like a “normal” function in Python / C Must be in a class. If called within that class, acts like a global function (Do memory diagram on board, including function call) public static void print_ints(int[] L) { for (int i = 0; i < L.length; i++) System.out.println(i + " " + L[i]); } public static void main(String[] args) int[] ilist = new int[10]; for (int i = 0; i < ilist.length; i++) ilist[i] = i * 5 + 50; print_ints(ilist);