DOC PREVIEW
UMD CMSC 433 - Basic Java

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 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 23 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 23 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 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 433, Adam Porter, U. Maryland 1CMSC433, Fall 2001 Programming LanguageTechnology and Paradigms Basic JavaAdam PorterSeptember 18, 2001CMCS 433, Fall 2001 - Adam Porter 2Administrivia• C++ project due Thursday, 6PM• Java project posted soon– a simple Web server– due Wednesday, Oct. 3, 6PM• Java readings from Thinking in Java– we’ll do parts of many chapters, but definitelyread Chapter 1 for an overview– I’ll add some suggestions on Web page, but useit as a refererenceCMCS 433, Fall 2001 - Adam Porter 3What Makes Java Different• Java fully specified– e.g., constants, size of types, array boundschecking, no dangling pointers– language specification intended to completelyspecify the behavior of all programs• not just correct ones• all runtime errors must be caught– KISS principle applied• many useful, but not essential, features from C++not included (operator overloading, templates,multiple inheritance, standalone functions, …)CMCS 433, Fall 2001 - Adam Porter 4Java semantics• Machine architecture insensitive– size of machine word– floating point format (must use IEEE 754)– big/little-endian• Compiled to machine independent bytecode• Many C++ programs break when moved tomachine with different word size orendiannessCMCS 433, Fall 2001 - Adam Porter 5Java security• Can strictly limit access to code–untrusted code can’t access files and arelimited in network connectivity by default• Compiled code (byte codes) can be verifiedfor correctness (by another program)• But security bugs are still possible–e.g., denial of service, insecure mode code– but all C++ programs run in an insecure modeCMCS 433, Fall 2001 - Adam Porter 6Java features• Strong type system• Multi-threading and synchronization• Garbage collection• Exceptions• class Class– classes are objects too• class Object– the class all classes are derived fromCMSC 433, Adam Porter, U. Maryland 2CMCS 433, Fall 2001 - Adam Porter 7Java libraries• Utilities– collection classes, Zip files, internationalization• GUIs, graphics and media• Networking– sockets, URLs, RMI, CORBA• Threads• Databases• Cryptography/securityCMCS 433, Fall 2001 - Adam Porter 8Java Basics• Mostly C++ syntax• Statements– empty, expressions, blocks {}– control flow (if, switch, while, for, …)– throw and try/catch/finally– synchronized– no gotoCMCS 433, Fall 2001 - Adam Porter 9Expressions• Mostly C/C++ syntax• Standard math operators: +, _, *, /, %• Bit operations: &, |, ^, ~, <<, >>, >>>• Update ops: =, +=, -=, *=, /=, …• Relational ops: <, <=, ==, >=, >, !=• Boolean ops: &&, ||, !• Conditional expression: b ? e1 : e2CMCS 433, Fall 2001 - Adam Porter 10Class operations• Select method/variable/class/subpackage: .• Class operators: new, instanceof, (Class)• No pointer operations: *, &, ->CMCS 433, Fall 2001 - Adam Porter 11Hello World examplepublic class HelloWorld { public static void main(String [] args) { if (args.length == 1) System.out.println(“Hello “ + args[0]); else System.out.println(“Hello world”); }}CMCS 433, Fall 2001 - Adam Porter 12Naming conventions• Classes/Interfaces start with a capital letter• packages/methods/variables start with alowercase letter• for names with multiple words,CapitalizeFirstLetterOfEachWord• Don’t use underscores• CONSTANTS all in uppercaseCMSC 433, Adam Porter, U. Maryland 3CMCS 433, Fall 2001 - Adam Porter 13Values• Object reference: null, or ref to object• boolean – a built-in type• char – Unicode; 16 bits• byte/short/int/long – 8/16/32/64 bits, signed• float/double – 32/64 bits IEEE 754CMCS 433, Fall 2001 - Adam Porter 14Objects and references• All objects allocated on the heap• No object can contain another object• All class variables/fields are references toan object• Reference is like a C++ pointer, except– can only point to start of heap-allocated object– no pointer arithmetic allowed– use . instead of -> to access fields/methodsCMCS 433, Fall 2001 - Adam Porter 15String exampleCMCS 433, Fall 2001 - Adam Porter 16Object operations• = assignment– for object references: copies reference, not object• == equality test– for object references: true if references to same object• foo.equals(bar)– intended to compare contents of objects– by default same as ==, but can/should be overridden• foo.toString()– returns String representation of the object, can/shouldbe overriddenCMCS 433, Fall 2001 - Adam Porter 17More Object operations•foo.clone()– returns shallow copy of foo (not supported onall Objects)•foo.getClass()– returns class of foo (result is of type Class)•foo instanceof Bar– true if object referenced by foo is a subtype ofclass BarCMCS 433, Fall 2001 - Adam Porter 18More Object operations•(Bar) foo– run-time exception if object reference by foo isnot a subclass of Bar– compile-time error if Bar is not a subtype offoo (i.e. it always throws an exception)– doesn’t transform anything, just allows treatingthe result as if it were of type BarCMSC 433, Adam Porter, U. Maryland 4CMCS 433, Fall 2001 - Adam Porter 21Arrays• a special kind of object (with lots of syntax)• can declare arrays of any type• have one instance variable: length• also have contents indexed with a subscriptfrom 0 … length-1• can be initialized using {val0,val1, …, valn}notation– inefficient for large arraysCMCS 433, Fall 2001 - Adam Porter 22Array declarations• Little surprising for C/C++ programmer•int[] A and int A[] have identical semantics– declares A to be a variable containing a reference to anarray of ints•int[] A[], B;–A is a ref to an array of refs to arrays of ints–B is a ref to an array of ints• None of these allocate an array•A = new int [10]; allocates an array of 10 ints,and makes A be a reference to itCMCS 433, Fall 2001 - Adam Porter 23Array exampleint[] array1 = {1, 3, 5};int[][] a = new int[10][3];// a.length == 10// a[7].length == 3a[5][2] = 42;a[9] = array1;// a[9][2] == 5// use of array initializersint[][] twoD = {{1, 2, 3}, {4, 5}, {6}};Object [] args = {“one”, “two”, a};main(new String [] {“a”, “b”, “c”});CMCS 433, Fall 2001 - Adam Porter 24Administrivia• C++ project– private drivers posted soon– other student’s code will be sent to you for commentary• 2 projects, about a paragraph for each• Java project posted soon– web server is


View Full Document

UMD CMSC 433 - Basic Java

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Basic Java
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 Basic Java 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 Basic Java 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?