Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)

Slides:



Advertisements
Similar presentations
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Elementary Programming
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
1 Chapter 2: Elementary Programming Shahriar Hossain.
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.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Chapter 2: Using Data.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
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.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Chapter 2 Variables.
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.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary 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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Topic 2 Elementary Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Chapter 2: Basic Elements of Java
elementary programming
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)

Terminology Primitive Data Type – a category of data. A description of how the computer will treat bits found in memory. Primitive Data Type – a category of data. A description of how the computer will treat bits found in memory. Variable – a named location in memory, treated as a particular data type, whose contents can be changed. Variable – a named location in memory, treated as a particular data type, whose contents can be changed. Constant – a named location in memory, treated as a particular type, whose contents cannot be changed. Constant – a named location in memory, treated as a particular type, whose contents cannot be changed. Declaration – the act of creating a variable or constant and specifying its type. Declaration – the act of creating a variable or constant and specifying its type. Literal – a hard-coded piece of data, part of the statement and not based on a variable or constant declaration. Literal – a hard-coded piece of data, part of the statement and not based on a variable or constant declaration. Operator – a symbol that describe how to manipulate data and variables in memory Operator – a symbol that describe how to manipulate data and variables in memory Expression – a combination of operators, variables, constants and/or literals that produces a resulting piece of data Expression – a combination of operators, variables, constants and/or literals that produces a resulting piece of data Assignment – copying the results of an expression into a variable. Assignment – copying the results of an expression into a variable. Statement – a program instruction telling the CPU what to do. (All statements end with semicolon). Statement – a program instruction telling the CPU what to do. (All statements end with semicolon).

Java Primitive Data types  boolean -- true/false  char -- Unicode character (good for internationalization)  byte -- 8-bit signed integer  short --16-bit signed integer  int bit signed integer  long bit signed integer  float bit floating point number  double bit floating point nbr

Identifiers Identifier = the name of a variable, constant, class, or method Identifier = the name of a variable, constant, class, or method Rules for using identifiers: Rules for using identifiers: An identifier must start with a letter, an underscore, or a dollar sign. An identifier must start with a letter, an underscore, or a dollar sign. An identifier cannot contain operators, such as +, -, and so on. An identifier cannot contain operators, such as +, -, and so on. An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier cannot be true, false, or null. An identifier can be of any length. An identifier can be of any length.

Declaring Variables int x; // declares x to be an // integer variable; // integer variable; double radius; // declares radius to // be a double variable; // be a double variable; char a; // declares a to be a // character variable; // character variable; General format: datatype identifier; Declaring multiple variables of the same type: datatype identifier1, identifier2, identifier3;

Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; General format: VariableIdentifier = expression; These expressions are all just Literals! Note: the = sign is an assignment operator. It is NOT a test for equality.

Declaring and Initializing in One Step int x = 1; int x = 1; double d = 1.4; double d = 1.4; float f = 1.4f; float f = 1.4f; char a = ‘a’; char a = ‘a’; Note: character literals are enclosed in single quotes NOTE: by default floating point literals are assumed to be doubles. If you want to assign a floating point literal to a float variable, you must append the “f” to the end of the number. declaration assignment

Constants final datatype CONSTANTNAME = VALUE; final double PI = ; final int SIZE = 3; The final modifier indicates that the identifier refers to a constant, not a variable. Constants must be initialized when declared.

Numeric Literals int i = 34; int i = 34; long l = ; long l = ; float f = 100.2f; or float f = 100.2F; float f = 100.2f; or float f = 100.2F; double d = 100.2d or double d = 100.2D; double d = 100.2d or double d = 100.2D;

Common Types of Operators Assignment= Assignment= Arithmetic+-*/% Arithmetic+-*/% Comparison== <> = != Comparison== <> = != Logical&&||!^ Logical&&||!^ equals AND OR NOT not equals modulus Exclusive OR

Modulus (remainder) Operator Modulus is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose you know January 1, 2005 is Saturday, you can find that the day for February 1, 2005 is Tuesday using the following expression:

Common Types of Expressions Arithmetic Arithmetic Combine numeric data with arithmetic operators Combine numeric data with arithmetic operators Return a number Return a number Conditional Conditional Combine boolean values with logical operators Combine boolean values with logical operators Boolean values can be derived from comparison operators or boolean data values Boolean values can be derived from comparison operators or boolean data values Returna boolean value Returna boolean value

Arithmetic Expressions x * y x * y 5 / 2 5 / 2 5 % 2 5 % 2 radius*radius* radius*radius*

Sample Statements with Arithmetic Expressions //Compute the first area radius = 1.0; area = radius*radius* ; //Compute the second area radius = 2.0; area = radius*radius*3.14;

Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

Shortcut Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

Increment and Decrement Operators x = 1; x = 1; x++;++x; x++;++x; x--;--x; x--;--x; y = 2 + x++; y = 2 + x++; y = x; y = x; y = 2 + x--; y = 2 + x--; y = x; y = x; Add 1 to x Subtract 1 from x X is incremented after adding to 2 X is incremented before adding to 2 X is decremented after adding to 2 X is decremented before adding to 2

Increment and Decrement Operators, cont.

Integer vs. Floating Point Division When performing operations involving two operands of different types, Java automatically converts the operand of a smaller range to the data type of the larger range. Example: 1/2  this will give 0 because both operands are integer 1.0/2 or 1/2.0  this will give 0.5 because the floating point literal is a double, so the integer literal (long) will be converted to a double; thus floating point division will take place.

Character Data Type char letter = 'A'; char letter = 'A'; char letter = '\u00041'; char letter = '\u00041'; char numChar = '4'; char numChar = '4'; Unicode representation Java uses Unicode instead of ASCII for character data representation

Character Escape Sequences Backspace \b Tab \t Linefeed \n Carriage return \r Backslash \\ Single quote \' Double quote \"

The boolean Data Type boolean lightsOn = true; boolean lightsOn = true; boolean lightsOn = false; boolean lightsOn = false; boolean test = 1==1; boolean test = 1==1; Returns true Returns true boolean test = 1==2; boolean test = 1==2; Returns false Returns false comparison expressions

The + symbol as concatenation operator System.out.println("The area is " + area + " for radius " + radius); String literals are enclosed in double quotes Here, the + is used to concatenate strings together

Boolean Operators Revisited Operator Name ! not && and || or ^ exclusive or

Truth Table for Operator !

Truth Table for Operator &&

Truth Table for Operator ||

Truth Table for Operator ^

Numeric Type Conversion Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * k / 2;

Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int.

Type Casting Implicit casting double d = 3; (type widening) double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) int i = (int)3.9; (Fraction part is truncated) Cast operator

Operator Precedence Casting Casting ++, -- ++, -- *, /, % *, /, % +, - +, -, =>, => ==, !=; ==, !=; && && || || =, +=, -=, *=, /=, %= =, +=, -=, *=, /=, %= first last Parentheses can be used to override normal precedence