CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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,
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
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.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
Chap. 2. Types, Operators, and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Expressions and Assignment
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Presentation transcript:

CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2

CSM-Java Programming-I Lesson-1 Objectives Review of last class. Identifiers and primitive data types. Operators.

CSM-Java Programming-I Lesson-1 Identifiers Java identifiers are used for names of variables, constants and labels. They must start with a letter, a currency symbol or underscore (_) followed by letters, digits or both. Java language keywords cannot be used as identifiers. Eg: int, private, void, new, for…

CSM-Java Programming-I Lesson-1 Primitive Types boolen either true or false char 16-bits byte 8-bits short 16-bits int 32-bits long 64-bits float 32-bits double 64-bits

CSM-Java Programming-I Lesson-1 Literals A literal is a constant value that may be assigned to a primitive type. Boolean – true and false Integer – Integer constants are strings of octal, decimal or hexadecimal digits. E.g.: 29-decimal 035-octal 0x1D, 0X1d-hexadecimal Suffix a L to indicate a long (64 bit) literal)

CSM-Java Programming-I Lesson-1 Literals Floating-Point – Floating point literals are expressed as decimal numbers. E.g.: 18., 1.8e1,.18E2 all denote the same number. A floating-point literal with no F or D suffix defaults to double type. Character – Character literals appear between single quotes. E.g.: ‘Q’.

CSM-Java Programming-I Lesson-1 Literals Character – Certain special characters can be represented by an escape sequence. E.g.: \n for newline, \t for tab, \b for backspace. Strings – String literals appear between double quotes. E.g.: “Hi”. A string literal references an object of type String.

CSM-Java Programming-I Lesson-1 Strings Strings – String is a sequence of characters. Strings are objects of the String class. The number of characters in a string is called the length of the string. Eg: int n = message.length(); Strings can be concatenated, by using the + operator. String name = “dave”; String message = “Hello, “ + name; Whenever one of the arguments of the + operator is a string, the other argument is converted to a string.

CSM-Java Programming-I Lesson-1 Strings If a string contains the digits of a number, use the Integer.parseInt or Double.parseDouble method to obtain the number value. Use the substring() method to extract a part of a string. Eg: String greeting = “Hello World!”; String sub = greeting.substring(0,4); // sub = “Hell” String positions are counted starting with 0.

CSM-Java Programming-I Lesson-1 final Keyword Any number that is not completely self- explanatory should be declared as a named constant. A final variable is a constant. Once its value has been set, it cannot be changed. Use named constants to make your programs easier to read and maintain. Eg: final int DAYS_PER_YEAR = 365;

CSM-Java Programming-I Lesson-1 static Keyword Java numbers are not objects, so you cannot invoke a method on a number. Eg: To calculate sqrt or power. A static method does not operate on an object. Eg: Math.sqrt(x); Math.round(y);

CSM-Java Programming-I Lesson-1 Type Conversion When an assignment is made to a variable, the types of either side of the assignment must be compatible. Eg 1: double total = “a lot”; //Error Eg 2: int dollars = 2; double total = dollars; // Correct – This is called implicit conversion. Eg 3: double dollars = 2; int total = dollars; // Error

CSM-Java Programming-I Lesson-1 Type Conversion Eg 3: double dollars = 2; int total = dollars; // Error Casting: You must convert the floating-point value to integer with a cast. (type) expression; int total = (int) dollars;

CSM-Java Programming-I Lesson-1 Type Conversion Loss of Information Eg 1: double total = 13.75; int pennies = (int)(total *100); Use the Math.round() method to round a floating-point number to the nearest integer to avoid the loss of information.

CSM-Java Programming-I Lesson-1 Operators Arithmetic Operators +, -, *, /, % (remainder) If both the arguments of the / operator are integers, the result is an integer and the remainder is discarded. Eg: 7/4 = 1; 3 is discarded.

CSM-Java Programming-I Lesson-1 Operators Arithmetic Operators +, -, *, /, % (remainder) Eg: 7.0/4.0, 7/4.0, 7.0/4 all yield 1.75 The % operator computes the remainder of a division. Eg: 7 % 4 = 3

CSM-Java Programming-I Lesson-1 Common Errors Integer Division Eg: int s1 = 5; int s2 = 6; double avg = (s1 + s2) / 3; // Error double avg = (s1 + s2) / 3.0; //Correct Unbalanced Parentheses 1.5 * ((- (b – Math.sqrt(b*b – 4*a*c)) / (2*a))

CSM-Java Programming-I Lesson-1 Operators Increment and decrement Operators ++, -- i++ is equivalent to i= i+1 Increment and decrement operators can be either prefix of postfix operators (they can appear either before or after what they operate on).

CSM-Java Programming-I Lesson-1 Increment and decrement Operators If increment or decrement operators appear before (prefix), the operation is applied before the value of the expression is returned. If increment or decrement operators appear after (postfix), the operation is applied after the value of the expression is used.

CSM-Java Programming-I Lesson-1 Example: Increment and decrement Operators public class Ex1 { public static void main(String args[]) { int x = 10; System.out.println(++x); System.out.println(x++); System.out.println(x); }

CSM-Java Programming-I Lesson-1 Relational and Equality Operators > greater than >= greater than or equal to < less than <=less than or equal to == equal to – equality op. != not equal to – equality op. The unary operator ! Inverts a boolean; !true is same as false Equality operators opearte only on boolean values

CSM-Java Programming-I Lesson-1 Logical Operators && - conditional AND || - conditional OR

CSM-Java Programming-I Lesson-1 Example1: Logical Operators if (x && y) { if (y || z) { Do something } The inner if is executed only if both x and y are true. If x is false, y is not evaluated. The body of inner if is excecuted if either y or z is true. If y is true, z is not evaluated.

CSM-Java Programming-I Lesson-1 Ternary Operator x = (a ? b : c) is equivalent to if (a) x = b; else x = c;

CSM-Java Programming-I Lesson-1 Assignment Operator a +=10; Same as a = a+10; Same for all arithmetic operators

CSM-Java Programming-I Lesson-1 Operator Precedence The order of precedence of operators from highest to lowest is: postfix [],., (params), expr++, expr— Unary ++expr, –expr, +expr, –expr ! Multiplicative* / % Additive+ - Relational, = Equality==, != Logical AND&& Logical OR|| Ternary?: Assignment=, +=, -=, *=, /=, %=

CSM-Java Programming-I Lesson-1 Operator Precedence Operators with the same precedence appear on the same line in the table. Precedence can be overridden using parentheses Operands to the operators will be evaluated left- to-right, except for &&, ||, ?:. Eg: x+y+z. First evaluates x, then y, adds the values together, evaluates z and adds that to the previous result.