CS 240: Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
Advertisements

Lab#1 (14/3/1431h) Introduction To java programming cs425
Introduction to Java The objectives of this chapter are: To describe the key aspects of Java To describe the Java software development kit (SDK) To explain.
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.
Introduction to Java Kiyeol Ryu Java Programming Language.
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 ISYS 350. A Brief History Sun Microsystems released this language in 1996 – Versions: 1.0 – 1.6 Java Development Kit, JDK – Standard.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
1.  At the end of this slide, student can:  Explore tools, features, properties and interface of the Textpad.  Creating a new project.  Open and run.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
POS 406 Java Technology And Beginning Java Code
Java Programming, Second Edition Chapter One Creating Your First Java Program.
COP 3330 Notes 1/12. Today's topics Downloading Java and Eclipse Hello World Basic control structures Basic I/O Strings.
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.
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 BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
4-Nov-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic 1: The Java Environment Maj Joel.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
introductory lecture on java programming
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++)
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
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.
Object Oriented Programming in
Information and Computer Sciences University of Hawaii, Manoa
Lecture 1b- Introduction
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Java Basics Introduction to Java.
Chapter 1 Introduction to Computers, Programs, and Java
Chapter No. : 1 Introduction to Java.
Text by: Lambert and Osborne
Java programming lecture one
Programming Language Concepts (CIS 635)
Getting Started ARCS Lab..
Advanced Programming Fall 2017.
An Introduction to Java – Part I, language basics
Units with – James tedder
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.
Advanced Programming CS206
Applying OO Concepts Using Java
Introduction to Java Brief history of Java Sample Java Program
(Computer fundamental Lab)
CS 240 – Advanced Programming Concepts
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
IS 135 Business Programming
Presentation transcript:

CS 240: Advanced Programming Concepts Java Fundamentals CS 240: Advanced Programming Concepts

Topics and Topic Order We won’t cover everything you will need to know in class Read the assigned chapters We will focus on things that are significantly different from C++ Topic order will be driven by the programming projects

8/23/2019 Where Java Came From Early 1991 - Green project started at Sun Microsystems Tried to write a better C++ compiler Late 1992 - Completed Oak 1993 - Mosaic introduced Early 1994 - Green team (FirstPerson) disbanded Oak renamed Java and HotJava Browser Created May 23, 1995 - Netscape announcement 2010 – Oracle Acquired Sun Microsystems, and Java

- The Java Language: An Overview (Sun Whitepaper) 8/23/2019 What is Java? “A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language”. - The Java Language: An Overview (Sun Whitepaper)

Java Overview Similar syntax but in many cases different semantics from C++ Differences between Java and C++ Built-in garbage collection References instead of pointers Data types are always the same size in Java Specific boolean datatype and language constructs made to use it if(x = 1) is a compile error in Java Classes dynamically linked at runtime (no separate link step) Java is a hybrid, compiled / interpreted language Several other differences

Getting and Installing Java Download the latest version of the JDK from Oracle’s website https://www.oracle.com/technetwork/java/javase/downloads/index.html Find platform specific installation instructions on the download page

Java IDEs Intellij Idea (community edition is free) Android Studio (free) Eclipse (free) Others

Compiled Code PC Compiler Compiler Source Code Executable Code 8/23/2019 Compiled Code Compiler Source Code Compiler Executable Code Executable Code UNIX Android Mac PC PC

Interpreted Code Interpreter Interpreter Source Code PC Android UNIX 8/23/2019 Interpreted Code Source Code Interpreter Interpreter UNIX Mac PC Android

Java Code Compiler JVM JVM Source Code PC Java Byte Code Android UNIX 8/23/2019 Java Code Compiler Java Byte Code Source Code JVM JVM UNIX Mac PC Android

Compiled vs. Interpreted Code Compiled = fast but not portable Runs on bare hardware-–instructions are not interpreted at runtime Recompile (and often re-code and then recompile) to run on different hardware Interpreted = slow but portable Runs on a VM or interpreted that interprets and translates instructions at runtime Runs on any platform with an interpreter without recompiling Java: seeks to have best of both (fast and portable) Compiled to bytecode which runs on a virtual machine spec Translation to actual machine language is minimal and fast Runs on any platform with a JVM (which is most platforms)

JIT Compilation and The Hotspot Virtual Machine JIT = Just in Time Compilation Hotspot VM = Dynamically recompilation at runtime Provides new opportunities for performance improvement Causes programs to start and run faster than JIT compiled code Can optimize to the specific hardware architecture Uses a generational garbage collector

Java Files MyClass.java = source file With a few exceptions, there is one Java class per .java file The file name must match the class name MyClass.class = executable file (executable by the JVM) The main method public static void main(String [] args) public static void main(String…args)

Creating Java Classes public class SimpleJavaClass { public static void main(String [] args) { System.out.println(“Hello BYU!”); } _______________ Code Examples: SimpleJavaClass.java Point.java Rectangle.java PointAndRectangleUser.java

Compiling and Running Java Programs Compile javac SimpleJavaClass.java Produces SimpleJavaClass.class For now, you must be in the directory that contains the .java file Run java SimpleJavaClass No .class at the end For now, you must be in the directory that contains the .class file

Compiling and Running in Intellij

Compiling and Running in Intellij

Javadoc Documentation for the Java class library Generated from code and Javadoc comments in the code Download and install or access from Sun’s website with a Google search Google search: Java 12 api Can generate for your own classes using the Javadoc tool that comes with the JDK

Primitive Datatypes byte short int long float double char boolean Code Example PrimitiveDataTypes.java

Converting a String to an int The Integer Wrapper Class int Integer.parseInt(String value) Several other methods for parsing between Strings, ints and Integers Similar Methods in: Byte Short Double Long Float Boolean

Strings String Declaration and Assignment String concatenation String s = “Hello”; String s = new String(“Hello”); String concatenation String s1 = “Hello”; String s2 = “BYU”; String s3 = s1 + ” “ + s2; Strings are immutable (concatenation always creates a new String) String formatting String s3 = String.format(“%s %s”, s1, s2); Code Example: StringExamples1.java

Important String Methods int length() char charAt() String trim() boolean startsWith(String) int indexOf(int) int indexOf(String) int substring(int) int substring(int, int) Many others. See Javadoc. Remember: Strings are immutable, none of these methods change the String Code Example StringExamples2.java

Special Characters \n (newline) \t (tab) \” (double quote) \’ (single quote) \\ (backslash) \b (backspace) \uXXXX (insert the Unicode character represented by XXXX) \r (carriage return—return to the beginning of the current line—obsolete) \f (form feed—advance to the next line— obsolete) Code Example SpecialCharacterExamples.java

Arrays See ArrayExample.java

Command-Line Arguments public class CommandLineArgsExample { public static void main(String [] args) { for(int i = 0; i < args.length; i++) { String message = String.format(”Argument %d is %s", i, args[i]); System.out.println(message); } } }

Specifying Command Line Arguments From the command line java CommandLineArgsExample abc 123 “Hello BYU” From Intellij Create a run configuration and specify arguments in the ”Program Arguments” field

Packages Packages provide a way to organize classes into logical groups Packages can have sub-packages (separated by . (dots)) Specify the package for a class with a ’package’ statement at the top of the .java file Files (.java and .class) must be in a directory structure that matches the path structure The package name becomes part of the class name. Example: Java has two date classes: java.util.Date java.sql.Date You must refer to classes by their fully-qualified package name unless you use imports Code Examples: Student.java Student2.java

Import Import statements provide a shorthand for the fully-qualified package name (they allow you to just enter the class part of the name) They do not increase the size of your compiled .class files (unlike C/C++ includes) If used, they appear at the top of the file—before class declarations but after the package declaration (if a package declaration exists) The wildcard * imports all classes in the package, but not subpackages Example: import java.util.*; You do not need an import in the following cases: You choose to use fully-qualified package names (not normally recommended) The class you are using is in the java.lang package (Object, String, and several others) The class you would import is in the same package as the class that needs to use it

CLASSPATH An environment variable that contains a list of directories that contain .class files, package base directories, or other resources your application needs to access Colon separated on Mac OS and Linux Semicolon separated on Windows . (current directory) is implicitly on the CLASSPATH if you don’t set a CLASSPATH Can use -classpath command line param IDEs like Intellij and Eclipse and Android Studio manage this for you

Input / Output (IO) Use a File object to represent a file in your program Use Readers and Writers to read and write text files Use InputStreams and OutputStreams to read and write binary files Readers and Writers, InputStreams and OutputStreams can be chained together to add functionality to your reads and writes Most file IO operations can result in IOExceptions being thrown For now, just handle them by declaring that your method throws them: public void myMethod() throws IOException { Will require you to import java.io.IOException (or use the fully-qualified name) Close your readers and writers when you are through (try-with-resources statements will do that for you) try(…) { Code Example CopyFileExample.java

Another Way to Read a File: java.util.Scanner public void processFile(File file) throws IOException { Scanner scanner = new Scanner(file); scanner.useDelimiter("((#[^\\n]*\\n)|(\\s+))+"); while(scanner.hasNext()) { String str = scanner.next(); // Do something with the String }

Another Way to Read A File: Files.readAllLines(Path) public List<String> readFile(File file) throws IOException { Path path = Paths.get(file.getPath()); List<String> fileContents = Files.readAllLines(path); return fileContents; }