Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to ETEC2101 + Java.

Similar presentations


Presentation on theme: "Intro to ETEC2101 + Java."— Presentation transcript:

1 Intro to ETEC Java

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

3 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

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

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

6 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

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

8 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

9 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

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

11 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

12 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;

13 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. }

14 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 * ; //ilist[10] = 1000; // Try it...I dare ya‘ System.out.println(ilist.length); // 10 ilist = null; // Triggers garbage collection

15 (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 * ; print_ints(ilist);


Download ppt "Intro to ETEC2101 + Java."

Similar presentations


Ads by Google