Chap 2. Identifiers, Keywords, and Types

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

Chapter 1: Computer Systems
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand 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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
The Java Programming Language
Outline Java program structure Basic program elements
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.
String Escape Sequences
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Program Elements -- Introduction
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
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.
1 Program Elements -- Introduction zWe can now examine the core elements of programming zLecture focuses on: ydata types yvariable declaration and use.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Java: Chapter 1 Computer Systems Computer Programming II.
Java Language and SW Dev’t
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Chapter 2: Java Fundamentals
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Lecture 2 Software Concepts Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Copyright Curt Hill Variables What are they? Why do we need them?
White Space Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
© 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.
Working with Java.
Chapter 4 Assignment Statement
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Chapter 2: Java Fundamentals
Units with – James tedder
Units with – James tedder
Focus of the Course Object-Oriented Software Development
Chapter 2 Primitive Data Types and Operations
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Chap 2. Identifiers, Keywords, and Types Objectives Comment Identifier Keyword Basic meaning: Class, object, member variable, reference Create a Java class definition Declare variables of class type Constructor Access member variables of an object using dot notation reference variable

Comments Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe processing steps Java comments can take two forms: // comment runs to the end of the line /* comment runs to terminating symbol, even across line breaks */

Semicolon, Blocks, White Space In java, statements are terminated with a semicolon(;) A block is marked by enclosing it in brackets({ }) Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space is ignored

Identifiers Identifiers are the words a programmer uses in a program Most identifiers have no predefined meaning except as specified by the programmer An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign They cannot begin with a digit Java is case sensitive, therefore Total and total are different identifiers

Identifier examples Valid identifier Invalid identifier identifier userName User_name _sys_var1 $change Invalid identifier 3identifier

Reserved Words(Keywords) Some identifiers, called reserved words, have specific meanings in Java and cannot be used in other ways abstract boolean break byte byvalue case cast catch char class const continue default do double else extends false final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while

Primitive Data Types A data type is defined by a set of values and the operators you can perform on them Each value stored in memory is associated with a particular data type The Java language has several predefined types, called primitive data types The following reserved words represent eight different primitive types: byte, short, int, long, float, double, boolean, char

Integers There are four separate integer primitive data types They differ by the amount of memory used to store them Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 Max Value 127 32,767 2,147,483,647 > 9 x 1018

Characters The ASCII character set is still the basis for many other programming languages ASCII is a subset of Unicode, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ...

Floating Point There are two floating point types: The float type stores 7 significant digits The double type stores 15 significant digits Approximate Min Value -3.4 x 1038 -1.7 x 10308 Approximate Max Value 3.4 x 1038 1.7 x 10308 Type float double Storage 32 bits 64 bits

Characters A char value stores a single character from the Unicode character set A character set is an ordered list of characters The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages

Boolean A boolean value represents a true or false condition They can also be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type

Wrappers For each primitive data type there is a corresponding wrapper class. For example: Wrapper classes are useful in situations where you need an object instead of a primitive type They also contain some useful methods Primitive Type int double char boolean Wrapper Class Integer Double Character Boolean

"To thine own self be true." Literals A literal is an explicit data value used in a program Integer literals: 25 69 -4288 Floating point literals: 3.14159 42.075 -0.5 String literals: "The result is: " "To thine own self be true."

Variables A variable is an identifier that represents a location in memory that holds a particular type of data Variables must be declared before they can be used The syntax of a variable declaration is: data-type variable-name; For example: int total;

Variables Multiple variables can be declared on the same line: int total, count, sum; Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; float unit_price = 57.25;

Assignment Statements An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable The expression can be a single value or a more complicated calculation

Constants A constant is similar to a variable except that they keep the same value throughout their existence They are specified using the reserved word final in the declaration For example: final double PI = 3.14159; final int STUDENTS = 25;

Aggregate Data Types. public class Date {. int day;. int month; Aggregate Data Types public class Date { int day; int month; int year; } Date mybirth, yourbirth; mybirth.day=26; mybirth.month=11; yourbirth.year=1960;

Java Coding Convention Class name should be noun(첫문자는 대문자) Interface name should be capitalized like class names. Method names should be verb(첫문자는 소문자) Variable: 소문자로 시작, meaningful Use comments to explain code segments Place only a single statement on any line, use 4-space indentation, to make your code readable

Objects An object has: For example, a particular bank account state - descriptive characteristics behaviors - what it can do (or be done to it) For example, a particular bank account has an account number has a current balance can be deposited into can be withdrawn from

Classes A class is a blueprint of an object It is the model or pattern from which objects are created A class defines the methods and types of data associated with an object Creating an object from a class is called instantiation; an object is an instance of a particular class For example, the Date class could describe many bank accounts, but mybirth is a particular bank account with a particular balance

Creating Objects The new operator creates an object from a class: Date mybirth = new Date(); This declaration asserts that mybirth is a variable that refers to an object created from the Date class It is initialized to the object created by the new operator The newly created object is set up by a call to a constructor of the class