SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.

Slides:



Advertisements
Similar presentations
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
String Escape Sequences
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
JAVA PROGRAMMING PART II.
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 (
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
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 Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
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.
CPS120: Introduction to Computer Science
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 Elementary Programming
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
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.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Working with Java.
Chapter 4 Assignment Statement
Java Variables and Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Data Types, Identifiers, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
null, true, and false are also reserved.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Java Programming
Basics of ‘C’.
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Chapter 2: Java Fundamentals
elementary programming
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes

Java (like virtually all programming languages) supports the concept of variables You probably are familiar with the concept of variables from algebra, such as x = 3 Where the variable x in this case represents the integer 3 (initially), but can be reassigned to represent another value at any time. SE Focus Dr. Mark L. Hornick 2

Brainstorm v = 3.14 w = 1/3 u = 1 c = sin(w) + cos(z) y 2 = -1 z = 1 + y SE Focus Dr. Mark L. Hornick 3 Suppose these are algebraic equalities. What type of numbers do the variables represent?

Variables in Java are a similar concept, but different If we want to use a variable x to represent an integer value of 3, we write int x = 3; In Java, we have to declare the datatype (i.e. the kind of value) that the variable will represent. In this case, “int” means that x can represent only integer values [of a specific range]. We say that x is the identifier (i.e. the name) of the variable SE Focus Dr. Mark L. Hornick 4 Note the semicolon; This is required in Java!

In Java, we can arbitrarily make up [nearly] any name we like to use as a variable identifier, provided we follow a few rules: 1. An identifier may consist of a sequence of one or more letters, digits, underscores, and dollar signs ( e.g. my_1st_$ ) 2. The first character in a Java identifier is generally a letter, rarely an underscore or dollar sign, and may not be a digit:  x_1 (ok)  $y2(ok, but unconventional)  _z3(ok, but unconventional)  1w(illegal) 3. Uppercase and lowercase letters are distinguished; the following are treated as 3 different identifiers:  myValue  Myvalue  MyValue 4. No spaces are allowed in an identifier. 5. A Java Reserved Word may not be used as an identifier. SE Focus Dr. Mark L. Hornick 5

6 Java’s reserved words cannot be used as identifiers abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

7 Java supports various “built-in” primitive datatypes There are six numeric datatypes in Java: byte short int long float double byte, short, int, and long represent integer values float and double represent real values

SE-1010 Dr. Mark L. Hornick 8 Numeric values have a limited range of allowed values Why do you think there are four different datatypes just to represent integer values?

SE-1010 Dr. Mark L. Hornick 9 There are generally accepted rules for naming Java variables that will represent primitive datatypes Use a lowercase letter for the first letter of a variable identifier, uppercase for subsequent words Use nouns or noun phrases to represent things that have values x areaCircle sumOfValues Identifiers that begin with uppercase letters (or are all uppercase letters) are used for other Java elements we’ll see later on… Good variable names convey a logical meaning as to the type of data the variables represent.

SE-1010 Dr. Mark L. Hornick 10 Don’t use verbs, adverbs, adjectives or meaningless names for variable identifiers getIt Quickly happy FredFlintstone iDontLikeBeets Names like these don’t convey any meaning to the reader of a program.

In Java, a variable can be also represent many non-primitive datatypes… String s; SE 1011 Dr. Mark L. Hornick 11 This declares a variable s of datatype String. String is another predefined, non-primitive Java datatype. What kind of values can be held by this type of variable???

SE-1010 Dr. Mark L. Hornick 12 The String datatype is a built-in class (from the java.lang package) that represents a sequence of characters String s1 = “a”; String s2 =“SE-1011”; String s3 =“123”; String s4 =“This is a 6 word string.”; String s5 = “”; There is no fundamental restriction on the maximum length of a Java String How does this differ from a int that represents 123? This represents an empty String String sequences are surrounded by double-quotes

SE-1010 Dr. Mark L. Hornick 13 The char primitive datatype represents only a single character char c1 =‘1’; char c2 =‘a’; char c3 =‘?’; char c4 =‘B’; char c5 =‘%’; How does this differ from a int that represents 1? Characters are surrounded by single-quotes

SE-1010 Dr. Mark L. Hornick 14 char c1 = ‘\t’; // tab char c2 =‘\n’; // newline char c3 =‘\r’; // carriage return char c4 =‘\”’; // double-quote char c5 =‘\’’; // single quote char c5 =‘\\’; // backslash Use escape sequences to represent special characters Escape sequences are two-characters consisting of the backslash followed by an escape code

SE-1010 Dr. Mark L. Hornick 15 String hello = “Hello \n World”; System.out.println( hello ); Escape sequences can be used in Strings too NOTE: PowerPoint uses distinct single- and double-quote characters that do not appear on your keyboard!

SE-1010 Dr. Mark L. Hornick 16 The boolean datatype represents only two logical values: true and false boolean isOff = true; boolean isCold = false; The boolean datatype is named after George Boole, an English mathematician/logician who developed Boolean algebra true and false are both Java reserved words

Repeat: You must declare a variable’s datatype in Java before you can use that variable int x; String name; char c1; boolean isDone; float someValue; double areaCircle; SE-1010 Dr. Mark L. Hornick 17 x, name, c1, isDone, someValue, and areaCircle are all identifiers that represent only specific types of data. So, you cannot assign a numeric value to name, nor can you assign a character string value to x.

Variables can be initialized at the same time as they are declared int x = 0; String name = “Mark”; char c1 = ‘a’; boolean isDone= false; float someValue = 3.14F; double areaCircle = 2.6; SE-1010 Dr. Mark L. Hornick 18 The value of a variable can be reassigned at any time, even when it’s initialized.

A constant is a value that cannot be changed after initialization final int ZERO = 0; final String YES = “Yeah”; final char QUESTION = ‘?’; final boolean OFF = false; final float E = 2.17F; final double PI= ; SE-1010 Dr. Mark L. Hornick 19 The value of a constant must be assigned at initialization, and cannot be reassigned afterwards.

SE-1010 Dr. Mark L. Hornick 20 Arithmetic operators in Java are used on numeric datatypes (byte, short, int, long, float, and double)

Examples of arithmetic expressions and assignment using the binary arithmetic operators *, +, -, / and % int x = 1; int y = x+1; x = x+3-y; int z = x*y*2; z = (x+y)*2+4; // note use of () int w = 4/2+5-3; w = 5 % 2; SE-1010 Dr. Mark L. Hornick 21

Increment and Decrement arithmetic operators The ++ and -- unary operators are used to increment or decrement the value of a variable by 1. x++; // increments x by 1 ++x; // ditto --x; // decrements x by 1 x--; // ditto SE-1010 Dr. Mark L. Hornick 22

SE-1010 Dr. Mark L. Hornick 23 Arithmetic Expressions Precedence rules for arithmetic operators and parentheses

Integer Overflow The range of an int is to So what happens when you add 1 to an int holding a value of 32767?? SE-1010 Dr. Mark L. Hornick 24

Integer Division Integers variables can only hold whole values, so what happens if you do the following? int x = 2; int y = 3; int z = x/y; // what is the result? SE-1010 Dr. Mark L. Hornick 25

Variable values can be assigned (and re-assigned) as the result of various operations int radius = 3; float areaCircle = radius * ; radius = 4; areaCircle = radius * ; SE-1010 Dr. Mark L. Hornick 26 Declaration and assignment of radius Declaration and assignment of areaCircle Reassignment of previously declared radius Reassignment of previously-declared areaCircle

Q: Would it make sense to declare areaCircle to be an int? int radius = 3; int areaCircle = radius * ; radius = 4; areaCircle = radius * ; SE-1010 Dr. Mark L. Hornick 27 Declaration and assignment of radius Declaration and assignment of areaCircle Reassignment of previously declared radius Reassignment of previously-declared areaCircle

SE-1010 Dr. Mark L. Hornick 28 Details: Primitive variables vs. non-primitive (object) variables The only difference between a variable for a primitive and a variable for objects is the contents in the memory locations. For primitives, a variable contains the numerical value itself. For objects (like Strings), a variable contains an address where the object is stored. 10 str1 x String object

SE-1010 Dr. Mark L. Hornick 29 Can we use arithmetic operators (like + or -) on object variables? String s1 = “se”; String s2 = “1011”; String output = s1 + s2; // does this work??

SE-1010 Dr. Mark L. Hornick 30 The only operator defined by Java for a String variable is “+” The “+” operator, when used to “add” Strings, actually causes the Strings to be concatenated String s1 = “se”; String s2 = “1010”; String output = s1 + s2; // output is “se1010” The only operator defined by Java for an object variable is “+”, and it only works on String objects