MIT AITI 2003 - Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables.

Slides:



Advertisements
Similar presentations
What Is Java? The name Java is a trademark of Sun Microsystems
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CSci 1130 Intro to Programming in Java
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
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,
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
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:
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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Lecture #5 Introduction to C++
Primitive Variables.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Primitive Variables.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
CPS120: Introduction to Computer Science Variables and Constants.
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 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.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Identifiers, Variables and Primitive Data Types
Chapter 2: Introduction to C++
IDENTIFIERS CSC 111.
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Variables and Constants
Presentation transcript:

MIT AITI Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables and Data Types »Operators »Control Structures »Arrays and Vectors

Variables and Primitive Data Types MIT AITI

Variables

What is a Variable? Variables are a place where information can be stored while a program is running Their value can be changed at any point in the program In order to create a variable, you must give it a name and identify what type of information it will store

Creating Variables You must create the variable by declaring its name and the type of information it will store The type is listed first, followed by the name For example: int highScore ; type name

More examples: String userName ; booleangameOver ; bytesmallerValue ; double testGrade ; You’ll learn more about the types used in these examples in the following lectures

Naming Variables The name that you choose for a variable is called an identifier An identifier can be of any length, but must start with: - a letter (a – z) - an underscore ( _ ) - or a dollar sign ($) The rest of the identifier can include any character except those used as operators in java such as +, -, *. You’ll learn more about operators in following lectures

In addition, there are certain keywords reserved in the Java language which can never be used as identifiers. Here is a table of the keywords: abstractassertbooleanbreakbyte casecatchcharclassconst continuedefaultdodoubleelse extendsfinalfinallyfloatfor gotoifimplementsimportinstanceof intinterfacelongnativenew packageprivateprotectedpublicreturn shortstaticstrictfpsuperswitch synchronizedthisthrowthrowstransient tryvoidviolatewhile

Java is a case sensitive language – the capitalization of letters must be consistent. A rose is not a Rose is not a ROSE It is good practice to select variable names that give a good indication of the sort of data they hold –For example, if you want to record the size of a hat, hatSize is a good choice for a name whereas qqq would be a bad choice

When naming a variable, the following convention is commonly used: –The first letter of a variable name is lowercase –Each successive word in the variable name begins with a capital letter –All other letters are lowercase Here are some examples: pageCount loadFile anyString threeWordVariable

POP QUIZ Which of the following are valid variable names? 1)$amount 2)6tally 3)my*Name 4)salary 5)_score 6)first Name 7)total#

Statements A statement is a simple command written in a programming language that causes something to happen. All statements in Java are separated by semicolons ; Some statements produce a value and are known as expressions

Variables and Statements You use statements to create variables and assign values to them One way is to declare a variable and then assign a value to it with two statements int e; //here we are declaring a variable e = 5; //here we are assigning a value to a declared variable Another way is to write a single definition statement int e = 5; //here we are declaring AND assigning

Java is a Strongly-Typed Language This means that it is a language in which each type of data is predefined as part of the programming language ALL variables defined in a program must be declared with one of the data types. Certain operations may be allowable only with certain data types. The language compiler enforces the data typing.

Primitive Data Types

There are eight built-in (primitive) data types in the Java language – 4 integer types ( byte, short, int, long ) – 2 floating point types ( float, double ) – Boolean ( boolean ) – Character ( char )

Integer Data Types There are four data types that can be used to store integers. The one you choose to use depends on the size of the number that we want to store. Data TypeDescription byte Variables of this kind can have a value from: -128 to +127 and occupy 8 bits in memory short Variables of this kind can have a value from: to and occupy 16 bits in memory int Variables of this kind can have a value from: to and occupy 32 bits in memory long Variables of this kind can have a value from: to and occupy 64 bits in memory

Here are some examples of when you would want to use integer types: –byte smallValue; smallValue = -55; –short pageCount = 1250; –long bigValue = L;

Note: In the last example we named a long integer type and the value had an L at the end: L That is because: if the number had been small enough to be counted as an int type instead, then the program would have automatically considered it an int instead of a long By adding an L to the end of the value, the program is “forced” to consider the value to be of a type long even if it was small enough to be an int

Floating Point Data Types There are two data types that can be used to store decimal values. The one you choose to use depends on the size of the number that we want to store. Data TypeDescription float Variables of this kind can have a value from: 1.4e(-45) to 3.4e(+38) double Variables of this kind can have a value from: 4.9e(-324) to 1.7e(+308)

Here are some examples of when you would want to use floating point types: –doubleg = 7.7e100 ; –doubletinyNumber = 5.82e-203; –floatcostOfBook = 49.99F; Note: In the last example we added an F to the end of the value. Without the F, it would have automatically been considered a double instead.

Boolean Data Type There is a data type that can be used in situations where there are two options, either true or false. These are called booleans. For example: –boolean monsterHungry = true; –boolean fileOpen = false;

Character Data Types There is a data type that can be used to store information for single characters such as letters, numbers, punctuation, and other symbols. For example: –char firstLetterOfName = ‘e’ ; –char myQuestion = ‘?’ ; Note that you need to use singular quotation marks when assigning char data types. Otherwise the program might look for another variable name instead of a letter, or assign an int type instead of a number

Introduction to Strings To store an entire string of characters as information, Java makes use of a “string”. Strings consist of a series of characters inside double quotation marks, as in the following statements: –String coAuthor = “Yaron Binur” ; –String password = “swordfish786” ; You will learn more about strings and their uses in following lectures

POP QUIZ What data types would you use to store the following types of information?: 1)The number of students in the room 2)Location of a point on screen 3)Speed of light 4)Open/Closed status of a file 5)The first letter of your name 6)$237.66