DOC PREVIEW
UMBC CMSC 341 - Java1

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at http pages cs wisc edu hasti cs368 JavaTutorial 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 http java sun com javase technologies index jsp 08 03 2007 UMBC CMSC 341 Java 1 2 Running and Compiling C C Project Library for Linux Linux binary C Code C C x Linu Wi 08 03 2007 Linux C C linker r pile m co nd ow com s C pile C r Linux executable Project Library for Windows Windows binary Windows executable Windows C C linker UMBC CMSC 341 Java 1 3 Running and Compiling Java Java Code Java Bytecode Java interpreter translates bytecode to machine code in JRE javac Hello java Java compiler Hello java ello H java JRE for Linux Hello class jav aH ello JRE for Windows JRE contains class libraries which are loaded at runtime 08 03 2007 UMBC CMSC 341 Java 1 4 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 5 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 6 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 7 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 8 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 9 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 10 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 11 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 x 5 Date d new Date d FEO3 Date FEO3 int Date ref 08 03 2007 obj UMBC CMSC 341 Java 1 12 Primitive Data Types Java has 8 primitive data types which always allocate the same amount of memory in JVM Integral 08 03 2007 byte 8 bits short 16 bits int 32 bits default for integer literals long 64 bits int x 5 short y 03 long z 0x23453252L UMBC CMSC 341 Java 1 13 Primitive Data Types cont 8 primitive data types cont Floating point double 64 bits default for literal decimal value double d 234 43 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 14 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 char char char 08 03 2007 c d e f A u04a5 t 96 UMBC CMSC 341 Java 1 15 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 16 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 arrayRef DFO7 int ref 08 03 2007 UMBC CMSC 341 Java 1 DFO7 0 0 Array 5 obj 0 0 17 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 18 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 pArray CDO8 Point ref 08 03 2007 CDO8 null null Array AB12 obj null null UMBC CMSC 341 Java 1 AB12 Point obj 19 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 20 Arrays cont Because arrays are objects and the name of an array is its …


View Full Document

UMBC CMSC 341 - Java1

Download Java1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?