Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = 3.14159; … areaOfCircle = radius *

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
Types and Variables. Computer Programming 2 C++ in one page!
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
A simple program: Area of a circle Problem statement: Given the radius of a circle, display its area Algorithm: 4. Display area To store values, we need.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 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)
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Primitive Variables.
Primitive Variables.
Chapter 2 Variables.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 MATERI PENDUKUNG TIPE DATA Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Chapter 2 Variables.
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Chapter 2.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Primitive Types and Expressions
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

Fundamental data types Horstmann Chapter 4

Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius * radius * PI;

Representing numbers Given 3 bits, what numbers can you represent? unsigned: Range 0… (= 0…7) signed: -2 2 … At base, everything in a computer is stored by bits of memory, which can be either on or off (1 or 0). 2 2 * * *1= = * * *0= = * * *1= = 5

Numerical data types: integers Name Range Size int … bits (signed) byte -2 7 … bits (signed) short … bits (signed) We can write big numbers using scientific notation: byte variables can fit numbers from –128 to 127 short variables can fit numbers from –32768 to int variables can fit numbers from – to long … bits (signed) ~ 2.15e+9 ~ 2.15*10 9 long variables can fit ~2.15e+9 times as many numbers as ints

Numerical data types: reals float and double are used to store real numbers: numbers with a (possibly unending!) sequence of decimals; for example, pi: double -1.7e308 … 1.7e (14…15 digits acc.) float -3.4e38 … 3.4e38 32 (6..7 digits accuracy) … float and double can only store a certain number of decimal digits: they can’t store them all! float pi = ; \\ the last 3 is a rounding up double pi = \\the 2 is rounding down

What do these mean? int … bits (signed) short … bits (signed) 2 15 = ( 2x2x2x2x2x2x2x2x2x2x2x2x2x2x2 ) = 32,768 How much bigger is 2 31 than 2 15 ?2 16 times bigger 2 31 = 2,147,483,648 float -3.4e38 … 3.4e38 32 bits (signed) 3.4e38 means 3.4 x (scientific notation) To compare, 2,147,483,648 ~=2.1 x 10 9

Numerical Operators +,-,/,*,% int i1 = ; // i1 becomes 35 double d1 = ; // d1 becomes 33.9 double d2 = 1.0/2.0; // d1 becomes 0.5 int i2 = 1/2; // i1 is 0!!!!! byte i3 = 1/2; // same for all integer types byte i4 = 20%3; // modulus operator. i4 is 2

Literals A basic data type value which appears directly is a literal int i = 34; // 34 is a literal. double d = 5.0; // 5.0 is a literal, /* floating point values are taken to be doubles until proven otherwise*/ float myFloat = 5.0f; // f means make this a float float myDouble = 5.0d; // d means make it a double!!

Shortcut Operators Some operations are so common, we have a shorthand. The following are equivalent: i = i + 1;i += 1; d = d - 1.0;d -= 1.0; f /= 2.0;f = f / 2.0; i++; d--;

Fundamental Data Types byte, short, int, long float, double char boolean Are all fundamental data types. Fundamental data types always start with a small letter. } Variables of fundamental data types are just boxes in memory. Java gives us simple operations (like +, -, *, / etc) for doing things with fundamental data types.

“Reference” data types String is not a primitive data type. String is a “reference” data type (it starts with a capital letter). Reference data types refer to (contain) a number of primitive elements combined together. For example, in String myName = “fintan”; The variable myName contains 6 char objects: the char s: ‘f’ ‘i’ ‘n’ ‘t’ ‘a’ ‘n’ Reference data types are very different from primitive types. They are not just boxes: they are Object s containing both data and methods (commands) for doing things with that data.

Converting String s to numbers We often want to convert a string into a number. One common situation is if the string has been entered by the user in a JOptionPane, for example. To convert Strings to some fundamental number type, we use conversion methods in a “Reference” datatype: String s = “10.123”; double d; d = Double.parseDouble(s); Note the capital letter in the “reference” data type After this, the variable d contains the double parseDouble is a method that converts (parses) a string into a double. It’s stored in the Double reference type.

Converting numbers to Strings Similarly, to convert numbers to Strings, we also use conversion methods in a reference type. int i = 10; String anIntString = Integer.toString(i); This converts the value 10 in the int i into the string “10” and puts in in a String variable. Integer holds methods for converting int s to String s. Note the capital letter!!! And the variable i as an argument. Primitive type Reference type holding methods intInteger floatFloat doubleDouble Reference types always start with capital letters

String conversion summary String s1 = “2”; String s2 = “10.123”; int i; float f; double d; i = Integer.parseInt(s1); f = Float.parseFloat(s2); d = Double.parseDouble(s2); s1 = Integer.toString(i); s2 = Float.toString(f); s2 = Double.toString(d); Strings to numbers: note String variables as arguments } Numbers back to strings: note number variables as arguments }

Converting between fundamental types Number range hierarchy: double > float > int Assigning smaller range variable to larger range variable is ok: int i = 5; float f = i; // ok double d = f; // also ok These conversion methods are used for converting Strings to fundamental data types such as int, double etc. For converting between fundamental types, things are simpler.

Type conversion 2 To assign a larger range value to a smaller variable, use a type cast. This tells the computer to “squeeze” the value into the variable. Caution is always required! You may lose information because you may be putting a big number into a too-small box. float fl = (float)10.1; // double to float. ok. int i = (int)fl; // i now has the value 10! int i2 = 1000; // ok byte b = (byte)i2; // very bad idea!!!!

Casting to integers..... REMEMBER! = loses all information after the decimal point. int i = (int)4.1; // i is now 4 double d = -2.5; i = (int)d; // i is now -2 The Math.round() method can be used to round double numbers to the nearest integer. Look up Math.round() in the API

Character data type type char holds a single character: char c1 = 'A'; char c2 = '7'; char c3 = '\u0041'; // unicode for A Characters map onto numbers: int code = (int)'A'; Write a program to investigate characters and integers....

Special characters char c1 = '\n'; // new line char c2 = '\t'; // tab stop char c3 = '\"'; // double quote More often, we use strings: String s1 = "Hello world"; String s2 = "I said \"Hello world\"\n";

The boolean data type A variable of type int can have 2 32 different values A variable of type boolean can have 2 different values: boolean b = true; b = false; More on booleans when we consider "if-statements"