Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.

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

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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Hello, world! Dissect HelloWorld.java Compile it Run it.
Java Basics Kris Secor Mobile Application Development.
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.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe.
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Mixing integer and floating point numbers in an arithmetic operation.
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
Java for Android is specific
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Java Tutotrial for [NLP-AI] 2
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Building Java Programs
Building Java Programs
Presentation transcript:

Primitive data types Lecture 03

Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public static void main(String args[]) { // Following code shows the multiplication table of 5. System.out.println("5 x 1 = 5 "); System.out.println("5 x 2 = 10"); System.out.println("5 x 3 = 15 "); System.out.println("5 x 4 = 20"); System.out.println("5 x 5 = 25"); System.out.println("5 x 6 = 30 "); System.out.println("5 x 7 = 35"); System.out.println("5 x 8 = 40 "); System.out.println("5 x 9 = 45"); System.out.println("5 x 10 = 50"); }

Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments Arithmetic operators

Example 123 (int) 1.5 (double) “HelloWorld” (String) `H’ (Char) ….

Basic Definitions Variables - A name that refers to a value. Assignment Statement - Associate a value with a variable. Constant: – 456—a literal numerical constant System.out.println(456); // Java – “A Literal String Constant” System.out.println(“My First Java”); // Java

There are eight primitive data types Boolean, byte, char, int, double, float, long, short In bytes, how long is the short data type? The int data type, the long data type? In bytes, how long is the float data type? The double data type? How long is the char data type?

Primitives Data types and Ranges PRIMITIVESIZE IN BITSRANGE int32bits (4 bytes)-2 31 to ,147,483,648 to +2,147,483,647 long64bits -- 8 bytes-2 63 to float32bits- -4 bytes-+( e-45 to e+38} double64+-( e-324 to e+308) char16bitsOne character string16bits per char Not applicable boolean8bits--1-byteTrue or false

Examples TypeSet of valuesSample literal vlues intinterges99 (-12) doubleFloating-point numbers3.14 (-1.5) booleanBoolean valuesTrue or false charcharacters‘a’ ‘1’ ‘£’ ‘%’ ‘\n’ StringSequence of characters“AC” ”Hello” ” 1.5”

Variable declaration declarationVariable nameVariable type int x;xinteger double d;ddouble char c;ccharacter String s;sstring Float f;ffloat

The assignment operator = declarationVariable name int x; x = 36; Declare the variable x as an integer Sets x to constant 36 at execution time int x = 36; Sets x = to the constant 36 at compile time Initializes x to 36 at the time memory is set aside for it String y; y = “Hellow”; Declare the variable x as an integer Sets y to constant “Hello” at execution time String y = “Hello”; Sets y = to the constant “Hello” at compile time Initializes x to “Hello” at the time memory is set aside for it

Initialisation If no value is assigned prior to use, then the compiler will give an error Java sets primitive variables to zero or false in the case of a boolean variable All object references are initially set to null An array of anything is an object – Set to null on declaration – Elements to zero false or null on creation

Declaration Examples int index = 1.2; // compiler error boolean retOk = 1;// compiler error double fiveFourths = 5 / 4; // no error! float ratio = 5.8f;// correct double fiveFourths = 5.0 / 4.0;// correct 1.2f is a float value accurate to 7 decimal places. 1.2 is a double value accurate to 15 decimal places.

int a, b, c ; b =1; a=b; c =a; System.out.print(“c= “ + c); What is the value of a, b & c Declaration (Cont)

Example Int1.java // uninitialised data // this program will declare and print a number Public class int3 { public static void main(String[] arguments) { int weight; System.out.println("your weight is " + weight); } //end of program

Example Int2.java // this program will declare and print a number class int2 { public static void main(String[] arguments) { int weight = 68; System.out.println("your weight is " + weight); } //end of program

Example Int5.java // uninitialised data // this program will declare and print a number class int5 { public static void main(String[] arguments) { int weight; weight = 65 ; //65 = weight ; System.out.println("your weight is " + weight); } //end of program

Example String2.java // this program will declare and print a string class string2 { public static void main(String[] arguments) { String name = "Lahcen"; String x = "my name is "; System.out.println( x + name ); //print string x and then string name } //end of program

Basic Mathematical Operators * / % + - are the mathematical operators * / % have a higher precedence than + or - double myVal = a + b % d – c * d / b; Is the same as: double myVal = (a + (b % d)) – ((c * d) / b);

Basic arithmetic Operators OperatorMeaningExample +Addition2+3 = 5 *Multiplication2*3=6 -subtraction3-2=1 /division4/2=2 %mod5 % 2 = 1, 6 % 2= 0

Precedence Rules 1.Evaluate all sub-expressions in parentheses 2.Evaluate nested parentheses from the inside out 3.In the absence of parentheses or within parentheses a.Evaluate *, /, or % before + or – b.Evaluate sequences of *, /, and % operators from left to right c.Evaluate sequences of + and – operators from left to right

Built-in Math Functions in Java

Example: Calculate square root class SquareRootExp{ public static void main(String args[]) { int x = 25; int result = Math.sqrt(x); System.out.println(“Square root of " + x + “ is " + result); }

Exercise Assume that the length and width of the rectangle is 10 and 5. Write a program which compute area of the rectangle and display result on the screen.

Summary Different data type Declarations Arithmetic operators Parse string to integer. Boolean operators Assignments