LESSON 5 – Assignment Statements JAVA PROGRAMMING.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
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.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
Expressions, Data Conversion, and Input
 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.
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.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Primitive Variables.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
1 Operations Making Things Happen (Chap. 3) Expressions.
Primitive Variables.
Introduction to Java Primitive Types Operators Basic input and output.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java-02 Basic Concepts Review concepts and examine how java handles them.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
ISBN Chapter 7 Expressions and Assignments Statements.
Primitive Types Four integer types: Two floating-point types:
Expressions.
Chapter 7: Expressions and Assignment Statements
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Chapter 2.
Conversions of the type of the value of an expression
Explicit and Implicit Type Changes
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Numerical Data Types.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Assignment Statements
Expressions and Assignment
Data Types and Expressions
Java Programming Language
Primitive Types and Expressions
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

LESSON 5 – Assignment Statements JAVA PROGRAMMING

Assignment statement In Java, the assignment statement is used to change the value of a variable The equal sign (=) is used as the assignment operator An assignment statement consists of a variable on the left side of the operator, and an expression on the right side of the operator Variable = Expression; An expression consists of a variable, number, or mix of variables, numbers, operators, and/or method invocations. For example: temperature = 98.6; count = numberOfBeans; The assignment operator is automatically executed from right ‐ to-left, so assignment statements can be chained number2 = number1 = 3;

Assignment compatibility In general, the value of one type cannot be stored in a variable of another type. A double value cannot be stored in an int variable. An int cannot be assigned to a variable of type boolean, nor can a boolean be assigned to a variable of type int Example: int intVariable = 2.99; //Illegal However, there are exceptions to this double doubleVariable = 2; – For example, an int value can be stored in a double type

Assignment compatibility More generally, a value any type in following list can be assigned to a variable of any type that appears to the right of it byte → short → int → long → float → double char Note that as your move down the list from left to right, the range of allowed values for the types becomes larger

Self Test 1 Is the following legal/illegal? 1. float x = 39.57; 2. int s = 5.6; 3. byte a = 500;

Type Casting Casting lets you convert primitive values from one type to another. Two type of casting: 1. Implicit casting – the conversion happens automatically 2. Explicit casting – programmer tells the compiler the type to cast Example of implicit cast: int a = 100; long b = a; // Implicit cast, an int value always //fits in a long Example of explicit cast: double d = 4.5; int i; i = (int)d; // The value of variable i is 4 // The value of variable d is 4.5 //Explicit cast, the integer could lose info

Widening conversion putting a smaller thing (say a byte) a say, into bigger container (like an int). an implicit cast happens when you're doing a widening conversion : small ‐ value ‐ into ‐ large ‐ container. Integer values may be assigned to a double variable without explicit casting, because any integer value can fit in a 64 ‐ bit double. – Example: double d = 100L; // Implicit cast

Narrowing conversion put a larger thing (say a long) into a conversion say, smaller container (like a int). The large ‐ value ‐ into ‐ small ‐ container conversion requires explicit cast. An explicit type cast is required to assign a value of larger type (e g float) e.g., to a variable with smaller value type (e.g. int) – Example: float a = f; int b = (int)a; // Explicit cast, the float //could lose info

END OF LESSON 5 THE END