Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Shorthand operators.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
The character data type char
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
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 means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Working with arrays (we will use an array of double as example)
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 3 Assignment Statement
Chapter 2.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
The Boolean (logical) data type boolean
Numerical Data Types.
In this class, we will cover:
Java’s Central Casting
The for-statement.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Numeric literals and named constants

Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program

The data type of literals Important fact: Every numerical literal (constant) has a data type

The data type of literals (cont.) Type assignment rules for constants in Java: A integer literal (integer constant) has the type int Example: A decimal literal (decimal constant) has the type double Example: has the type int has the type double

Special tags that change the data type of a constant long tag: The tag L or l after an integer literal will change the data type of the literal to long Example: 12345L has the type long

Special tags that change the data type of a constant (cont.) The float tag: The tag F or f after a decimal literal will change the data type of the literal to float Example: f has the type float

Exercise: can you tell what's wrong with these statements ? Figure out what cause the error in each of the statements in the following Java program: public class Exercise3 { public static void main(String[] args) { int a; float b; a = 1L; // What is wrong ? b = 1.0; // What is wrong ? }

Exercise: can you tell what's wrong with these statements ? (cont.) Example Program: (Demo above code) –Prog file: Exercise3.java How to run the program: Right click on link and save in a scratch directory To compile: javac Exercise3.java Can't run the program, look at the reported errors !

Exercise: can you tell what's wrong with these statements ? (cont.) Here is the compile errors: Exercise3.java:10: possible loss of precision found : long required: int a = 1L; // What is wrong ? ^ Exercise3.java:12: possible loss of precision found : double required: float b = 1.0; // What is wrong ? ^ 2 errors

Assigning integer constants to byte and short variables Strictly speaking, you cannot assign an integer literal (which has the data type int) to a byte typed or short typed variable. Example: strictly speaking, the following assignment statements are not allowed:

Assigning integer constants to byte and short variables (cont.) public class Demo1 { public static void main(String[] args) { byte a; short b; a = 1; // Assigns an int value (1) to a byte variable (a) b = 1; // Assigns an int value (1) to a short variable (b) }

Assigning integer constants to byte and short variables (cont.) Strictly speaking, you need to use casting operators: The designers of the Java language deem this to be inconvenient and introduced a convenience conversion rule into the Java language public class Demo1 { public static void main(String[] args) { byte a; short b; a = (byte) 1; // Converts an int value (1) to a byte value b = (short) 1; // Converts an int value (1) to a short value }

Assigning integer constants to byte and short variables (cont.) Convenience auto-conversion rule: If an integer literal (constant) is assigned to a byte typed or short typed variable, the literal (constant) is automatically converted into the appropriate type if the constant is within the range of the data type of the variable.

Assigning integer constants to byte and short variables (cont.) Example: The range of the byte type is − Therefore, the following assignment statements are permitted (through the convenience conversion rule): byte a; a = 1; a = 127; a = -128;

Assigning integer constants to byte and short variables (cont.) However, the following assignment statements are illegal (out of range !): byte a; a = 128; a = -129;

Assigning integer constants to byte and short variables (cont.) Example Program: (Demo above code) –Prog file: Byte.java How to run the program: Right click on link and save in a scratch directory To compile: javac Byte.java Can't run - look at the compiler errors.

Named literals (constants) Named constants in Mathematics: π = e = (base of the natural log)

Named literals (constants) (cont.) We can give a name to any constant in Java Later on in the program, we can use the name in place of the actual constant Syntax to define a named constant: final datatype CONSTANT_NAME = value ;

Named literals (constants) (cont.) Explanation: The keyword final introduces the definition of a named constant datatype is the data type of the named constant (Yes, a named constant also has a data type) CONSTANT_NAME is the name of the constant CONSTANT_NAME is an identifier value is the value of the named constant

Named literals (constants) (cont.) Example: area of a circle using a named constant myPi public class AreaOfCircle2 { public static void main(String[] args) { double r; // variable containing the radius double area; // variable containing the area final double myPi = ; r = 4; // Give the radius area = myPi * r * r; // Compute the area of the circle System.out.print("The radius = "); System.out.println(r); System.out.print("The area = "); System.out.println(area); }

Named literals (constants) (cont.) Example Program: (Demo above code) –Prog file: AreaOfCircle2.java How to run the program: Right click on link and save in a scratch directory To compile: javac AreaOfCircle2.java To run: java AreaOfCircle2