Constants in Java Why and How Copyright © 2005-2011 Curt Hill.

Slides:



Advertisements
Similar presentations
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Advertisements

Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
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.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Java Data Types Data types, variable declaration, and initialization.
Copyright © 2005 Curt Hill Constants in C++ Why and How.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
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 Types What they do.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Wednesday, September 3, 2014 CSCI 351 – Mobile Applications Development.
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.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Fundamentals 2.
The Second C++ Program Variables, Types, I/O Animation!
Chapter 1.2 Introduction to C++ Programming
Week 2 - Wednesday CS 121.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 1.2 Introduction to C++ Programming
The Machine Model Memory
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Programming Fundamentals
ECE Application Programming
More important details More fun Part 3
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Variables and Primative Types
Type Conversion, Constants, and the String Object
Variables Store information needed by the program Must have a TYPE
Methods Additional Topics
Building Java Programs Chapter 2
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
A Different Kind of Variable
Chapter 2: Java Fundamentals
Compound Statements A Quick Overview
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Variables, Types, Operations on Numbers
Chapter 2: Java Fundamentals
The Java Alternative to Multiple Inheritance
Variables In today’s lesson we will look at: what a variable is
The Java switch Statement
Overloading functions
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Introduction to Primitives
Introduction to Primitives
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Module 2 Variables, Data Types and Arithmetic
EECE.2160 ECE Application Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Presentation transcript:

Constants in Java Why and How Copyright © 2005-2011 Curt Hill

Introduction There are two kinds of constants in Java Literals Named constants We will see the whys and wherefores now Copyright © 2005-2011 Curt Hill

Literal constants We have seen the following fours kinds of constants: -45 (int) 2.3 (floating point of type double) 1E4 (float) ‘A’ (char) “Hi” (string) Normally, merely inspecting the constant will tell you its type, as well as its value Copyright © 2005-2011 Curt Hill

Literals Continued What do we do to set the constant to something other than a standard int or double? We can use a suffix to tell it the type The L or l indicates that it is long The U or u indicates unsigned Floating point constants are by default double Suffix of F or f makes it a float, L a long double You may also use binary, octal or hexadecimal constants but we do not need to do that here Copyright © 2005-2011 Curt Hill

Literal Problems However, literal constants have the following problems that sometimes need to be dealt with We have to retype them each time This is a problem with high precision constants like PI Opportunities for typing errors Just remembering what the 14 digit value is We have to remember what they represent Small integer constants tend to get confused with one another Copyright © 2005-2011 Curt Hill

Example: Suppose that I am a manager and I have 5 people who work 5 days a week and 8 hours a day Thus the number of manhours is: workhours = 5 * 5 * 8; Now we will try something new, four ten hour days per week I use my text editor and change all 5s to 4s and all 8s to 10s We now get workhours = 4 * 4 * 10; The problem with the above expression is that the 5 and 8 have no inherent meaning Copyright © 2005-2011 Curt Hill

Alternative It would be better to say: int people = 5, dayperweek = 5, hoursperday = 8; ... workhours = people * dayperweek * hoursperday; Unfortunately that leads to a problem: An inadvertent assignment to one of the variables The solution is that we would like to be able to name constants, just like we name variables but without possibility of assignment Copyright © 2005-2011 Curt Hill

Final Keyword A feature that allows constants in Java is the final qualifier: final float pi = 3.14159; Any attempt to assign to this will result in an error The float type determines the precision used Copyright © 2005-2011 Curt Hill

Discussion The final is a keyword that prefixes the declaration This may be used anywhere a variable may be declared The declaration must have an initialization Copyright © 2005-2011 Curt Hill

Conclusion The reserved word final makes a variable into a constant Use to make the code more readable Shortens the typing for constants with many digits Prevents using global replaces from doing too much Copyright © 2005-2011 Curt Hill