1 / 21

Numeric literals and named constants

Numeric literals and named constants. Numeric literals. Numeric literal: Example:. A numeric literal is a constant value that appears in a Java program. 3.14159265358979 1776. The data type of literals. Important fact:.

shandi
Download Presentation

Numeric literals and named constants

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Numeric literals and named constants

  2. Numeric literals • Numeric literal: Example: • A numeric literal is a constant value that appears in a Java program. 3.14159265358979 1776

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

  4. 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

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

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

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

  8. 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 !

  9. 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

  10. 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:

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

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

  13. 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.

  14. 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;

  15. 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;

  16. 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.

  17. Named literals (constants) • Named constants in Mathematics: • π = 3.14159265358979323846264338327950288419716939937510582097... • e = 2.7182818284590452353602875.... (base of the natural log)

  18. 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 ;

  19. 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

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

  21. 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

More Related