Binghamton University. CS-140 Fall Data Types in Java

Similar documents
Introduction to Programming Using Java (98-388)

Computational Expression

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

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

Java Primer 1: Types, Classes and Operators

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS111: PROGRAMMING LANGUAGE II

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

Object-Oriented Programming Concepts

PROGRAMMING FUNDAMENTALS

McGill University School of Computer Science COMP-202A Introduction to Computing 1

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

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Java Identifiers, Data Types & Variables

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

Java Classes & Primitive Types

Datatypes, Variables, and Operations

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Getting started with Java

Selected Questions from by Nageshwara Rao

Creating Classes and Objects

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Java Classes & Primitive Types

Programming with Java

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

An Introduction to Processing

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Getting started with Java

CSC Java Programming, Fall Java Data Types and Control Constructs

CS 231 Data Structures and Algorithms, Fall 2016

Programming. Syntax and Semantics

Practice Midterm 1. Problem Points Score TOTAL 50

Computer Science II (20082) Week 1: Review and Inheritance

Object-Oriented Programming

9 Working with the Java Class Library

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

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

Operators and Expressions

2. The object-oriented paradigm

CHAPTER 7 OBJECTS AND CLASSES

ECE 122. Engineering Problem Solving with Java

a data type is Types

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Classes Basic Overview

Array. Prepared By - Rifat Shahriyar

Fundamentals of Programming Data Types & Methods

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Lexical Structure (Chapter 3, JLS)

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Lecture 7: Classes and Objects CS2301

CS260 Intro to Java & Android 03.Java Language Basics

Programming II (CS300)

CLASSES AND OBJECTS. Fundamentals of Computer Science I

JAVA GUI PROGRAMMING REVISION TOUR III

BM214E Object Oriented Programming Lecture 4

Zheng-Liang Lu Java Programming 45 / 79

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

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

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

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

CS 251 Intermediate Programming Methods and Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS 251 Intermediate Programming Methods and More

Full file at

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Introduction to Java

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

CHAPTER 7 OBJECTS AND CLASSES

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

Class Libraries and Packages

Object Oriented Modeling

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Data Types, Literals, Operators

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CP122 Computer Science I. Chapter 2: Using Objects

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

c) And last but not least, there are javadoc comments. See Weiss.

CS 314 Principles of Programming Languages. Lecture 9

1B1b Classes in Java Part I

Chapter 6 Introduction to Defining Classes

Chapter 6 Classes and Objects

Compiling Techniques

SECTION II: LANGUAGE BASICS

VARIABLES AND TYPES CITS1001

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

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

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

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

In Java there are three types of data values:

Lab5. Wooseok Kim

Transcription:

Data Types in Java 1

CS-211 2015 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer ownership Move to a new location Repaint Delete a car 2

What s in a class? Fields The data used to describe an object in the class Methods The functions used to manipulate an object in the class 3

What are Data Types? A data type describes how many bits make up the data A data type describes how bits should be interpreted A data type describes what operations are allowed Chap 4 Type Bits Size Encoding Value int 0000 0000 0000 0000 0000 0000 0000 0110 32 30 6 b i 2 i char 0000 0000 0100 0111 16 UTF-16 G float 0100 0000 0100 1000 1111 0101 1100 0011 i=0 32 1 S 1. sig 2 exp 3.14 4

Java Primitive Data Types Flavor Type Bits Range Precision logic boolean? (8) true or false byte 8-128 to +127 short 16-32,768 to +32,767 Integer char 16 0 to 65,535 exact int 32 ~-2.14x10 9 to ~2.14x10 9 long 64 ~-9.22x10 18 to ~9.22x10 18 floating point float 32 ~-10 38 to ~+10 38 ~7 digits double 64 ~-10 308 to +10 308 ~15 digits Sect. 4.1.1 5

Declaring Local Variables Syntax (within a method) type name; type name=constant; Examples int j; int count=0; or count is an instance of an integer whose initial value is zero 6

Declaring Variables Sect. 2.2 Variables must be declared and initialized before they are used Java is an imperative language, and this is typical Enables compiler to check to make sure variables are used correctly Declaration and initialization can be combined Declaration specifies type how this variable will be used and what values are valid specifies name how you will refer to this value from now on Initialization specifies the first value that the variable will have. 7

Examples of primitive variable declaration int value = 1; // declaration and initialization int input; // declaration input = 5; // initial value or a change of value char letter = 'g'; char akoreanchar = '\ud55c'; // 한 8

More examples: long large = 1234567890123456789L; boolean choice = false; double precipaug = 3.59; float precipjuly = 2.69F; byte tiny = (byte)110; // values from -128 to 127 short small = (short) 25000; // from -32768 to 32767 9

Declaring Fields (or Instance Variables) For each data that is used to define an object We need to define the name of the field We need to define the type of the field Field Declaration Syntax: class classname { type fieldname; type fieldname; } 10

Simple example with Primitive Fields class Account { long id; int dollars; byte cents; } Fields Schematic Account Object Fields used to define what we want to model about an account Account id 7789435 dollars 12417 cents 35 Class name Values 11

Field Declarations with References class Angle { short degrees; byte minutes; double seconds; } class Position { Angle latitude; Angle longitude; } seconds is an instance of a double value longitude is a reference to an Angle object 12

Field Declarations with References class Angle { short degrees; byte minutes; double seconds; } Position latitude longitude class Position { Angle latitude; Angle longitude; } Angle degrees 42 minutes 5 Angle seconds 14.78 degrees 75 minutes 58 seconds 5.35 13

Classes as Types Primitive types pre-defined types known to Java only 8 : boolean, byte, short, int, long, char, float, double Java allows types which are references to objects Type is the class name Result is a reference variable like a pointer in C, but with extra rules Reference variables can be thought of as a handle on the encapsulated object 14

CS-211 2015 Comparing Types and Classes as Types Data Types A data type describes how many bits make up the data A data type describes how bits should be interpreted A data type describes what operations are allowed Classes as Types A class describes how much data make up the object A class describes how to interpret its fields A class defines what methods may be applied to an object 15

Example Field Declarations class Car { String make; String model; short year; Color color; FullName owner; Position location; double mileage; } How Cars are Described Make Model Year Color Owner Location Mileage 16

Local Reference Variable Examples We can declare reference variables: Counter mycounter; Account myacc; String str; java.io.printwriter output; java.awt.rectangle rect; Chap. 3 Reference Variables need to be instantiated before they are used Instantiation defines which object the reference variable refers to 17

Instantiating Reference Variables Need to call class constructor to create a new object mycounter = new Counter(); myacc = new Account(150.0); String str= "My String"; //same as: String str = new String("My String"); output = new java.io.printwriter( new java.io.fileoutputstream("out.txt")); java.awt.rectangle rect = new java.awt.rectangle(10,30,25,60); 18

Combined Declaration & Instantiation Most typically, java code declares a reference variable, and instantiates that reference variable with a new object in the same statement Counter mycounter = new Counter(); Notice the required redundancy required First Counter is the type of variable mycounter Second Counter is the invocation of the Counter class creation method 19

Reference Variables Schematically Account myacc = new Account(150.0); myacc Account id 7789436 dollars 150 cents 0 20

Reference Variables Handles on the encapsulated object The handle only fits objects in a specific class We need the handle to be able to pass the object around and/or use that object The handle is either attached to a specific object or not attached to any object (null) The handle is attached to an object with an assignment statement NO ARITHMETIC ON HANDLES!!!!! 21

Instantiating Reference Variables output = new java.io.printwriter( new java.io.fileoutputstream("out.txt")); java.awt.rectangle rect = new java.awt.rectangle(10,30,25,60); To reference the PrintWriter method in the io sub-package of the java package, we need to specify java.io.printwriter Writing package names every time is a nuisance 22

Importing Packages importing a package allows you to use components without package names import java.io.*; import java.awt.rectangle; output = new PrintWriter( new FileOutputStream("out.txt")); Rectangle rect = new Rectangle(10,30,25,60); 23

Class (Static) Variables If you use the static keyword, that changes a field into a class variable instead of a normal field Class variables are shared by all objects Created when the class is loaded the start of the program Deleted when the class is dismissed the end of the program May be initialized when declared Often used to keep track of things which a higher level class could take care of 24

Example Class Variable class Widget { String wtype; String datemfg; static int count=0; public Widget(String t) { wtype=new String(t); count++; } public static void howmany() { System.out.println( } } Built + count + widgets. ); 25