Java Fall 2018 Margaret Reid-Miller

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Full file at

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

CS 112 Introduction to Programming

Declaration and Memory

Values and Variables 1 / 30

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

3. Java - Language Constructs I

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Program Fundamentals

Chapter 2: Using Data

Basics of Java Programming

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Visual C# Instructor s Manual Table of Contents

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Primitive Data Types: Intro

Chapter 2 Elementary Programming

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

More Programming Constructs -- Introduction

PROGRAMMING FUNDAMENTALS

Fall 2017 CISC124 9/16/2017

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CMPT 125: Lecture 3 Data and Expressions

Datatypes, Variables, and Operations

Introduction to Programming Using Java (98-388)

CSI33 Data Structures

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Computational Expression

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS 251 Intermediate Programming Java Basics

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

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

CIS133J. Working with Numbers in Java

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

Java Primer 1: Types, Classes and Operators

ECE 122 Engineering Problem Solving with Java

Chapter 2: Data and Expressions

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

CIS 110: Introduction to Computer Programming

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture Set 2: Starting Java

ESc101 : Fundamental of Computing

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Lecture Set 2: Starting Java

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 2: Data and Expressions

Introduction to Programming (Java) 2/12

Programming with Java

VARIABLES AND TYPES CITS1001

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Full file at

CEN 414 Java Programming

COMP 202 Java in one week

Information Science 1

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

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

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

Chapter 2 Primitive Data Types and Operations. Objectives

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Language Reference Manual

egrapher Language Reference Manual

Getting started with Java

A variable is a name for a location in memory A variable must be declared

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming Language Basics

CS 231 Data Structures and Algorithms, Fall 2016

Key Differences Between Python and Java

Lecture 1. Types, Expressions, & Variables

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Values, Variables, Types & Arithmetic Expressions. Agenda

Java Bytecode (binary file)

Chapter 2: Data and Expressions

Data Conversion & Scanner Class

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Fundamental of Programming (C)

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

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Mr. Monroe s Guide to Mastering Java Syntax

Using Java Classes Fall 2018 Margaret Reid-Miller

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

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

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Information Science 1

Transcription:

Java 15-121 Fall 2018 Margaret Reid-Miller

Reminders How many late days can you use all semester? 3 How many late days can you use for a single assignment? 1 What is the penalty for turning an assignment late when you run out of late days? No credit Fall 2018 15-121 (Reid-Miller) 2

Java vs Python Program Translation: Java is compiled, Python is interpreted Java syntax: Uses braces {} to group statements (indentation is recommended, but not semantic) Most statements end in semi-colons. Java programs: Often have several classes, each in a file with same name. Java programs start with the method main. Fall 2018 15-121 (Reid-Miller) 3

Today Working with Eclipse Basic Types Types (primitive/reference) (operators/precedence) Variable declaration Expressions Naming conventions Fall 2018 15-121 (Reid-Miller) 4

Declared Types Java requires that you declare the type of each variable (when it first appears in your program) and the return type of each method. Types: boolean (true or false) int (integer) double (floating point decimal) String (sequence of characters) void - return type of a method that returns no value Java checks that the types match when you compile. Compiler error messages often indicate a type mismatch. Fall 2018 15-121 (Reid-Miller) 5

Primitive Data Types Java has two categories of data: primitive data (e.g., number, character) object data (programmer created types) There are 8 primitive data types: byte, short, int, long, float, double, char, boolean Primitive data are only single values; they have no special capabilities. Fall 2018 15-121 (Reid-Miller) 6

Common Basic Types Type Description Example of Literals int integers (whole numbers) 42, 60634, -8, 0 double real numbers 0.039, -10.2, 4.2E+72 char single characters 'a', 'B', '&', '6' boolean logical values true, false String List of characters "", "a", "ab6&" Fall 2018 15-121 (Reid-Miller) 7

Numbers Type Storage Range of Values byte 8 bits -128 to 127 short 16 bits -32,768 to 32,727 int 32 bits -2,147,483,648 to 2,147,483,647 long 64 bits -9x10 18 to 9x10 18 float 32 bits ±10-45 to ±10 38, 7 significant digits double 64 bits ±10-324 to ±10 308, 15 significant digits Fall 2018 15-121 (Reid-Miller) 8

Basic Type Operators Type Description Operators int integers +, -, *, /, %, ++, -- double real numbers +, -, *, / char boolean single characters relational logical ==,!=, <, >, <=, >=,!, &&, String Strings + (concatenation) Fall 2018 15-121 (Reid-Miller) 9

Strings String is not a primitive type, but a class/type String variables store references to memory addresses Strings are immutable! You can assign a new string to a variable, though. A string cannot span more than one line: "Not a valid String literal Useful operations(methods): WRONG! charat, compareto, contains, equals (don t use ==), length, substring, trim (see String API) Fall 2018 15-121 (Reid-Miller) 10

Variable Declaration Before you can use a variable, you must declare its type and name. You can declare a variable only once in a method. Examples: int numdimes; double length; char coursesection; boolean done; String lastname; camelcase is the Java convention Fall 2018 15-121 (Reid-Miller) 11

Declaring Variables Declaring a variable instructs the compiler to set aside a portion of memory large enough to hold data of that type. int count; double length; count length No value has be put in memory yet. That is, the variable is undefined. Fall 2018 15-121 (Reid-Miller) 12

Assignment Statements An assignment statement stores a value into a variable's memory location: <variable> = <expression>; The type of the variable and expression must match. The first assignment to a variable initializes it. count = 3; count 3 length = 72.3 + 2.0; length 74.3 Fall 2018 15-121 (Reid-Miller) 13

Assignment statements shortcuts int n; n = 0; int n = 0; n = n + 1; n += 1; n++; n = n - 1; n -= 1; n--; Other assignment operators: *=, /=, %= Fall 2018 15-121 (Reid-Miller) 14

Data Conversion Widening conversions convert data to another type that has the same or more bits of storage. E.g., short to int, long (safe) int to long (safe) int to float, double (magnitude the same but can lose precision) Narrowing conversions convert data to another type that has the fewer bits of storage and can lose information. E.g., double or float to any integer type int, long to short Fall 2018 15-121 (Reid-Miller) 15

Mixing Types When a Java operator is applied to operands of different types, Java does a widening conversion automatically, known as a promotion. Example: 2.2 * 2 evaluates to 4.4 1.0 / 2 evaluates to 0.5 double x = 2; assigns 2.0 to x "count = " + 4 evaluates to "count = 4" string concatenation Fall 2018 15-121 (Reid-Miller) 16

Mixing Types Conversions are done on one operator at a time in the order the operators are evaluated. 3 / 2 * 3.0 + 8 / 3 2.0 * 4 / 5 + 6 / 4.0 5.0 3.1 Fall 2018 15-121 (Reid-Miller) 17

Mixing Types String concatenation has the same precedence as + - and is evaluated left to right. 1 + "x" + 4 "1x4" "2+3=" + 2 + 3 1 + 2 + "3" "2*3=" + 2 * 3 4-1 + "x" "x" + 4-1 "2+3=23" "33" "2*3=6" "3x" syntax error Fall 2018 15-121 (Reid-Miller) 18

Type Casting Type casting tells Java to convert one type to another. Uses: Convert an int to a double to force floating-point division. Truncate a double to an int. Examples: double average = (double) 12 / 5 int feet = (int) (28.3 / 12.0) Fall 2018 15-121 (Reid-Miller) 19

Type casting Because type casting has high precedence, it casts the operand immediately to its right only. Example: double s = (double) 2 + 3 / 2; double s2 = (double) (2 + 3) / 2; double average = (double) 22 / 4; double average2 = 22 / (double) 4; double wrong = (double) (22 / 4); 3.0 2.5 5.5 5.5 5.0 Fall 2018 15-121 (Reid-Miller) 20

char data type A variable of type char holds exactly one (Unicode) character/symbol. Every character has a corresponding integer value. The digit characters '0' '9' have consecutive integer values, as do the letters 'A' 'Z' and 'a' 'z'. We can use this ordering to sort alphabetically. Conversions: String letter = "" + 'M'; // evaluates to "M" int aasint = 'a'; // evaluates to 97 'a' + 2; // evaluates to 99 char c = (char)('a' + 2); // evaluates to 'c' Fall 2018 15-121 (Reid-Miller) 21

Operator Precedence Operator type Operator Associates grouping (), [],. Left to right postfix <expr>++, <expr>-- Right to left unary/prefix ++, --, +, -,! Right to left cast (type) Right to left multiplicative *, /, % Left to right additive +, - Left to right relational <, <=v >, >= Left to right equality ==,!= Left to right AND && Left to right OR Left to right assignment =, +=, -=, *=, /=, %= Right to left Fall 2018 15-121 (Reid-Miller) 22

Identifiers and Keywords Identifiers are names that specify different elements of a program such as class, method, or variable can be any combination of letters, digits, _ or $ the first character must NOT be a digit case-sensitive (total is different from Total) Examples: main method1 maxcount TUESDAY $amount Puzzle Keywords are a set of predefined identifiers that are reserved for special uses. Examples: public static void class Fall 2018 15-121 (Reid-Miller) 23

Naming Conventions Java naming conventions help readers readily distinguish various Java elements: Class: Starts with a capital letter A class names should be a noun that describe an object type. e.g., String, Radio Method: Starts with a lower case letter A method name should start with a verb and describe what the method does. e.g., displayquestion, getname, computetax Variable: Starts with a lower case letter A variable name should be a noun that describes what data it holds. e.g., favoritefood, name, taxrate Fall 2018 15-121 (Reid-Miller) 24