Transcription

1 Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively. A Python statement may be entered at the interpreter prompt (>>>), and will be executed immediately. (Implementations make some use of automatic compilation into bytecodes (.pyc files). Python is dynamically typed: A variable is introduced by assigning a value to it. Example: somevariable = 42 A variable that has been assigned a value of a given type may later be assigned a value of a different type. Example: somevariable = 42 somevariable = 'Hello, world' Python supports the following built-in data types: Plain integers (normally 32-bit integers in the range through ). Long integers (size limited only by memory size of the machine running on) Booleans (False and True). Real numbers. Complex numbers. In addition, Python supports a number of types that represent a collection of values - including strings, lists, and dictionaries. Java Java supports only object-oriented programming. Programs written in Java must be explicitly compiled into bytecodes (.class files), though an IDE may do this automatically in a way that is transparent to the user. Java does not support direct execution of statements - though there are tools like Dr. Java that support this. Java is statically typed: A variable must be explicitly declared to be of some type before assigning a value to it, though declaration and assignment may be done at the same time. int somevariable; int somevariable = 42; A variable that has been declared to be of a particular type may not be assigned a value of a different type. Java has two kinds of data types: primitive types and reference types. Java supports the following primitive data types: byte - 8-bit integers short - 16-bit integers int - 32-bit integers long - 64-bit integers (Java also supports a class java.math.biginteger to represent integers whose size is limited only by memory) float - 32-bit real numbers. double - 32-bit real numbers. boolean - (false and true). char - a single character. In addition, Java supports arrays of any type as the reference types, and the API includes the class String and a large number of classes used for collections of values. 1

2 Python is line-oriented: statements end at the end of a line unless the line break is explicitly escaped with \. There is no way to put more than one statement on a single line. this is a statement this is another statement this is a long statement that extends over more \ than one line Python comments begin with # and extend to the end of the line. Example: # This is a comment A new statement starts here Statements in Java always end with a semicolon (;). It is possible for a statement to run over more than one line, or to have multiple statements on a single line. this is a statement; this is another statement; this is a long statement that extends over more than one line; a statement; another; another; Java has two kinds of comments. A comment beginning with // extend to the end of the line (like Python comments). Comments can also begin with /* and end with */. These can extend over multiple lines or be embedded within a single line. // This is a comment A new statement starts here /* This is also a comment */ /* And this is also a comment, which is long enough to require several lines to say it. */ Statement starts /* comment */ then continues Python strings can be enclosed in either single or double quotes (' or ""). A character is represented by a string of length 1. 'This is a string' "This is also a string" # Equivalent 'c' # A string "c" # An equivalent string Python uses the following operators for constructing compound boolean expressions: and, or and not. Example: not(x > 0 and y > 0) or z > 0 In Python, the comparison operators (>, <, >=, <=, == and!=) can be applied to numbers, strings, and other types of objects), and compare values in some appropriate way (e.g. numeric order, lexical order) where possible. Java strings must be enclosed in double quotes (""). A character is a different type of object and is enclosed in single quotes ('). "This is a String" 'c' // A character, but not a String Java uses the following operators for constructing compound boolean expressions: &&,,! and ^ (meaning exclusive or) Example:! (x > 0 && y > 0) z > 0 ^ w > 0 In Java, most of the comparison operators ( >, <, >=, and <=) can be applied only to primitive types. Two (== and!=) can be applied to any object, but when applied to reference types they test for same (different) object rather than same (different) value. 2

3 There is no universally-accepted Python convention for naming classes, variables, functions etc. By convention, most names in Java use mixed case. Class names begin with an uppercase letter; variable and function names begin with a lowercase letter. Class constants are named using all uppercase letters with underscores. AClassName avariablename afunctionname() A_CLASS_CONSTANT Python definite looping statements have the form for variable in expression: Example: for p in pixels: Python uses the built-in function range() with for to loop over a range of integers. for i in range(1, 10) (i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9) for i in range(1, 10, 2) (i takes on values 1, 3, 5, 7, 9) Python indefinite looping statements have the form while condition: Example: while x > 0: Java has two kinds of definite looping statements. One has the form for (variable in collection) Example: for (p in pixels) Java uses a different form of the for to loop over a range of integers. for (int i = 1; i < 10; i ++) (i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9) for (int i = 1; i < 10; i += 2) (i takes on values 1, 3, 5, 7, 9) Java has two forms of indefinite looping statement. One has the form while (condition) Example: while (x > 0) The other has the form do... while(condition). Example: do while(x > 0); 3

4 Python conditional statements have the form if condition: and an optional else part has the form else:. The form elif condition: is allowed as an alternative to an else: immediately followed by an if. else: different elif x > 0: different else: yet another thing Java conditional statements have the form if (condition) and an optional else part has the form else (no colon) There is no elif form - else if is used directly. else different; else if (x > 0) different; else yet another thing; Java also has another form of conditional statement (switch) which has no analog in Python. Example: switch(i) case 0: 1; 2;... break; case 1: else; break;... default: yet else; (The individual cases can include any number of statements [ shown for the first case only, but possible for any ], normally ending with the special statement break; ) 4

5 The scope of a Python conditional or looping statement is denoted by indentation. (If multiple lines are to be included, care must be used to be sure every line is indented identically). do do another thing regardless of the value of x do do else do yet a third thing do another thing regardless of the value of x The scope of a Java conditional or looping statement is normally just the next statement. Indentation is ignored by the compiler (though stylistically it is still highly desirable for the benefit of a human reader). If multiple lines are to be included, the scope must be delimited by curly braces (, ). (Optionally, these can be used even if the scope is a single line.) do do // Bad style-don't do this! do do else; do yet a third thing; do 5

6 A Python function definition has the form def function-name(formal-parameter-list): body Example: def disc(a, b, c): return b * b - 4 * a * c If there are no parameters, an empty list (()) is used. The body is delimited by indentation, and can be any number of lines. A function does not have to return a value; if it does not, no return statement is required, though return without a value (or with None) can be used to end the function execution early. A Java function definition always occurs in the context of some class. It has the form type function-name(formal-parameter-list) body Example: class Solver... double disc(double a, double b, double c) return b * b - 4 * a * c;... The return type of a Java function must always be specified. If it does not return a value, void must be used. If there are no parameters, an empty list (()) is used. A type must be specified for each of the parameters. The body is always delimited by curly braces (, ) even if the body is a single line. Indentation is ignored by the compiler (though stylistically it is still highly desirable for the benefit of a human reader). If the function does not return a value (its type is void), no return statement is required, though return without a value can be used to end the function execution early. A Python function is called by specifying its name followed by a list of actual parameters (that must be the same length as the list of formal parameters) Example: discr(1, 2, 1) A Java function is called by specifying the object or class to which it is to be applied followed by a dot, then its name followed by a list of actual parameters (that must be the same length as the list of formal parameters, with each actual parameter compatible with the declared type of the corresponding formal parameter) Example: solver.discr(1, 2, 1) 6

1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

JAVA TYPES BASIC DATA TYPES GENERAL Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type

Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

JAVA PRIMITIVE DATA TYPE Description Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in programming. For performance

Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.

Introduction to Programming and Algorithms Module 2 CS 146 Sam Houston State University Dr. Tim McGuire Introduction To Computers And Java Chapter Objectives To understand: the meaning and placement of

java.util.scanner java.util.scanner is a class in the Java API used to create a Scanner object, an extremely versatile object that you can use to input alphanumeric characters from several input sources

WEEK ONE Introduction to Python Python is such a simple language to learn that we can throw away the manual and start with an example. Traditionally, the first program to write in any programming language

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 The Java Type System By now, you have seen a fair amount of Java. Time to study in more depth the foundations of the language,

Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

An Introduction to the Java Programming Language History of Java In 1991, a group of Sun Microsystems engineers led by James Gosling decided to develop a language for consumer devices (cable boxes, etc.).

Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating

Basic Java Constructs and Data Types Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C 1 Contents Hello World Program Statements Explained Java Program Structure in

Java Crash Course Part I School of Business and Economics Institute of Information Systems HU-Berlin WS 2005 Sebastian Kolbe skolbe@wiwi.hu-berlin.de Overview (Short) introduction to the environment Linux

Answer first, then check at the end. Review questions for Chapter 9 True/False 1. A compiler translates a high-level language program into the corresponding program in machine code. 2. An interpreter is

1001ICT Introduction To Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 1 3 A First MaSH Program In this section we will describe a very

PHP Intro, Syntax, Variables, Echo, Data Types Introduction PHP scripts are executed on the server. What You Should Already Know Before you continue you should have a basic understanding of the following:

Debugging ESE112 Java Programming: API, Psuedo-Code, Scope It is highly unlikely that you will write code that will work on the first go Bugs or errors Syntax Fixable if you learn to read compiler error

csce4313 Programming Languages Scanner (pass/fail) John C. Lusth Revision Date: January 18, 2005 This is your first pass/fail assignment. You may develop your code using any procedural language, but you

Java and the JVM Martin Schöberl Overview History and Java features Java technology The Java language A first look into the JVM Disassembling of.class files Java and the JVM 2 History of a Young Java 1992

Access Tutorial 12: An Introduction to Visual Basic 12.1 Introduction: Learning the basics of programming Programming can be an enormously complex and difficult activity. Or it can be quite straightforward.

Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed Computing Netprog 2002 Java Intro 1 What is

13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.

AppendixA1A1 Java Language Coding Guidelines A1.1 Introduction This coding style guide is a simplified version of one that has been used with good success both in industrial practice and for college courses.

Introduction to C++ January 4, 2011 Massachusetts Institute of Technology 6.096 Lecture 1 Notes: Introduction 1 Compiled Languages and C++ 1.1 Why Use a Language Like C++? At its core, a computer is just

1 7 JavaScript: Control Statements I 7.1 Introduction 2 The techniques you will learn here are applicable to most high-level languages, including JavaScript 1 7.2 Algorithms 3 Any computable problem can

C H A P T E R 2 Common Syntax and Semantic Errors 2.1 CHAPTER OBJECTIVES To understand the fundamental characteristics of syntax and semantic errors To be able to identify specific common syntax and semantic

- 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class This homework is to be done individually. You may, of course, ask your fellow classmates for help if you have trouble editing files,

CSC45 AUTOMATA 2. Finite Automata: Examples and Definitions Finite Automata: Examples and Definitions A finite automaton is a simple type of computer. Itsoutputislimitedto yes to or no. It has very primitive

Scanner The Scanner class is intended to be used for input. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit. For example, suppose the input

Python 3.3 to C/C++ Summer 2014 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. Both require #include directives to access libraries

Java Program Coding Standards 4002-217-9 Programming for Information Technology Coding Standards: You are expected to follow the standards listed in this document when producing code for this class. Whether

To ensure the functioning of the site, we use cookies. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy &amp Terms.
Your consent to our cookies if you continue to use this website.