Primitive Types CSE 115 Spring 2006 April 3 & 7 2006.

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

Air Force Institute of Technology Electrical and Computer Engineering
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
 Instructor Nash  Readings: Savitch, Chapter 3 (Section 3)  CSS 161 V2.0.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
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.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
Introduction to Java Programming, 4E Y. Daniel Liang.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Building Java Programs Primitive Data and Definite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive 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.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operators.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 CS161 Introduction to Computer Science Topic #6.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
© 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.
Lecture 3 Java Operators.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Building Java Programs Chapter 2
Examples of Primitive Values
Computers & Programming Languages
Building Java Programs Chapter 2
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs Chapter 2
Primitive Data Types and Operators
Building Java Programs
Presentation transcript:

Primitive Types CSE 115 Spring 2006 April 3 &

Type  A set of values and the operations we can do with those values.

Object Types vs. Primitive Types  We have been using object types all semester.  Primitive types are another type built into Java.  The distinction between these two types has been significantly blurred by Java 5.

Primitive Types  Primitive Types ≠ Objects  The way we create them is different.  Primitive types have a value.  Objects have instance variables.  Primitive types have operations we can perform on them.  Objects have methods we can call on them.

Types of Primitives  Numbers  Booleans  Characters

Numbers  Whole Numbers  byte – 1 byte (-128 to 127)  short – 2 bytes  int – 4 bytes ( to )  long – 8 bytes

Operations on Numbers (Unary) +  Promotes a byte, short or char to an int -  Unary negation ++  Increment --  Decrement

Operations on Numbers (Binary) +  Addition -  Subtraction *  Multiplication /  Division %  Modulus

Important Note  In Java, operations on integers are closed. This means that an operation performed on an integer returns an integer, or likewise, an operation performed on two integers returns an integer.

More Numbers  Real/floating point numbers  float  double  Operations: Same as for whole numbers

Important note  Operations on two real numbers return a real number.  Operations on a real number and a whole number return a real number.

How to get the “real” answer to the division of two integers?  Typecasting – we can force Java to believe that we are doing floating point arithmetic. int a = 1; int b = 2; double realAnswer = (double)1/2;

Booleans  Two-valued algebra system  True  False  The boolean values in Java are true and false (not 0 and 1).

Operations that return boolean values <  Less than >  Greater than <=  Less than or equal to >=  Greater than or equal to ==  Equals !=  Not equals

Operations performed on booleans (Unary)  Negation (not)  Symbol: !  Meaning: p!p truefalse falsetrue

Operations performed on booleans (Binary)  Conjunction (and)  Symbol: &&  Meaning:  Disjunction (or)  Symbol: ||  Meaning: pq p && q truetruetrue truefalsefalse falsetruefalse falsefalsefalsepq p || q truetruetrue truefalsetrue falsetruetrue falsefalsefalse

Short circuit boolean evaluation  Java supports short circuit boolean evaluation.  This means that if there is a false in a conjunction or a true in a disjunction, Java stops evaluating the expression and produces the result.

Characters  Single letter, digit, or symbol  Character literals are surrounded by ‘’ in code: char aCharacter = ‘x’;  Character class has methods of interest when working with characters.

String  Not a primitive type  Sequence of characters  String literals are surrounded by “” in code String greeting = “Hi there!”;

String concatenation  Can combine strings together using the + operator. String one = “light”; String two = “house; String result = one + two;  result now has the value “lighthouse”

String concatenation  You can also combine a string with a primitive type. int a = 5; int b = 6; int result = a * b; String output = “The result of multiplying 5 and 6 is: ” + result;

String Class  There are many useful methods for String available in the String class.