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
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
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:
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Lecture 3 Number Representation 2 This week – Recap Addition – Addition circuitry – Subtraction of integers in binary – Representing numbers with fractional.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
 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)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
PROCESSING Types. Objectives Be able to convert between binary and decimal numbers. Be able to declare and initialize character variables and constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
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.
1.2 Primitive Data Types and Variables
A: A: double “4” A: “34” 4.
Numbers in Computers.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 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 data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
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 & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 6: Data Types Lectures # 10.
Numbers in a Computer Unsigned integers Signed magnitude
Object Oriented Programming
A First C Program (mixing datatypes)
A First C Program (mixing datatypes)
Assignment and Arithmetic expressions
Java package classes Java package classes.
Review Operation Bingo
Unit-2 Objects and Classes
Negative Integer Representation
Introduction to Java, and DrJava part 1
A First C Program (mixing datatypes)
Introduction to Java, and DrJava
Names of variables, functions, classes
Introduction to Java, and DrJava
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: /2 (= ) fix this minimum int: /2 (= ) 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)