{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CIT590 From Python to Java

CIT590 From Python to Java - CIT590 Fr om Python to Java...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
3/13/13 CIT590 From Python to Java www.cis.upenn.edu/~matuszek/cit590-2013/Pages/python-to-java.html 1/6 CIT 590: From Python to Java Spring 2013, David Matuszek Jump to: Introduction Python and Java are both object-oriented (O-O) languages, so most of the concepts are very similar. Here are some important differences you must keep in mind. Variables are typed and must be declared. That is, you must declare , for every variable you use, what type (integer, string, etc.) of value it may hold; then you must put only values of the correct type into that variable. Everything is in a class. Well, almost everything: package statements (which specify the enclosing directory) and import statements go in front of the class declaration. Normally, every class is put on a separate file. But all variable and method declarations must be inside classes . Statements must be inside methods. You can't just execute a statement by itself; it has to be within a method, and the method has to be within a class. Braces, { }, are used instead of indentation. Nevertheless, you should continue to indent as in Python. Style Standard indentation is four spaces. Do not use tabs (your editor should help you with this). Capitalization conventions are well established. Variables and method names begin with a lower case letter, and are camelCase . Names of classes and interfaces begin with a capital letter, and are CamelCase . Constants ( final variables) are written in ALL_CAPS_WITH_UNDERSCORES . Opening braces, { , go at the end of a line, not on a line by themselves. This differs from the C/C++ convention. Put spaces around all binary operators except dot. Program structure Until you understand all the details, here is a good program structure to start with. package myPackage ; // optional package declaration import java . util .*; // imports go here; util is needed for ArrayList, HashSet, HashMap public class MyClass { // file must be named myPackage/MyClass.java (note capitalization!) int myInstanceVar ; // declare instance variables here public static void main ( String [] args ) { new MyClass (). myStartingMethod (); } void myStartingMethod () { // declare local variables here // statements go here; can call other methods } //other methods go here } Strings and comments Java string literals are enclosed in double quotes ( " ), never in single quotes. Character literals are enclosed in single quotes ( ' ), never in double quotes. A one-character string is not the same as the single character. Escape sequences ( \n , \t , etc.) are single characters. There are no triple-quoted strings or raw strings in Java.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3/13/13 CIT590 From Python to Java www.cis.upenn.edu/~matuszek/cit590-2013/Pages/python-to-java.html 2/6 Strings can be concatenated with the + operator. Any object, of any type, may be concatenated to a String; it will be automatically
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}