DOC PREVIEW
UMBC CMSC 341 - Java1

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

Save
View full document
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
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
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
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
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 341Important Java Concepts and TerminologyRunning and Compiling C/C++Running and Compiling JavaImportant Java ConceptsMethods in JavaMethods in Java (cont.)Static Method InvocationInstance Method InvocationInstance Method Invocation (cont.)Slide 11Data TypesPrimitive Data TypesPrimitive Data Types (cont.)Slide 15Reference Data TypesArraysArrays (cont.)Slide 19Slide 20Slide 21Slide 22Slide 23Multidimensional ArraysMultidimensional Arrays (cont.)CMSC 341Introduction to Java Based on tutorial by Rebecca Hasti athttp://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/08/03/2007UMBC CMSC 341 Java 12Important 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., visithttp://java.sun.com/javase/technologies/index.jsp08/03/2007UMBC CMSC 341 Java 13Running and Compiling C/C++ C++ CodeLinux binaryWindowsbinaryLinux executableWindowsexecutableProject Library for LinuxProject Library for WindowsLinux C/C++ compilerWindows C/C++ compilerLinux C/C++ linkerWindows C/C++ linker08/03/2007UMBC CMSC 341 Java 14 Running and Compiling JavaJavaCodeJavaBytecodeJRE for LinuxJRE for WindowsJava compilerHello.javajavac Hello.javaHello.classjava Hellojava HelloJava interpreter translates bytecode to machine code in JREJRE contains class libraries which are loaded at runtime.08/03/2007UMBC CMSC 341 Java 15Important 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/2007UMBC CMSC 341 Java 16Methods in JavaThe main method has a specific signature.Example: “Hello world!” Program in Javapublic class Hello{public static void main(String args[]){System.out.println(“Hello world!”);}}Notice no semi-colon at the end!08/03/2007UMBC CMSC 341 Java 17Methods 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/2007UMBC CMSC 341 Java 18Static 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/2007UMBC CMSC 341 Java 19Instance 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/2007UMBC CMSC 341 Java 110Instance 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/2007UMBC CMSC 341 Java 111Instance 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/2007UMBC CMSC 341 Java 112Data 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();5xintdFEO3Date refDate objFEO308/03/2007UMBC CMSC 341 Java 113Primitive 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 bitsint x = 5;short y = 03;long z = 0x23453252L;08/03/2007UMBC CMSC 341 Java 114Primitive Data Types (cont.)8 primitive data types (cont.)Floating pointdouble - 64 bits - default for literal decimal valuedouble d = 234.43;double db = 123.5E+306;float - 32 bits - literal value must contain a F or f to avoid compiler errorsfloat f = 32.5f;08/03/2007UMBC CMSC 341 Java 115Primitive Data Types (cont.)8 primitive data types (cont.)Logicalboolean - 1 bitboolean a = true;boolean b = 5 < 3;Textualchar- 16 bit - Unicodechar c = ‘A’;char d = ‘\u04a5’char e = ‘\t’;char f = 96;08/03/2007UMBC CMSC 341 Java 116Reference 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/2007UMBC CMSC 341 Java 117Arrays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 [ ] refarrayRefDFO7Array objDFO75000008/03/2007UMBC CMSC 341 Java 118Arrays (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


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