Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Similar documents
CISC 370: Introduction to Java

9/8/16. What is Programming? CSCI 209: So.ware Development. Discussion: What Is Good So.ware? CharacterisJcs of Good So.ware?

Review: Python Transi,on Warning

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

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

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Basics of Java Programming

Another Simple Program: Adding Two Integers

Chapter 2: Data and Expressions

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS11 Java. Fall Lecture 1

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

Pace University. Fundamental Concepts of CS121 1

Chapter 2: Data and Expressions

B.V. Patel Institute of BMC & IT, UTU 2014

CS 11 java track: lecture 1

Chapter 2: Data and Expressions

Programming Language Basics

1007 Imperative Programming Part II

Program Fundamentals

Java+- Language Reference Manual

Getting started with Java

Full file at

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

VARIABLES AND TYPES CITS1001

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

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

Introduction to Java Applications; Input/Output and Operators

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Values and Variables 1 / 30

4 Programming Fundamentals. Introduction to Programming 1 1

Java Basic Datatypees

Imports. Lexicon. Java/Lespérance 1. PROF. Y. LESPÉRANCE Dept. of Electrical Engineering & Computer Science

Full file at

Visual C# Instructor s Manual Table of Contents

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

Programming with Java

Introduction to C++ with content from

CS 231 Data Structures and Algorithms, Fall 2016

An overview of Java, Data types and variables

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSC Web Programming. Introduction to JavaScript

Main Memory Organization

Review for Test 1 (Chapter 1-5)

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Java Bytecode (binary file)

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Advanced Computer Programming

MODULE 02: BASIC COMPUTATION IN JAVA

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

3 The Building Blocks: Data Types, Literals, and Variables

2 rd class Department of Programming. OOP with Java Programming

First Java Program - Output to the Screen

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

9/16/15. Review: Assign 0. Objec,ves. Review. Review: Python Transi,on Warning. What is the output? INTRO TO JAVA LIBRARIES

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CHAPTER 2 Java Fundamentals

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSI33 Data Structures

Programming Languages and Techniques (CIS120)

Why OO programming? want but aren t. Ø What are its components?

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

PROGRAMMING FUNDAMENTALS

Object-Oriented Programming

Sept 26, 2016 Sprenkle - CSCI Documentation is a love letter that you write to your future self. Damian Conway

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

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

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Getting Started with Java. Atul Prakash

DATA TYPES AND EXPRESSIONS

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Primitive Data Types: Intro

CS102: Variables and Expressions

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Java Fall 2018 Margaret Reid-Miller

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

Course Outline. Introduction to java

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Fundamentals of Programming. By Budditha Hettige

Key Differences Between Python and Java

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

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

Index COPYRIGHTED MATERIAL

Transcription:

Objec+ves Basics of Java Syntax Java fundamentals Ø Primi+ve data types Ø Sta+c typing Ø Arithme+c operators Ø Rela+onal operators 1 Review What are quali+es of good sooware? What is Java? Ø Benefits to using Java? How do you compile a Java program? Ø How do you run/execute a Java program? Purpose of questions? 2 1

Review: Benefits of Java Rapid development of programs Ø Large library of classes, including GUIs, Enterpriselevel applica+ons, Web applica+ons Portability Ø Run program on mul+ple plaxorms without recompiling Sta+cally-typed language Ø Compiling - find some errors before execu+on, performance benefits 3 Review: Java Programming Language En+rely object-oriented Similar to Python Step 1: Program.java Compiler javac Program.class Wri[en in Java Programming Language Bytecode: machine code for a virtual CPU 4 2

Review: Java Virtual Machine CPU (machine code) Step 2: Mac JVM Program.class Bytecode UNIX JVM Windows JVM Same bytecode executes on each plaxorm Don t need to provide the source code 5 Java Class Libraries Pre-defined classes Ø Included with Java Development Kit (JDK) and Java Run+me Environment (JRE) Ø View the available classes online: http://docs.oracle.com/javase/8/docs/api/ Similar in purpose to modules included with Python Sept 9, 2016 6 3

Summary of Java PlaXorm SE 7.0 Image from http://docs.oracle.com/javase/8/docs/index.html Sept 9, 2016 7 Benefits of Java Rapid development of programs Ø Large library of classes, including GUIs, Enterpriselevel applica+ons, Web applica+ons Portability Ø Run program on mul+ple plaxorms without recompiling Sta+cally-typed language Ø Compiling: find some errors before execu+on! Ø Can give performance boost by doing op+miza+ons Sept 9, 2016 8 4

Which Java is this class about? Java programming language Java class libraries Use the JVM but won t learn about how it works Ø For more informa+on: http://docs.oracle.com/javase/specs/ Sept 9, 2016 9 Aside: JavaScript vs Java JavaScript is not Java Ø JavaScript is a scrip&ng language, primarily embedded in HTML, executed by Web browsers Web Browser JavaScript <script type="text/javascript"> function myfunction() { return ("Hello, have a nice day!") </script> </head> <body> <script type="text/javascript"> document.write(myfunction()) </script> Sept 9, 2016 10 5

Java Development Kit JDK: Java Development Kit SDK: SoOware Development Kit Free from Oracle Contains Ø javac: Java compiler Ø java: Java Virtual Machine Ø Java class libraries http://www.oracle.com/technetwork/java/javase/ downloads/index.html Sept 9, 2016 11 Python Review # a Python program def main(): print("hello") main() What does this program do? 12 6

Example Java Program What are your observations about this program? What can you figure out? 13 Example Java Program Everything in Java is inside a class Ø Java is en&rely object-oriented* Ø This class is named Hello 14 7

Example Java Program Blocks of code marked with { Defines the class Hello In general, each Java program file contains one class defini+on* Name of the class is name of file Ø E.g., Hello.java 15 Example Java Program Access Modifier: controls if other classes can use code in this class 16 8

Example Java Program method Class contains one method: main 17 Example Java Program: main Method Similar to main in Python Ø But must be associated with a class Must take one parameter: an array of Strings Ø For command-line arguments Must be public static Must be void: data type of what method returns (nothing) main is automa+cally called when program is executed from command line 18 9

Example Java Program Method contains one line of code Ø What do you think it does? 19 Example Java Program: Print Statements Calls the println method on the System.out object println takes one parameter, a String Displays string on terminal, terminates the line with new line (\n) character 20 10

Example Java Program: Comments /** * Our first Java class * @author Sara Sprenkle */ //print a message Comments: /* */ or // Ø /** */ are special JavaDoc comments 21 Code Style /** * Our first Java class * @author Sara Sprenkle */ Comments at top of program Ø Must include your name Ø High-level descrip+on of program Proper indentaeon Ø Similar to Python Ø Everything within sets of { is indented the same 22 11

What are the Differences? # a Python program def main(): print( Hello ) main() /** * Our first Java class * @author Sara Sprenkle */ //print a message 23 Java vs. Python, so far Seman&cs the same, syntax different Ø Blocks of code Ø End statements Access modifiers Data type declara+ons Class-based programs Compiled We ll see more differences as we go 24 12

Literal Transla+on to Python Program? /** * Our first Java class * @author Sara Sprenkle */ //print a message 25 Transla+on to Python Program class Hello: """Our first Python class""" def init (self): # fill in later def main(self): print ("Hello") Semi-literal transla+on 26 13

JAVA FUNDAMENTALS 27 Print Statement Syntax: System.out.println(<String>); System.out.print(<String>); No newline Similar to Python s file.write() method Ø Need to combine parameter into one String using + s Python s print used commas Ø More on String opera+ons later 28 14

String Concatena+on If a string is concatenated with something that is not a string, the other variable is converted to a string. Note + System.out.println("The answer is " + 78); Automatically converted to a String 29 Escape Sequences Same as Python: Meaning Newline character (carriage return) Sequence In Java, you can print a ' without escaping What does the following display? System.out.println("To print a \\, you must use \"\\\\\""); \n Tab \t Quote \" Backslash \\ First.java 30 15

Java keywords/reserved words Case-sensi+ve Can t be used for variable or class names Reserved words seen so far Ø public Ø class Ø static Ø void Exhaus+ve list Ø http://docs.oracle.com/javase/tutorial/ java/nutsandbolts/_keywords.html 31 Data Types Java is strongly-typed Ø Every variable must be a declared type All data in Java is an object except for the primieve data types: int 4 bytes (-2,147,483,648 -> 2,147,483,647) short 2 bytes (-32,768 -> 32,767) long 8 bytes (really big integers) byte 1 byte (-128 -> 127) float double char 4 bytes (floa+ng point) 8 bytes (floa+ng point) 2 bytes (Unicode representa+on), single quotes boolean false or true 32 16

Variables Must be declared before used Ø Syntax: <datatype> <name> [= value]; Op+onal assignment Variable names typically start with lowercase le[er Ø _ (underscore) also a valid first character Ø Conven+on: Subsequent words are capitalized Called Camel Casing 33 Variable Examples Must be declared before used Ø Syntax: <datatype> <name> [= value]; Examples: Ø int x; Ø double pi = 3.14; Ø char exit = 'q'; Ø boolean isvalid = false; Note must use single quotes for chars Camel Casing 34 17

Floats in Java Decimal literals are considered doubles This code won t compile: Compiler error message: Float.java:13: possible loss of precision found : double required: float float f = 3.14; To fix code, add an f to specifica+on of number or declare as double float f = 3.14; Compiler reads 3.14 as a double Float.java 35 Python Transi+on Warning OK: You cannot redeclare a variable name in the same scope int x = 3; x = -3; Not OK: int x = 3; int x = -3; boolean x = true; Compiler errors 36 18

More Data Type Informa+on Default data types Ø Same as Python 2, not Python 3 Ø Result of integer division is an int Example: 4/3 =?? Cas+ng Ø Similar to Python for primi+ve types Ø Example: 4/(double) 3 TestScore.java 37 Forma[ed Output printf or format Ø System.out is a PrintStream object double d1=3.14159, d2=1.45, total=9.43; // simple formatting... System.out.printf("%6.5f and %5.2f ", d1, d2); // %n is platform-specific line separator, // e.g., \n or \r\n System.out.printf("%-6s%5.2f%n", "Tax:", total); Format.java 38 19

Constants Read-only variables Ø Cannot be assigned new values Keyword final precedes data type Ø Example within a method: final double CM_PER_INCH = 2.540; Why might we want to use constants within a method? 39 Class Constants Constant variable for all methods in class or for mul+ple classes Ø Much more common than constant instance variables Requires static keyword Ø static: for class Ø Also used for methods (will see more later) static final double CM_PER_INCH = 2.540; 40 20

Arithme+c, Rela+onal Operators Java has most of the same operators as Python: Ø Arithme+c operators: +, -, *, /, % No power operator: ** Ø Rela+onal operators: ==,!=, <, >, <=, >= Evaluate to a boolean value Ø Increment and decrement += x, -= y, etc. Addi+onal shortcut for += 1, -=1: ++, -- Conversion.java 41 Unix Output Redirec+on: > We can redirect output to a file Ø For example ls *.java > java_files.out Ø Above command saves the output from the ls command into the file named java_files.out This is how you will save output from your Java programs ini+ally Ø For example java Intro > out Please follow instructions on names 42 21