DOC PREVIEW
UMBC CMSC 341 - Java for C++ Programmers

This preview shows page 1-2-3-4-5-6-44-45-46-47-48-49-50-89-90-91-92-93-94 out of 94 pages.

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

Unformatted text preview:

Java for C++ ProgrammersOverviewFirst Night AgendaC++ File StructureC++ Example – factorial.HC++ Example – factorial.CC++ Example – example1.CJava File StructureJava Main SignatureJava Factorial ExampleC++ CompilationJava CompilationJava Compilation & ExecutionStandard In/Out/ErrorStandard Out/ErrorStandard InPrimitive TypesSlide 18Slide 19Slide 20ConstantsSlide 22FunctionsStringsSlide 25Reference TypesSlide 27Slide 28ArraysArray AllocationEnhanced For LoopSlide 32Passing VariablesSlide 34Slide 35Command Line ArgumentsPackagesNamespaces in C++Packages in JavaPackaging ConventionsUsing a PackageExercisesBreakC++ Class DeclarationC++ Class DefinitionJava DifferencesJava Naming ConventionsJava Class Declaration & DefinitionC++ Method/Member AccessJava Access ModifiersC++ DestructorsJava “Destructor”Operator OverloadingOutputting an Object in C++Outputting an Object in JavaSlide 56Slide 57Comparing Objects in C++Comparing Objects in JavaSlide 60Slide 61Object Assignment in C++Object Assignment in JavaJava Copy ConstructorC++ InheritanceSlide 66Slide 67Slide 68Slide 69Slide 70Slide 71Java InheritanceSlide 73Slide 74Slide 75Slide 76InterfacesInterfaceJava Interface ExampleSlide 80Slide 81Slide 82Comparable InterfaceSlide 84Example Comparable ClassSlide 86Slide 87Date ComparableJavadoc CommentsJavadoc TagsA Javadoc ExampleJavadoc GenerationGenerated HTML DocumentationSlide 94Java for C++ ProgrammersFirst NightOverview•First Night–Basics–Classes and Objects•Second Night–Enumerations–Exceptions–Input/Output–Templates vs. Generics–STL vs. JavaSE APIFirst Night Agenda•Basics – file structure, compilation & execution differences, standard out/err/in, primitive types, constants, functions, strings, reference types, arrays, passing variables, command line arguments, packages –Discussion–Lab exercises•Break•Classes & Objects – declaration, instantiation, access modifiers, members, methods, common methods, inheritance, interfaces, javadoc–Discussion–Lab exercisesC++ File Structure•We typically have header files (*.h, *.H *.hpp) and a source file (*.c, *.C, *.cpp)•There is no coloration between the name of the file and the classes (if any) defined withinC++ Example – factorial.H#ifndef _FACTORIAL_H_#define _FACTORIAL_H_int factorial(int n);#endifC++ Example – factorial.C#include "factorial.H"int factorial(int n) { int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result;}C++ Example – example1.C#include <iostream>#include "factorial.H"using namespace std;int main(int argc, char** argv) { int n = 4; cout << "factorial(" << n << "): " << factorial(n) << endl; exit(0);}Java File Structure•No separate header file – all code is in a *.java file–No duplication of method signature in declaration (*.H) and definition (*.C) to keep in sync–No guarding of files to prevent against multiple includes•A file must contain a class of the same name–By convention files/classes start with a capital letter–Enforced consistency•No code lives outside of any classJava Main Signature•Main signature is a bit different–No return value–Only 1 argument to main – a String arraypublic static void main(String[] args)Java Factorial Examplepublic class Factorial { public static void main(String[] args) { int n = 4; System.out.print("factorial("); System.out.print(n); System.out.print("): "); System.out.println(factorial(n)); } static int factorial(int n) { int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result; }}C++ Compilation•Compilation in C++ is done by compiling each of the *.C files, to produce *.o files•The object files are then linked to create an executable which can be run directlyfactorial.H factorial.Cexample1.Cfactorial.oexample1.oa.outg++ -c factorial.Cg++ -c example1.Cg++ example1.o factorial.oTo run:a.outJava Compilation•In Java, *.java files are compiled into *.class files •These *.class files contain what is known as “bytecode”–Bytecode does not get run directly–Bytecode is not linked to produce an executable–Instead, this bytecode is interpreted and executed by another program known as the Java Virtual Machine (JVM)–Bytecode can be run on any platform having a JVMJava Compilation & Execution•*.java files are compiled using the javac command•The class files are then executed using the java command–Note no .class specified when runningFactorial.javaFactorial.classjavac Factorial.javaTo run:java FactorialStandard In/Out/Error•Like C++, Java automatically creates streams for stdout, stdin and stderr–cout is equivalent to System.out–cerr is equivalent to System.err–cin is equivalent to System.in•Unlike C++, no imports or includes need to be made to use these streamsStandard Out/Error•Output is easily done using one of the print methods off of these objects–System.out.print(arg) – prints the arg–If you want a newline (what you would get by using endl), use the println variant of this function – System.out.println(arg)•A detailed list of methods available on System.out or System.err can be found at:–http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#method_summaryStandard In•Like C++, Java automatically creates a stream for stdin–cin is equivalent to System.in•Unfortunately, reading a given type off of System.in is not as easy as using cin in C++•Typically another class is used in conjunction with System.in to more easily read values off of the streamPrimitive Types•Like C++, Java has a built-in boolean type–C++ uses the keyword bool, whereas Java used the keyword boolean–Both use the values true and false–Unlike C++, you cannot assign non-boolean types into booleans•boolean x = 1 results in a compiler errorPrimitive Types•Like C++, Java defines a character type –Just like C++, Java uses the char keyword to declare this type–Character literals are defined using single quotes such as 'a'Primitive Types•Integer types are pretty much the same, both provide the following types–A C++ short int is a Java short–A C++ int is a Java int–A C++ long int is a Java long•There are no unsigned variants in Java•Unlike C++, if you want to assign a higher precision number into a lower precision container you must explicitly down cast…int i = 1234567;short s = (short)i;Primitive Types•Like C++, Java has both float and double types•The same down casting rules that applied with integer


View Full Document

UMBC CMSC 341 - Java for C++ Programmers

Download Java for C++ Programmers
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 Java for C++ Programmers 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 Java for C++ Programmers 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?