CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
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.
Class 7.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
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.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
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.
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.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Primitive Variables.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 2. Program Construction in Java. 01 Java basics.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Variables and Primative Types
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
C++ Data Types Data Type
Introduction to Java, and DrJava
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to Java, and DrJava part 1
Presentation transcript:

CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall

Data in a Computer stored in memory at address locations addresses and data are in binary code letter 'A' stored as could be stored in location such as:

Program Data Can Be variables the program can change the values e.g., different customers could have different values for customerAge constants will always be the same in program LA_COUNTY_SALES_TAX

Data a variable or constant is defined by: name (looks up its location in memory) must start with a letter (not a number) data type e.g., various types of numbers, character, logical, etc. actual data value

Naming Java Data can include letters, numbers, and the special characters $ and _ (underscore) must start with a letter convention: use lower case for 1 st letter must not be a Java "reserved word" eg., can't name a variable Class or public

Declaring Data declaring is identifying a variable before it is used Java is "strongly typed" must declare all variables with data types declaration is a program statement, so it needs to end with ; int myAge; // type int (integer)

Declaring Data - 2 can declare a variable and also assign a value in same statement char middleInitial = 'P'; can declare multiple variables in one statement, if they are same type char firstInitial, middleInitial ;

Java "Primitive" Data Types individual items, not combinations numeric integer: byte, short, int, long decimal: float, double logical boolean character char

Numeric Data Types integers (whole numbers) byte (–128 to +127) 8 bits (1 byte) short ( to ) 16b (2B) int (± 2 billion) 32b (4 B) long (± 9 quintillion) 64b (8B) # of (decimal) digits = 30% x # of bits

Numeric Data Types - 2 floating point (have decimals) e.g., 1.2, 297,765, ) float (7 significant [accurate] digits) 32b can have 38 0s before or after decimal point (contra text p. 92) double (15 significant digits) 64b can have 308 0s before or after decimal point

Numeric Data Types - 3 tradeoff: memory vs. reliability declaring reserves memory long data type uses 8 bytes per variable storing a lot of small integers in long variables will waste memory program will crash if try to put a large number in a data type that isn't big enough

Arithmetic Operators + addition - subtraction * multiplication / division

Arithmetic Operators - 2 integer arithmetic is different: / division 8/5 = 1 % modulus (remainder) 8%5 = 3

Numeric Type Conversions if variables in a calculation are of same type, result is same type if add, divide, etc. integers, result is integer for different types, result is type that holds largest number (unifying type) largest to smallest types: double, float, long, int, short, byte

Boolean Data Type boolean value is either true or false boolean goodStudent = true comparison operators less, greater than (4<8) ==, != equal, not equal (4!=9) = less or equal, greater or equal boolean ok = (age < weight) ;

Character Data Type holds one character ('A', 'b', '3', etc.) must be enclosed in single quotes 1 != '1'

Character Data Type - 2 can be an "escape character" escape means don't treat as normal letter '\b' has same effect as backspace key '\n' moves down to next line '\r' moves to start of line '\n' + '\r' is same as hitting Enter key

Java Constants value is "hard coded" program can't change the value float BASE_TAX_RATE =.15 "naming convention" is to use all capital letters to make easier to spot in code