CIT590 From Python to Java

As in python objects are passed by reference and so

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t;5 j+ { o it ; ; +) i (lcj = c){ f boki = h rtr i eun ; } } } rtr -; eun 1 } Aside from fancy features, the syntax of a method call is the same in Java as it is in Python. As in Python, objects are passed by reference (and so can be altered). The Java keyword t i corresponds to the Python word s l ; it is used to indicate an instance variable or instance method. Unlike hs ef Python, it is only needed when you need to distinguish an instance variable from a local variable (or parameter) with the same name. In Python, a constructor is a method with the name _ i i _ , and it must have s l as a first parameter. In Java, a constructor is like a _nt_ ef method, but the ret urnType and met hodName are replaced with the name of the class: acs Casaetp prmtr .. tp prmtr { ces lsNm(ye aaee, ., ye aaee) .. . } Example constructor (assuming that we are within a C rclass): a pbi CrSrn mk,itya){ ulc a(tig ae n er; hsmk ae; hsya er } A constructor may contain a r t r ; statement, but it may not explicitly return a value; the newly constructed object is always implicitly eun returned. Constructors are called with the keyword n w e: CrmCr=nwCr"ru" 20) a ya e a(Pis, 05; Scope Anything declared within a class (but not inside a method) is available everywhere throughout the class. Variables and methods do not have to be in any particular order. Usually, variables are put first, and usually the main method is either before or after all other methods. Parameters to a method are available throughout the method. Within a method, local variables are usually declared at the top, making them available throughout the method. However, the rule is: Anything declared within braces, { } is available from the point of declaration to the next closing brace. , matuszek/cit590- 2013/Pag es/python- to- java.html 5/6 3/13/13 CIT590 Fr om Python to Java As a special case, a variable may be declared within a f rloop (f r ( n i = 0 . ., and is available throughout the loop. o o it ; .) Access Variable declarations and method declarations may be preceded by one of the following access modifiers: p b i - Available from anywhere. ulc p o e t d- Available from anywhere within this folder/directory, and to all subclasses. rtce (default; no keyword, but called "package") - Available from anywhere within this folder/directory. p i a e- Available only within this class. Instance variables should almost always be private. rvt Unit testing Python ipr pafi mot lyar ipr uits mot ntet Java ipr ogjntBfr; mot r.ui.eoe ipr ogjntTs; mot ipr sai ogjntAsr.; mot ttc r.ui.set* casTsPafi(ntetTsCs) ls etlyaruits.etae: dfstpsl) e eU(ef: sl.ihr=Pafi(Uiest" efcpe lyar"nvriy) pbi casPafiCpeTs { ulc ls lyarihret PafiCpe cpe; lyarihr ihr @eoe Bfr pbi vi stp){ ulc od eU( cpe =nwPafiCpe(Uiest"; ihr e lyarihr"nvriy) } dftsGto(ef: e eteRwsl) sl.setqa(,cpe.eRw'') efasrEul0 ihrgto(a) ntetmi( @et Ts pbi vi tsGto( { ulc od eteRw) asrEul(,cpe.eRw'') setqas0 ihrgto(a); } matuszek/cit590- 2013/Pag es/python- to- java.html 6/6...
View Full Document

Ask a homework question - tutors are online