Chapter 2: Java Fundamentals

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

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,
©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[]
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
String Escape Sequences
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
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.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Copyright Curt Hill Variables What are they? Why do we need them?
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[]
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CPS120: Introduction to Computer Science Variables and Constants.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
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.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Topic 2 Elementary Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java Variables and Types
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Fundamentals 2.
C++ Data Types Data Type
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Primitive Types and Expressions
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2: Java Fundamentals Variables, Constants and Built-in Data Types

Objectives Discovering what is a variable Discovering what is a data type Learning about the basic data types Constants and variables identifiers Get acquainted with how to select proper types for numerical data. Write arithmetic expressions in Java.

Programs and Data Keyboard Screen Processing input data output data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory. A space memory has three characteristics Identifier Data Type State

State of the Space Memory The state of the space memory is the current value (data) stored in the space memory. The state of the space memory: May be changed. In this case the space memory is called variable. Cannot be changed. In this case the space memory is called constant. 

Space Memory Identifier Identifier is a sequence of characters that denotes the name of the space memory to be used. This name is unique within a program. Identifier Rules It cannot begin with a digit (0 – 9). It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed. 

Identifier Conventions in Java Constants: All uppercase, separating words within a multiword identifier with the underscore symbol, _. Variables All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word.

Identifiers are Case-Sensitive Identifiers in Java are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

Data Type The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program.

Java built-in Data Types

Primitive Data Types Type Size (bits) Range Description boolean true, false Stores a value that is either true or false. char 16 0 to 65535 Stores a single 16-bit Unicode character. byte 8 -128 to +127 Stores an integer. short -32768 to +32767 int 32 bits -2,147,483,648 to +2,147,483,647 long 64 bits -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 float accurate to 8 significant digits Stores a single-precision floating point number. double accurate to 16 significant digits Stores a double-precision

Variable/Constant Declaration When the declaration is made, memory space is allocated to store the values of the declared variable or constant. The declaration of a variable means allocating a space memory which state (value) may change. The declaration of a constant means allocating a space memory which state (value) cannot change.

Constant Declaration final dataType constIdentifier = literal | expression; final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; The reserved word final is used to declare constants. These are constants, also called named constant. These are called literals. This is called expression.

Variable Declaration A variable may be declared: With initial value. Without initial value. Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3; Variable declaration without initial value; dataType variableIdentifier; double avg; int i;

Variables – Cont. Float The default type of floating point numbers is double . The declaration : float rate = 15.5f ; without the f , the compiler will generate an error

Variables – Cont. Char When using the char data type, you enclose each character represented within single quotations marks. Ex: char c = ‘A’ char x = ‘&’ char space = ‘ ‘ Each character is represented by a value (‘A’ is represented by the value 65)

Example // import section: Empty public class Example { // main method public static void main( String args[] ){ // Declaration section: int x=5; double y=7.5; // Input section: Empty // Processing section: Empty // Output section System.out.println(“ the value of x=” +x); System.out.println(“ the value of y=” +y); } // end main } // end class