Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Java Syntax Primitive data types Operators Control statements.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Copyright Curt Hill Variables What are they? Why do we need them?
Java Programming, Second Edition Chapter Two Using Data Within a Program.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Chapter 7: Expressions and Assignment Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 6: Data Types Lectures # 10.
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Java package classes Java package classes.
Lecture 5 from (Chapter 4, pages 73 to 96)
Java - Data Types, Variables, and Arrays
Introduction to Java, and DrJava part 1
C Programming Getting started Variables Basic C operators Conditionals
Names of variables, functions, classes
Introduction to Java, and DrJava part 1
Presentation transcript:

Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities

boolean values: true, false operations: –&&“and” –||“or” –!“not”

int values: 0, 1, -1, 2, -2, … maximum int: (= ) minimum int: (=2 32 ) operations: + - * / % 5+2 = 7+: (int,int)  int 5-2 = 3-: (int,int)  int 5*2 = 10*: (int,int)  int 5/2 = 2 (quotient) /: (int,int)  int 5%2 = 1 (remainder)%: (int,int)  int

integral types’ representations representation used differs according to whether type is signed (byte, short, int, long) or unsigned (char): –signed integral values are represented using “two’s complement” representation –unsigned integral values are represented using “binary” representation size of representation differs according to type: –byte is 1 byte wide (1 byte = 8 bits) –short is 2 bytes wide –int is 4 bytes wide –long is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!

types of operators for type int Notice that all of these operators take two int arguments, and produce an int result. There is hardware circuitry to perform these operations.

double values: 0.0, 1.0, -3.5, e-3 inexact representation (!) operations: + - * / = 7.0+: (double,double)  double 5.0 – 2.0 = 3.0-: (double,double)  double 5.0 * 2.0 = 10.0*: (double,double)  double 5.0 / 2.0 = 2.5/: (double,double)  double

floating point types’ representation both double and float use the IEEE754 representation scheme size of representation differs according to type: –the representation of a float is 4 bytes wide –the representation of a double is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!

mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation too! “Good” + “ ” + “morning!”  “Good morning!” What happens in an expression which mixes values of different types? = ???? 5 is coerced to its equivalent double value, 5.0: = 7.5 Type coercion happens only from “smaller” type to “larger” type (e.g. int  double, not double  int)

relational operators We can form expressions like: x < y which have a value of true or false (i.e. the type of this expression is boolean) relational operators: >= ==

Equality testing Equality testing: –of primitive values: == –of objects: equals method Consider: Foo a = new Foo();Foo c = a; Foo b = new Foo(); what is value ofwhat is value of (a==b)(a==c)

assignment = ; evaluate, assign value to Example: int x = 5; int y = x + 3; // what value is assigned to y?

assignment Example: int x = 5; x = x + 3; // what value is assigned to x? Notice that “=” is not “equality”, but “assignment”.

Some other operators ++ increments the variable it is applied to Example int x = 5; x++;// value of x is now 6 (Caution: the value of the expression x++ is perhaps not what you would expect. The value of x++ is the value of x prior to the increment of x.)

Control structure review if ( ) true false

Control structure review if ( ) else true false

Control structure review while ( ) true false

Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring to many objects? No, but we saw how to build a composite object. IBehavior cc = new CompositeBehavior(cc,br); But it’s not too easy to add/remove items from a composite. Are there options?

Collections interface: java.util.Collection classes: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection

Use of a collection HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …

for-each loop for (String name : names) { System.out.println(name); } This would print out: Amy Cindy Dave Emma

Collection.shuffle Collection.shuffle(List )