1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
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.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
02 Variables1November Variables CE : Fundamental Programming Techniques.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Primitive Variables.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a 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.
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.
Chapter 2 Variables.
Chapter 2 Basic Computation
Multiple variables can be created in one declaration
Variables and Primative Types
Assignment and Arithmetic expressions
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Fundamentals 2.
Introduction to Java, and DrJava
Expressions and Assignment
elementary programming
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations and Arithmetic Expressions l Assignment Statement l Associativity of Operators l Preview: More Operators and Console Input

2 Format of a Java Application l A Java application consists of one or more classes A Java class consists of one ore more methods (functions) one of which must be the main method l A method is a collection of instructions (statements) describing how to carry out a particular task.. The following is a simple Java application to print the the message "Hello World!“ on the screen. public class Hello { public static void main ( String[] args ) { System.out.println("Hello World!"); }

3 Format of a Java Application – Contd. The Statement: public class Hello starts a new class named Hello public means the class is accessible to all other objects, we shall encounter private later. The class containing the main method must be declared as public l It is up to you to give names to your classes provided you follow the following simple rules and conventions. A class name »must be one word (no spaces) »must start with a letter »must not contain special characters (+, &, etc) »should start with capital letter »If a class name contains more than one English words, each word should start with a capital letter (e.g. MyFirstClass ) »It should reflect the function of the class A Java file must be saved with the name of the class it contains with the extension.java If it contains more than one class, the name of the class containing the main method must be used.

4 Format of a Java Application – Contd. The Statement: public static void main starts the main method. Again the main method must always be declared as public – most methods are private. The main method must always be declared as static, to be explained later - most methods are not static The parameter (String[],args) is required for the main method. It is used to pass command line arguments l Naming a method follow the same rule as naming a class, however, by convention, we shall always start a method with a small letter to differentiate it with a class. If a method contains more than one English word, the subsequent words should start with a capital letter. E.g. myFirstMethod The Statement: System.out.println(“Hello world”); prints a line of text “Hello world” on the standard output (the screen)

5 Format of a Java Application – Contd. The standard output is represented by an object called out contained in the class System and referred to as System.out println is a method of the System.out object and can be used to print strings and numbers. e.g. System.out.println(3+4); displays 7 l When ever you use a method in Java, you must specify three items: »The object containing the method (e.g. System.out ) »The name of the method (e.g. println ) »A pair of parenthesis which may contain any other information needed by the method (e.g. “Hello world”) The println method prints the contents of its argument and move to the next line. E.g. the following prints two lines. System.out.println(“ICS Department”); Syetem.out.println(“KFUPM”); Another method of the System.out object is print. This prints its argument but does not move to the next line. The following statements prints one line. System.out.print(“ICS Department”); Syetem.out.print(“KFUPM”);

6 Primitive Data Types Java has eight primitive data types as described below. TypeSize/Format Range (whole numbers) byte 8-bit two's complement-128 to 127 short 16-bit two's complement-32,768 to 32,767 int 32-bit two's complementabout –2 billion to 2billion long 64-bit two's complementabout –10E18 to +10E18 (real numbers) float 32-bit IEEE E38 to +3.4E38 double 64-bit IEEE E308 to 1.7E308 (other types) char 16-bit Unicode characterA single character boolean true or falseA boolean value (true or false) Apart from these, any other information is represented in Java as an Object.

7 Variables Declaration You can declare a variable to hold a data value of any of the primitive types. int counter; int numStudents = 583; long longValue; long numberOfAtoms = L; float gpa; float batchAverage = 0.406F; double e; doublepi = 0.314; char gender; char grade = ‘B’; boolean safe; boolean isEmpty = true; public class Example1 { public static void main ( String[] args ) { int payAmount = 123; System.out.println("The variable contains: " + payAmount ); }

8 Arithmetic Operators For whole/Real numbers: OperatorUseDescription + op1 + op2 Adds op1 and op2 - op1 - op2 Subtracts op2 from op1 * op1 * op2 Multiplies op1 by op2 / op1 / op2 Divides op1 by op2 % op1 % op2 Computes the remainder of dividing op1 by op2 public class Example2 { public static void main ( String[] args ) { int hoursWorked = 40; double payRate = 10.0; System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : " + (hoursWorked * payRate) ); }

9 Arithmetic Operation Modes of operation : If operand1 and operand2 are of same data type, then the resultant value of the operation will be of that same data type. If operand1 and operand2 are of different data type like real and integer, then the resultant value of the operation will be of real data type. The real data type may be float or double. e.g. 1/2 gives 0 1.0/2 gives /2.0 gives 0.5 Operand1Operand2Result Real Whole Real WholeReal

10 Arithmetic Expressions l Definition: An expression is a sequence of variables, constants, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. l In Java, Arithmetic expressions are evaluated very similar to the way they are evaluated in algebra. Operator Meaning Precedence - unary minus highest + unary plus highest * multiplication middle / division middle % remainder middle + addition low - subtraction low e.g / 4  / 2 – 9  -4

11 Associativity of Operators l When operators of equal precedence appear in the same expression, associativity rule governs, which is to be evaluated first. l Left associativity rule In Java, all binary operators except for the assignment operators are evaluated in left to right order. 2 * 7 * * l Right associativity rule Assignment operators are evaluated right to left. int result; result = ((a + b) / 2) % 3;

12 Evaluating Expressions l Arithmetic Expression int result, a = 3, b = 5; result = ((a + b) / 2) % 3; ((a + b) / 2) % 3 (8 / 2) % 3 4 % 3 1

13 Assignment Statement variable = expression; l The value of constants / variables / expression in the right side of the = operator, is assigned to the variable in the left side of the = operator. l Examples: a = 5; b = a; c = a + b; l The parameter in the left hand side of the = operator should be a variable. l It can not be a constant or an expression. l Example of an invalid assignment statement : a + b = c ;

14 Assignment Statement l Java allows multiple assignment. int start, end; int width = 100, height = 45, length = 12; start = end = 0; l Note that whole integers appearing in the source code are taken to be ‘int’. So, one might wish to flag them when assigning to non-ints: float maxGrade = 100f ; // now holds ‘100.0’ double temp = 583d ; // holds double precision float temp = 5.5 ; // ERROR! // Java treats 5.5 as a double to // retain maximal accuracy l Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L): float maxGrade = 100F ; // now holds ‘100.0’ long x = 583l ; // holds 583, but looks like 5,381 long y = 583L ; // This looks much better!