March 2005 1/18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple.

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

March /18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Review of Primitive TypesReview of Primitive Types Arithmetic OperationsArithmetic.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
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.
March R. Smith - University of St Thomas - Minnesota Today’s Class Recap of a simple programRecap of a simple program A “history” of CA “history”
March R. Smith - University of St Thomas - Minnesota Today’s Class IntroductionsIntroductions About the CourseAbout the Course FundamentalsFundamentals.
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.
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.
March /20R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Revised history of JavaRevised.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Hello, world! Dissect HelloWorld.java Compile it Run it.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Week 2 - Wednesday CS 121.
Chapter 2 Variables.
Chapter 2 Basic Computation
Multiple variables can be created in one declaration
Variables and Primative Types
Chapter 2 Basic Computation
IDENTIFIERS CSC 111.
5 Variables, Data Types.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Java Fundamentals
Java Programming Review 1
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

March /18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple types and printing “Primitive types” in general“Primitive types” in general Back to “Lab #1”Back to “Lab #1” – due first thing TOMORROW.

March /18R. Smith - University of St Thomas - Minnesota Sample Java Program public class hello { public static void main (String[] args) { System.out.println(“Hello, world!”); }}

March /18R. Smith - University of St Thomas - Minnesota “Compiling” - the mechanics Should work the same on PCs and MacsShould work the same on PCs and Macs You have to “compile” programs to run themYou have to “compile” programs to run them $ javac hello.java $ You have to use the “JVM” to run them, tooYou have to use the “JVM” to run them, too $ java hello Hello, world! $

March /18R. Smith - University of St Thomas - Minnesota “Types” of variables First use: you say the typeFirst use: you say the type int = “integer”int = “integer” signed, no decimal part, less than 2Gsigned, no decimal part, less than 2G String = character stringString = character string Usually captured in quotesUsually captured in quotes Note that “String” is capitalizedNote that “String” is capitalized ExamplesExamples int sampleInt = 12; sampleInt = 25; System.out.println(sampleInt);

March /18R. Smith - University of St Thomas - Minnesota String and int example String myname = “Joe”; int age = 25; System.out.print(“My name is “ +myname + “. I am “ + age +”.”); myname = “Barb”; age = 26 System.out.print(“My name is “ +myname + “. I am “ + age +”.”);

March /18R. Smith - University of St Thomas - Minnesota Primitive Types Java programs deal with 2 kinds of thingsJava programs deal with 2 kinds of things –Primitive Types, like int –Objects, like String (note it’s capitalized) Four ‘types’ of primitive typesFour ‘types’ of primitive types –Integers in 4 sizes –Floating point numbers in 2 sizes –Booleans - Chars WHY???WHY??? –It’s based on computer hardware –Computers can easily deal with those 8 types

March /18R. Smith - University of St Thomas - Minnesota Primitive types vs Objects Primitive Types Variables onlyVariables only Primitive operationsPrimitive operations One value eachOne value each No AttributesNo Attributes No MethodsNo Methods Objects Attributes Methods Multiple values

March /18R. Smith - University of St Thomas - Minnesota Java is “Strongly Typed” Prevents “risky” data movementPrevents “risky” data movement ExampleExample byte little; int bigger; long biggest; bigger=little; // OK little=bigger; // ERROR; biggest=little; //OK little=biggest; //ERROR

March /18R. Smith - University of St Thomas - Minnesota Chars and Strings are interesting Consider the following:Consider the following: int intThree = 3; String strThree = “3”; char charThreeA = 51; char charThreeB = ‘3’; We can print all of these.We can print all of these. –We can only do arithmetic on the first one The char holds a numeric codeThe char holds a numeric code –We get digits for numeric values 48 plus –We get letters for numeric values 65 plus A String is an “array” of charsA String is an “array” of chars

March /18R. Smith - University of St Thomas - Minnesota Lab Problem #1 Given the text, write print and/or println method calls to print it out. Replace the name (“Jon”) and pronoun (“he”) with string variables. Set the variables to different values and produce the results: Name=Sally, pronoun=she Name=Frank, pronoun=he

March /18R. Smith - University of St Thomas - Minnesota The Text Jon went to the store. When he arrived, he got a shopping cart. Then he walked to the produce section. Jon was surprised. The raspberries were fresh, so he picked up two boxes. Then he went to the cereal section and picked up three boxes. Jon really likes cereal. After he had visited the whole store, Jon pushed the cart to the front. There, he paid for the groceries. It took six bags to carry all of Jon's groceries. The End.

March /18R. Smith - University of St Thomas - Minnesota Suggestion Do the first line, compile it, run it.Do the first line, compile it, run it. –Fix what doesn’t work, try again till it’s right. Add another line, compile, run.Add another line, compile, run. Etc.Etc. Find and fix problems incrementally.Find and fix problems incrementally. –It’s much easier than trying to fix 30 different errors.

March /18R. Smith - University of St Thomas - Minnesota Summary of Data Types IntegerInteger FloatFloat BooleanBoolean CharChar

March /18R. Smith - University of St Thomas - Minnesota Integer Data Types The SizesThe Sizes –byte = range +/- 127 –short = range +/ –int = range +/- 2 billion –long = range +/- 9 quintillion Literal valuesLiteral values –Must be all digits –No decimal point, or the letter E.

March /18R. Smith - University of St Thomas - Minnesota Floating point data types Two floating point number sizesTwo floating point number sizes –float = 7 significant digits, +/- 10^38 –double = 15 significant digits +/- 10^308 Literal valuesLiteral values –Insert a decimal point, you get a double –End it with the letter “f” and you get a float –Scientific notation: 2.34E-23

March /18R. Smith - University of St Thomas - Minnesota Boolean data type Yield values “true” and “false”Yield values “true” and “false” Actual variables take up the smallest practical amount of RAMActual variables take up the smallest practical amount of RAM Boolean expressions are essential for controlling your programsBoolean expressions are essential for controlling your programs Literal boolean values:Literal boolean values: –true, false

March /18R. Smith - University of St Thomas - Minnesota Character ‘char’ data type Stores a single keystrokeStores a single keystroke –Letter, digit, punctuation, etc. –Strings are “arrays” of chars LiteralsLiterals –A character enclosed in single quotes ‘A’ ‘B’ ‘C’‘A’ ‘B’ ‘C’ –A ‘String’ is enclosed in double quotes “A B C”“A B C” A char is also a numberA char is also a number –A coded value for a keystroke

March /18R. Smith - University of St Thomas - Minnesota