Top Banner
Numeric literals and named constants
21

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

Dec 18, 2015

Download

Documents

Asher Townsend
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.

Numeric literals and named constants

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

Numeric literals

• Numeric literal:

Example:

• A numeric literal is a constant value that appears in a Java program.

3.14159265358979 1776

Page 3: 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

Page 4: 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 (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:

• 12345 has the type int

•123.45 has the type double

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

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

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

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:

• 123.45f has the type float

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

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 ? } }

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

Exercise: can you tell what's wrong with these statements ? (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/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 !

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

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

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

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:

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

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) } }

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

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 } }

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

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.

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

Assigning integer constants to byte and short variables (cont.)

• Example:

• The range of the byte type is −128 ... 127

• Therefore, the following assignment statements are permitted (through the convenience conversion rule):

byte a;

a = 1; a = 127; a = -128;

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

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;

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

Assigning integer constants to byte and short variables (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/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.

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

Named literals (constants)

• Named constants in Mathematics:

• π = 3.14159265358979323846264338327950288419716939937510582097...

• e = 2.7182818284590452353602875.... (base of the natural log)

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

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 ;

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

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

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

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 = 3.14159265358979;

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); } }

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

Named literals (constants) (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/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