JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
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,
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Operaciones y Variables
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
 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.
Numeric Types, Expressions, and Output ROBERT REAVES.
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.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
Primitive Variables.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Java Data Types Assignment and Simple Arithmetic.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
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 Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Primitive Variables.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Topic 2 Elementary Programming
Arithmetic Expressions
Object-Oriented Programming (OOP) Lecture No. 21
Object Oriented Programming
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Increment and Decrement
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Numerical Data Types.
With Assignment Operator
Variables & Data types Selection Statements Additional Operators
Structured Program Development in C
Expressions and Assignment
Primitive Types and Expressions
Structured Program Development in C
Presentation transcript:

JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Unary Operators Unary operators such as -- or ++ involve only one variable For example in order to increase x by 1 the statement x++ is enough When two variables are used in a calculation it’s called a binary operation

Unary Operators Reminder These only need one variable (increment by 1) (decrement by 1) 3. variable += x (same as variable = variable + x) 4. variable - = x (same as variable = variable - x) 5. variable *= x (same as variable = variable * x) 6. variable /= x (same as variable = variable / x) 7. variable %= x (same as variable = variable % x)

Two ways of Using Unary Operators Unary operators can be used in two ways; 1. Postfix 2. Prefix Type StatementEquivalent Postfix n++n = n +1 n--n = n – 1 Prefix ++nn = n nn =n -1

Example Although the end result is the same, there is a difference when using postfix or prefix

Output The outputs are the following; In n++ ◦ The value of n (9) is stored in x ◦ Then n is increased by 1 ◦ Resulting value stored in n In ++n ◦ First n (9) is increased by one ◦ Then the result is stored in x ◦ So we end up with the output above

Using Reals When numbers with a fraction must be stored, real type variables must be used; 1. float 2. Double These can be used just like regular variables However, when using float the letter f must be added at the end of the number

Example

Conversions When variables are assigned to other variables for example num1 = num2, one must keep in mind the following: 1. Both variables are of the same data type, or 2. The two variables are compatible, 3. or 4. The destination variable type is larger than the source type.

Examples

Converting Variable Types We must look at the hierarchy of variable types in order to know which variables can be converted Example double is the biggest, so it cannot be converted to anything else; whilst byte (since it’s the smallest) can be converted to any type (except char). Note that boolean data types cannot be converted to anything since they are not numbers

Hierarchy doublefloatlongintcharshortbyte

Type Casting Types can also be changed by specifying to which type you want to convert them in brackets. For example: int x = (int) 9.45; This will change 9.45 to an integer (so it becomes a 9) and stores it in x.

Scope Whenever the curly brackets are opened and closed, a scope is created If a variable is declared in the main scope it can be used throughout the method, however if it is declared between the curly brackets further in the program it will be only available in that scope.

Example As you can see in the previous example x can be used throughout the program however j can only be used in that scope

Constants A constant is very similar to a variable The difference is that its values cannot be changed Hence a constant is read-only A constant is declared the same as a variable however the keyword final must be used in order to show that it’s a constant

Example For example the mathematical value for PI never changes, Hence it can be declared as a constant in our program In order to identify variables from constants, constants are created using capital letters. final double PI = 3.142;