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
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 Programmers First Night Overview First Night Basics Classes and Objects Second Night Enumerations Exceptions Input Output Templates vs Generics STL vs JavaSE API First 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 exercises C 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 within C Example factorial H ifndef FACTORIAL H define FACTORIAL H int factorial int n endif C 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 class Java Main Signature Main signature is a bit different No return value Only 1 argument to main a String array public static void main String args Java Factorial Example public 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 directly To run a out a out g example1 o factorial o example1 o g c example1 C example1 C factorial o g c factorial C factorial H factorial C Java 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 JVM Java 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 running To run java Factorial Factorial class javac Factorial java Factorial java Standard 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 streams Standard 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 summary Standard 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 stream Primitive 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 error Primitive 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 types applies to doubles and floats also So if you want to assign a double into a float you must cast it to be a float Additionally Java assumes that floating point literals are doubles by default If you want to create a literal you can tack on an f to the literal to declare it of a float type float f 1234 5678f Constants Constants in C are typically done in one of two ways A define define PI 3 14159 Or a const declaration const double PI 3 14159 Constants Java does not have a traditional preprocessor to do substitutions like C C Thus it uses an approach similar to the const declaration Constants in Java are typically declared to be public static final Declared inside a class outside all methods public static final double PI 3 14159 Functions C allows non member functions Java does not allow non member functions All functions in Java are actually methods Methods may be made static essentially making them equivalent to functions static int factorial int n int result 1 for int i n i 0 i result i return result Strings Java provides a String class much like the string class in the C STL Similarities Can create by simply using a double quoted string Concatenation is done using the operator Length obtained by calling length method Some notable differences No overloaded i operator use the charAt i method instead Strings in Java are immutable For more documentation on Java strings see http java sun com javase 6 docs api java lang String html Strings Since Strings are immutable a new String is created on all operations Need to be sure to assign result back into variable otherwise result of operation is lost String s foo s s bar baz s s substring 3 System out println s s s replace b B System out println s Reference Types The way Java handles reference non primitive types in Java is very different from that of C A reference is kind of like a pointer in


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