Primitive Variables.

Slides:



Advertisements
Similar presentations
Variables and Operators
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
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.
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Primitive Types CSE 115 Spring 2006 April 3 &
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
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.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Chapter 2: Using Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Primitive Variables.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 2. Program Construction in Java. 01 Java basics.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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 and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fundamentals 2.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Object Oriented Programming
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Java, and DrJava part 1
Fundamentals 2.
Chapter 2: Java Fundamentals
Expressions and Assignment
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

Primitive Variables

What’s a Variable? A quantity or function that may assume any given value or set of values (source – Dictionary.com) What types of values have we assigned to variables in mathematics and science? Integers and Real Numbers Temperature and Weight Names and Places

What’s a Primitive? Being the first or earliest of the kind or in existence Given just one group of binary digits, what types of things could we come up with? A Binary Value (True/False) Numbers (1, 4, 21) Characters (A, k, &)

JAVA’s Primitive Variables Binary Value (boolean) Numbers Integer (byte, short, int, long) Real Numbers (float, double) Characters (char)

Why so many Integer names? Each one consists of a different number of bits. For example, a byte consists of 8 bits and holds a value from –128 to 127. Calculate 28 = 128 + 127 + 1 =

The other Integer Variables short Occupies 16 bits or 2 bytes int Occupies 32 bits or 4 bytes long Occupies 64 bits or 8 bytes Can you guess the range of values each one stores?

Why so many? It’s a tradeoff The greater number of bytes each one stores, the greater the range of values it can represent. But the larger number of bytes used put more demand on memory resources.

Real Numbers float double Occupies 32 bits or 4 bytes, with 6 or 7 significant digits. double Occupies 64 bits or 8 bytes, with 14 or 15 significant digits.

Characters & Booleans char boolean Contains a standard ASCII character Contains a TRUE/FALSE value

Declaration variable_type variable_name; Example: int x; double temperature; char initial; boolean light;

Naming Rules Must start with a letter, underscore(_), or dollar sign($) After 1st letter, you can use numbers Can NOT be a JAVA keyword White Space is not permitted (space). No Special Characters They are case sensitive so “BOX” is not the same name as “box”

JAVA keywords

Naming Convention The first letter of the variable is lower case. When using more than one word in the variable name, the first letter of each word after the first is capitalized. Be descriptive (to a limit)

Example If we wanted to have a double that stores the room’s temperature: The Good double roomTemperature; The Bad double ZED; double theTemperatureInOurComputerRoomOnSeptemberTheTenth; The Ugly (Illegal) double case; double room Temperature; double 9/10/07Temperature;

Initialization Variables can be initialized when they are declared: int butterbeerBottles = 99; double bodyTemperature = 98.6; char display = ‘a’; boolean light = false; A variable can be turned into a constant by adding the word final at the beginning of the declaration.

Unary Operators + Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical compliment operator; inverts the value of a boolean

Operators Assignment operator Arithmetic Operators = Simple Assignment operator Arithmetic Operators + Additive operator - Subtraction operator * Multiplication operator / Division operator % Remainder operator

Order of Operations Order is determined by the precedence assigned to the operators in use within the expression. Example: 4 + 5 * 9 evaluates to 49 Still always use parenthesis!!!!…. even if it feels redundant.

Online resources http://java.sun.com/docs/books/tutorial/java/index.html