CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Object Oriented Programming in JAVA
Road Map Introduction to object oriented programming. Classes
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 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Introduction to Java.
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.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
CMSC 341 Java Review. 08/03/2007 CMSC 341 Java Review 2 Important Java Concepts and Terminology JRE is the Java Runtime Environment and it creates a virtual.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CMSC 202 Computer Science II for Majors Object-Oriented Programming.
DAT602 Database Application Development Lecture 5 JAVA Review.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Java Programming Language Lecture27- An Introduction.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
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.
CMSC 202 Computer Science II for Majors Fall 2010 Introduction Version 9/101.
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.
The need for Programming Languages
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Internet and Java Foundations, Programming and Practice
Data types and variables
Java package classes Java package classes.
Chapter 3 Introduction to Classes, Objects Methods and Strings
An Introduction to Java – Part I, language basics
Introduction to java Part I By Shenglan Zhang.
Review of Java Fundamentals
Presentation transcript:

CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at

08/03/2007 UMBC CMSC 341 Java 1 2 Important Java Concepts and Terminology JRE is the Java Runtime Environment and it creates a virtual machine within your computer known as the JVM (Java Virtual Machine). JRE is specific to your platform and is the environment in which Java byte code is run. JDK (formerly SDK) is the Java Development Kit. JDK = JRE + development tools J2SE is the Java 2 Platform Standard Edition, which you will be using in this course to build stand alone applications. To learn more about JDK, JRE, etc., visit

08/03/2007 UMBC CMSC 341 Java 1 3 Running and Compiling C/C++ C++ Code Linux binary Windows binary Linux executable Windows executable Project Library for Linux Project Library for Windows Linux C/C++ compiler Windows C/C++ compiler Linux C/C++ linker Windows C/C++ linker

08/03/2007 UMBC CMSC 341 Java 1 4 Running and Compiling Java Java Code Java Bytecode JRE for Linux JRE for Windows Java compiler Hello.java javac Hello.java Hello.class java Hello Java interpreter translates bytecode to machine code in JRE JRE contains class libraries which are loaded at runtime.

08/03/2007 UMBC CMSC 341 Java 1 5 Important Java Concepts Everything in Java must be inside a class. Every file may only contain one public class. The name of the file must be the name of the class appended to the java extension. Thus, Hello.java must contain one public class named Hello.

08/03/2007 UMBC CMSC 341 Java 1 6 Methods in Java The main method has a specific signature. Example: “Hello world!” Program in Java public class Hello { public static void main(String args[]) { System.out.println(“Hello world!”); } Notice no semi-colon at the end!

08/03/2007 UMBC CMSC 341 Java 1 7 Methods in Java (cont.) All methods must be defined inside a class. Format for defining a method: [modifiers] return_type method_name([param_type param]*) { statements; } For main, modifiers must be public static, return type must be void, and the parameter represents an array of type String, String []. This parameter represents the command line arguments when the program is executed. The number of command line arguments in the Hello program can be determined from args.length.

08/03/2007 UMBC CMSC 341 Java 1 8 Static Method Invocation Methods are invoked using the dot notation. Methods are either static or instance methods. Static methods do not have access to instance data. Static methods are typically invoked using the class name as follows: Math.random();

08/03/2007 UMBC CMSC 341 Java 1 9 Instance Method Invocation Create an instance of the class and have object invoke method. System.out is an object in the System class that is tied to standard output. We invoke the println() and print() methods on the object to write to standard output. System.out.println(“Hello world!”);

08/03/2007 UMBC CMSC 341 Java 1 10 Instance Method Invocation (cont.) The println and print methods have been overloaded so that they can convert all 8 of Java’s primitive types to a String. The + sign is overloaded to work as a concatenation operator in Java if either operand is a String. int x =15, y = 16; System.out.println(x + y + “/” + x + y);

08/03/2007 UMBC CMSC 341 Java 1 11 Instance Method Invocation (cont.) To invoke an instance method, you must first create an instance of the class (an object) and then use the object to invoke the method. StringBuffer phrase; phrase = new StringBuffer(“Java is fun”); phrase.replace(8,11, “cool”); System.out.println(phrase);

08/03/2007 UMBC CMSC 341 Java 1 12 Data Types There are two types of data types in Java – primitives and references. Primitives are data types that store data. References, like pointers and references in C++, store the address of an object, which is encapsulated data. int x = 5;Date d = new Date(); 5 x int d FEO3 Date ref Date obj FEO3

08/03/2007 UMBC CMSC 341 Java 1 13 Primitive Data Types Java has 8 primitive data types which always allocate the same amount of memory in JVM.  Integral byte – 8 bits short – 16 bits int – 32 bits – default for integer literals long – 64 bits int x = 5; short y = 03; long z = 0x L;

08/03/2007 UMBC CMSC 341 Java 1 14 Primitive Data Types (cont.) 8 primitive data types (cont.)  Floating point double - 64 bits - default for literal decimal value double d = ; double db = 123.5E+306; float - 32 bits - literal value must contain a F or f to avoid compiler errors float f = 32.5f;

08/03/2007 UMBC CMSC 341 Java 1 15 Primitive Data Types (cont.) 8 primitive data types (cont.)  Logical boolean - 1 bit boolean a = true; boolean b = 5 < 3;  Textual char- 16 bit - Unicode char c = ‘A’; char d = ‘\u04a5’ char e = ‘\t’; char f = 96;

08/03/2007 UMBC CMSC 341 Java 1 16 Reference Data Types Reference data types contain an address and function like pointers without the complex syntax. In the following code, the second line does not call a copy constructor, but rather you will have two references pointing to the same object. Date d = new Date(); Date e = d; Java tackled the problem of memory leaks in C++ by  not allowing the programmer to have direct access to the memory (i.e. no more pointer arithmetic),  checking array bounds at runtime, and  having a garbage collector in the JVM that periodically reallocates memory that is not referenced.

08/03/2007 UMBC CMSC 341 Java 1 17 Arrays Arrays in Java are objects. The first line of code creates a reference for an array object. The second line creates the array object. int [] arrayRef; arrayRef = new int[5]; arrayRef[2] = 5; int [ ] ref arrayRef DFO7 Array obj DFO

08/03/2007 UMBC CMSC 341 Java 1 18 Arrays (cont.) All primitive data in an array is initialized to its zero value.  boolean - false  char – ‘\u0’  byte, short, int, long, float, double – 0 All references are initialized to null. All arrays have a length property that gives you the number of elements in the array.  args.length is determined at runtime

08/03/2007 UMBC CMSC 341 Java 1 19 Arrays (cont.) An array of objects is an array of object references until the objects are initialized. Point pArray [] = new Point[5]; pArray[2] = new Point(); Point [ ] ref pArray CDO8 Array obj CDO8 null AB12 null AB12 Point obj

08/03/2007 UMBC CMSC 341 Java 1 20 Arrays (cont.) Arrays may also be initialized when they are declared using the following syntax. int intArray[]={1,2,3,4,5}; Point pArray[]={ new Point(1,2), new Point(3,4), new Point(5,6) };

08/03/2007 UMBC CMSC 341 Java 1 21 Arrays (cont.) Because arrays are objects and the name of an array is its reference, arrays in Java can grow or shrink upon reassignment. Also, the location of the square brackets can change. int [] aArr = new int[5]; int bArr [] = new int[3]; bArr = aArr; // now both are pointing // to same array and have // length of 5

08/03/2007 UMBC CMSC 341 Java 1 22 Arrays (cont.) The System class provides an arraycopy method that performs a shallow copy of one array to another. Use System.arraycopy to copy an array of primitive data, not for an array of references. System.arraycopy(srcArray, 4, destArray, 3, 2); Index in source Index in target number of elements

08/03/2007 UMBC CMSC 341 Java 1 23 Arrays (cont.) The declaration of array is carried through a comma separated list. The following declares two integer arrays. int [] a, b;

08/03/2007 UMBC CMSC 341 Java 1 24 Multidimensional Arrays The following declares a two-dimensional array, a reference. int [][] twodim; The following creates the array with twenty elements initialized to 0. twodim = new int [4][5]; The following does both at the same time. Notice the array is not rectangular. int [][] twodim2 = {{1,2,3}, {3,4}, {5,6,7,8}};

08/03/2007 UMBC CMSC 341 Java 1 25 Multidimensional Arrays (cont.) A pictorial rendition of twodim2. int [ ] [ ] ref twodim2 Array obj of array refs Array obj Each element is an int [] ref