Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Similar documents
Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Java Bytecode (binary file)

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programming with Java

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

Java Basic Programming Constructs

Lecture Set 4: More About Methods and More About Operators

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Pace University. Fundamental Concepts of CS121 1

Course Outline. Introduction to java

double float char In a method: final typename variablename = expression ;

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

CS 302: Introduction to Programming

Data Structure and Programming Languages

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Full file at

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

CMSC131. Introduction to your Introduction to Java. Why Java?

Important Java terminology

DATA TYPES AND EXPRESSIONS

b. Suppose you enter input from the console, when you run the program. What is the output?

AP CS Unit 3: Control Structures Notes

Arithmetic and IO. 25 August 2017

4 WORKING WITH DATA TYPES AND OPERATIONS

4 Programming Fundamentals. Introduction to Programming 1 1

PROGRAMMING FUNDAMENTALS

Program Fundamentals

Computational Expression

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

Section 2.2 Your First Program in Java: Printing a Line of Text

Full file at

Object-Oriented Programming

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Introduction to Java & Fundamental Data Types

Oct Decision Structures cont d

CS111: PROGRAMMING LANGUAGE II

Introduction to Software Development (ISD) David Weston and Igor Razgon

Elementary Programming

1. An operation in which an overall value is computed incrementally, often using a loop.

Key Differences Between Python and Java

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Simple Java Reference

DM503 Programming B. Peter Schneider-Kamp.

Introduction to Programming Using Java (98-388)

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

CS 231 Data Structures and Algorithms, Fall 2016

Computer Components. Software{ User Programs. Operating System. Hardware

AP Computer Science A

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

DM550 Introduction to Programming part 2. Jan Baumbach.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Getting started with Java

Introduction to Computer Science Unit 2. Notes

Lecture Set 4: More About Methods and More About Operators

Zheng-Liang Lu Java Programming 45 / 79

Lec 3. Compilers, Debugging, Hello World, and Variables

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Using Java Classes Fall 2018 Margaret Reid-Miller

Section 2.2 Your First Program in Java: Printing a Line of Text

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

( &% class MyClass { }

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Introduction to Java Applications

Java Coding 3. Over & over again!

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Loops and Expression Types

Mr. Monroe s Guide to Mastering Java Syntax

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Introduction Basic elements of Java

Object-oriented programming in...

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Chapter 3. Selections

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Language Reference Manual

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

CSE 114 Computer Science I

4. If the following Java statements are executed, what will be displayed?

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Java Classes: Math, Integer A C S L E C T U R E 8

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

MODULE 02: BASIC COMPUTATION IN JAVA

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

1007 Imperative Programming Part II

CS2141 Software Development using C/C++ C++ Basics

Transcription:

Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac TestClass.java Creates a new executable file: TestClass.class To run it: > java TestClass If TestClass uses other classes that you create, make sure they are in the same directory and they will be compiled at the same time with.class files of their own. Classes, Objects, and Methods Java class object method Python class object method In Python, a method is a function within an object, which is defined by a class. In Java, all functions are in objects or classes so they are all called methods. Hello World def main(): print Hello World ---------------------------------------------------------------- System.out.println( Hello World"); System.out.println( Hello World"); 1

A class definition for the class named HelloPrinter. Curly brackets mark the start and end of the class definition. A class definition The name of the file for a public class must match the class name: HelloPrinter.java public means it can be accessed from outside of this file. private means only accessed within this file A method definition for the method named main. 2

Curly brackets mark the start and end of the method definition. As in Python the method main has special meaning. The execution of the file starts here. This portion allows you to create a main method that will take parameters (command line arguments). public means it can be accessed from outside of this class. private means only accessed within this class. static means that this method (main) does not operate on an object. Without the word static it does operate on an object. void indicates the type of object it returns. In this case it does not return anything so to indicate that we use the word void. 3

This line prints the string Hello World to the screen. println is a method that takes in a string as a parameter and prints it and then goes to a new line. print is another method that prints the parameter string, but does not add the newline. println operates on the object out (an object of the class PrintStream), which refers to the system output or the console window. out is declared in the class System, which is a class that is part of the Java library. The semicolon ; marks the end of the line of execution. Java Has a Free-Form Layout public static void main(string[] args) Means the same as: 4

Java Has a Free-Form Layout Punctuation is very important! Python relied on indentation to differentiate one command from another. Java uses punctuation such as curly brackets and semicolons ; Nevertheless, indentation is very important for the readability of Java programs. Your Python training will serve you well. Java Capitalization Conventions The first letter of a class name should be capitalized, but not all letters in the name. The first letter of all variables and methods should be lower case. All the letters of a constant should be capitalized. Typing has strong typing All values have a type. Variables must be defined as a type and only used to store values of that type. Strong Typing Once a variable is defined to be of a certain type, you can only assign it values of that type. int num; num = 7; num = 8.5; // OK at this point // will result in an error Typing There are class types and primitive types. Example class type: String Example primitive types: int, float, char Class Type String greeting = Hello ; In this command, the object greeting is defined to be of type String and is assigned the value Hello. This can be done in two steps if desired: String greeting; greeting = Hello ; 5

Class Type Method Primitive Type Since String is a class, there are methods defined to operate on objects of that class. For example the method length returns the number of characters in the string. greeting.length() will return 5 defined types that are not classes. The primitive building block Integers: byte, short, int, long 1,2,4,8 bytes Real Numbers: float, double 4, 8 bytes Characters: char 2 bytes Boolean: boolean (true or false) 1 bit int num = 256; Primitive Type In this command, variable num is declared to be of type int and assigned the value 256. Primitive Type Method Since int is not a class, there are no methods defined to operate on an int. For example there is no method length that operates on an int: num.length() will return an error Static Methods Could be called Class Methods Static methods do not operate on objects. The reason for static methods is to operate on the parameters of the method as opposed to objects. Static Methods There are static methods defined in Java that can be used to manipulate primitive types. For example sqrt is a static method defined in the class Math. Math.sqrt(num) will return 16. 6

Static Methods public static int max3 (int k, int m, int n) if (k > m) if (k > n) return k; return n; if (m > n) return m; return n; public class Util public static int max3 (int k, int m, int n) if (k > m) if (k > n) return k; return n; if (m > n) return m; return n; Static Methods In the program, you would call the static method as follows: int a = 5; int b = 8; int c = 4; int max_int; max_int = Util.max3(a, b, c); or possibly: max_int = Util.max3(9, b, c); Mathematical Operators Python Java + + - - * * / / ** Math.pow(x, y) % % abs(x) Math.abs(x) sin(x) Math.sin(x) x+=1 x++ int i = 5; int j; Increment / Decrement j = i++; j has the value 5 and i has the value 6 i = 5; j = ++i; Both j and i have the value 6 Works the same with -- Division The same in Python and Java 10 / 4 returns 2 10.0 / 4.0 returns 2.5 10.0 / 4 returns 2.5 ints are automatically converted to floats 7

Casting Strings x = 3.3 Both Python and Java have + abc + def returns abcdef Python int(x) returns 3 Java (int) x returns 3 Substring abcs = abcdefghijk Python abcs[0:3] Java abcs.substring(0,3) returns abc returns abc String number Reading Input eval( 19 ) returns 19 eval( 19.8 ) returns 19.8 Integer.parseInt( 19 ) returns 19 Double.parseDouble( 19.8 ) returns 19.8 grade = input( Enter grade: ) import java.util.scanner; // before class def Scanner in = new Scanner(System.in); System.out.print( Enter grade: ); int grade = in.nextint(); Booleans Comparisons the same: <, <=, ==, >=, >,!= For Java Strings string1.equals(string2)! string1.equals(string2) : True, False Operators: and, or : true, false Operators: &&, if x < 0: x = x * x 8

x = x + 5; same as: different from: x = x + 5; x = x + 5; if x < 0: x = x * x print x different from: x = x + 5; System.out.println(x); if x < 0: x = x * x : x = 2 * x if x < 0: x = x * x elif x > 0: x = 2 * x : x = -1 if (x > 0) 9

System.out.println(x); if (x > 0) System.out.println(x); if (x > 0) Same as: if (x > 0) Same as: if (x > 0) if (x > 0) Very readable. The first true condition executes; the rest are not executed. if (y < 0) x = x * y; if (x > 0) if (y < 0) x = y; Nested x = 5.2 for i in range(10): x = 3.9 * x * (1-x) print x double x = 5.2; int i; for (i = 0; i < 10; i++) x = 3.9 * x * (1-x); System.out.println(x); For Loops 10

for i in range(2,11,2): print i For Loops for (int i = 2; i < 11; i=i+2) System.out.println(i); For Loops for (i = 2; i < 11; i=i+2) System.out.println(i); for (i = 2; i <= 10; i=i+2) System.out.println(i); While Loops i = 1 while i <= 10: print i i = i + 1 int i = 1; while (i <= 10) System.out.println(i); i = i + 1; 11