Java Basic Syntax
Transkrypt
Java Basic Syntax
Java to C++ comparizon First program Basic Java syntax Version control system Java Basic Syntax Java vs C++ Wojciech Frohmberg1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Outline 1 Java to C++ comparizon 2 First program 3 Basic Java syntax 4 Version control system Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Outline 1 Java to C++ comparizon 2 First program 3 Basic Java syntax 4 Version control system Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Outline 1 Java to C++ comparizon 2 First program 3 Basic Java syntax 4 Version control system Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Outline 1 Java to C++ comparizon 2 First program 3 Basic Java syntax 4 Version control system Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Tabular comparizon Paradigm Compilation/Running Standard libraries Multiple inheritance Run-time object information Type instances passing C++ Procedural/Object-oriented Compiled to machine code that can run on specific system Limited to the very basic functionality (e.g. STL supports regular expression only since C++11) Full support, including virtual inheritance Java Object-oriented is highly recommended Compiled to byte code that can be run on system with JVM Grows with each release One can achieve similar result using interfaces Only type names are available Full meta-information support, including dynamic code invocation by value/by reference/by pointer instances of primitive types by value, rest by reference (that can be perceived as pointers) explicit – one has to force system to free disposing memory is managed by, so memory called, garbage collector most of operators can be overloaded does not allow to overload operators Memory management Overloading operators Logic program hierar- namespaces chy Inline abstract class- not possible es/interfaces implementation Wojciech Frohmberg package = path to the source file, filename = inner type name allowed Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Hello world in Java Java source code 1 2 3 4 5 6 7 8 9 C++ source code package hw; import java.lang.System; public class Hello { public static void main(String args[]) { System.out.println("Hello world"); } } 1 2 3 4 5 6 7 8 #include <iostream> using std::cout; using std::endl; int main(int argc, char **argv) { cout << "Hello world" << endl; } Compiling 1 javac path/to/file/FileName.java Running 1 java path.to.file.FileName Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Creating jar file Compiled classes by javac are stored as files with extension class in a file system. While application grows there are plenty of such class files. To make the application transferable one have to use some other mechanisms that merge the number of files in a single executable application. To handle this problem Java applications make use of jar files. Command creating jar 1 jar cfe path/to/jarfile/jarfilename.jar path.to.mainclass.MainClass classfile[, ...] Running jar 1 java -jar jarfile.jar Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Tasks Complete the tasks, answer questions 1 Create hello world application in Java and create executable executable jar file. Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system A few guidelines Access modifier (without colon) before every class member declaration Inheritance of classes using extends keyword Implementing interface using implements keyword To declare constant values use final keyword To use some class from outside of the package use import keyword Virtual methods don’t have to be additionally specified – every method is virtual and can be overridden To declare abstract method use abstract keyword Use dot to access members of the object instance Use dot to access the package elements Use dot to access a static class members Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Example car/Car.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package cars; import vehicles.Vehicle; import vehicles.IStartEnginable; public class Car extends Vehicle implements IStartEnginable { public int cylinders = 10; public void startEngine() { System.out.print("B"); for (int i = 0; i < this.cylinders; i++) { System.out.print("R"); } System.out.println(); } public static void main(String[] args) { Car car = new Car(); car.startEngine(); } } Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Tasks Complete the tasks, answer questions 1 Use a class hierarchy from the first presentation (car.h/cpp, volkswagen.h/cpp, hyundai.h/cpp, main2.cpp) and rewrite its functionality to Java language. Remember to create a jar file. Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system What for? Version control system allows to manage the changes in the project. It can serve to monitor the project progress, restore code after getting into dead-end path, tracking the differences between files or even automated merging of changes of a single file by two programmers. Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Git To our todays laboratories purpose we will use git tool as it is available on polluks server. It though isn’t the most user-friendly one, but it will serve only to show how to make use of the powerful tool of version control system. Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system Basic use case Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system How to use(1) On server side (initialization) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ssh [email protected] #enters to polluks mkdir gittut cd gittut/ git init echo "Sample text" > sample.txt git status git checkout dumpbranch #creates repositorium directory #enters the repo directory #initializes repo #creates first repo file #prints the changes in repo says that #that sample.txt hasn’t been yet tracked #adds the sample.txt to tracked files #commits changes #shows available branches #creates dumpbranch to use master branch #without collision #switches to dumpbranch exit #exits the server git git git git add sample.txt commit -m "First commit" branch branch dumpbranch Wojciech Frohmberg Java Basic Syntax Java to C++ comparizon First program Basic Java syntax Version control system How to use(2) On client side (cloning repo and first push) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #downloads the repo to client git clone ssh://[email protected]/home/infXXXXXX/gittut cd gittut echo "Sample text2" >> sample.txt git status git commit -a -m "sample.txt modified" git diff sample.txt git push origin master git log git pull origin master #enters the repo directory #adds line to sample.txt #shows changes in repo #commits changes in sample.txt #shows changes on sample.txt #since previous commit #inserts changes into server #into master branch #shows the log of the repo #gets commits currently #available on remote server Wojciech Frohmberg Java Basic Syntax