Docsity

Data Conversion - Java Programming - Exam Review Notes | COP 2250, Quizzes of Javascript programming

Toggle sidebar
bg1
1. Data Conversion
•In Java, data conversions can occur in three ways:
–assignment conversion
float f;
int i = 5;
f = i; // f is 5.0
–promotion
float f = 2.0f;
// or 2.0F since 2.0 is a double constant
// float f = 2.0; wrong!
int i = 5;
f = f/i;
// the value of i is promoted to float type and //
then the division between two float type data //is
performed
–casting
float f = 2.0f;
int i = 5;
i = (float)f/i;
//since the expression of “f/i” will return a
//float type data which could not be
//automatically converted to integer and
casting //is required
//Casting: a pair of parenthesis with casting
//type enclosed such as (int) (float)
•Widening conversions: from small data type to a larger one
short -> int -> long
float -> double
int -> float
long -> double
Reverse arrows correspond to narrowing conversions
Widening conversions are safe and could be done automatically.
int i = 5;
float f;
f = i; // int -> float
Narrowing conversions need to be done through casting!
i = f; // wrong assignment. Correct one is: i = (float)f and the value of i is the
integer part of variable f.
Try to understand:
int total = 20, count = 7; float average;
average = total / count; // 2.0
average = (float)total / count; // 2.857143
average = total / (float)count; // 2.857143
pf3
pf4
pf5

Partial preview of the text

Download Data Conversion - Java Programming - Exam Review Notes | COP 2250 and more Quizzes Javascript programming in PDF only on Docsity!

  1. Data Conversion
    • In Java, data conversions can occur in three ways:
      • assignment conversion float f; int i = 5; f = i; // f is 5.
      • promotion float f = 2.0f; // or 2.0F since 2.0 is a double constant // float f = 2.0; wrong! int i = 5; f = f/i; // the value of i is promoted to float type and // then the division between two float type data //is performed
      • casting float f = 2.0f; int i = 5; i = (float)f/i; //since the expression of “f/i” will return a //float type data which could not be //automatically converted to integer and casting //is required //Casting: a pair of parenthesis with casting //type enclosed such as (int) (float)
    • Widening conversions: from small data type to a larger one short -> int -> long float -> double int -> float long -> double Reverse arrows correspond to narrowing conversions Widening conversions are safe and could be done automatically. int i = 5; float f; f = i; // int -> float Narrowing conversions need to be done through casting! i = f; // wrong assignment. Correct one is: i = (float)f and the value of i is the integer part of variable f. Try to understand: int total = 20, count = 7; float average; average = total / count; // 2. average = (float)total / count; // 2. average = total / (float)count; // 2.

average = (float)(total / count); // 2. //casting in the last statement is redundant since //int->float is a widening conversion and could be //converted automatically

  1. Creating objects
    • Creating object reference variable A class name can be used as a type to declare an object reference variable String title; // String is a class name // title is declared as String reference variable
    • Creating object Generally, we use the new operator to create an object title = new String (“Java Programming”); // String (“Java Programming”) is String constructor , which is // a special method that sets up the object // To create a String object, there is another way to do it: // title = “Java Programming”;
    • Invoking methods use the dot operator to invoke its methods count = title.length(); //length() is method of String object, title
    • References An object reference can be thought of as a pointer to the location of the object String name1 = “Tom”; String name2 = “Mike”; System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Tom name2: Mike name2 = name1; // name2 points to the object which name1 points to System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Tom name2: Tom // String object “Mike” has no object reference // variable pointing to it and is ready to be // garbage collected by java compiler automatically name1 = “Peter”; System.out.println(“name1: ” + name1 + “ name2: ” + name2); //name1: Peter name2: Tom String Class
  • Two ways to create String object //with new
  • String methods The index begins at zero in each string In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4 length() – return the length of the string Each escape character corresponds to one character String s = new String(“Hello\tWorld!\n”); System.out.println( s.length() ); // substring(start, end) – return substring of the string object Start – inclusive ; end – exclusive String s = new String(“Hello\tWorld!\n”); System.out.println( s.substring(2, 5) ); //llo; 2 corresponds to first l and 5 to ‘\t’
  1. Formatting output
  • NumberFormat //import class import java.text.NumberFormat; //create formatter object //Currency Formatter object NumberFormat currencyFmt = NumberFormat.getCurrencyInstance(); // Percent Formatter object NumberFormat percentFmt = NumberFormat.getPercentInstance(); // Each formatter object has a method called format // that returns a string with the specified // information in the appropriate format double findal TAX_RATE = 0.06; System.out.println(“Tax Rate: ” + percentFmt.format(TAX_RATE)); //Tax Rate: 6%
  • DecimalFormat //import class import java.text.DecimalFormat; //create DecimalFormat object DecimalFormat fmt = new DecimalFormat ("0.###"); //0 Digit // # Digit, zero shows as absent System.out.println(fmt.format(3.8976));

System.out.println(fmt.format(3.8)); //3.