# Widening conversion happens automatically no

• Notes
• UltraLightningCobra8457
• 5

This preview shows page 2 - 4 out of 5 pages.

Widening conversion happens automatically. No information is lost. int i = 45; double d = i; 9 Automatic type promotion We have already seen that Java will automatically promote the smaller numeric type when an arithmetic operator is used on mixed types (e.g. adding an int and a double): 2.0 automatically promoted d 5.0 + 3.0 For example: double d; d = 2 + 3.0; 10 Explicitly converting between compatible types We can use a "cast" to force a conversion from one type to another. For example: the fractional part is truncated when the conversion takes place d i . 23 7 23 double d = 23.7; int i = (int) d; 11 Calling a Method Statements that end with () are method calls, e.g. System.out.println ( "Hello" ) ; static methods instance methods There are two types of methods in Java: We say that we are calling the println() method. 12 Using Static Methods A static method is a method that is called using the following form: ClassName . methodName ( list of values ) Math . random(); Math . max(a, b); Math . min(a, b); ClassName . methodName () For example: or

Subscribe to view the full document.

CS101 Lecture 5 3 13 double x = Math.max(25.1, 13.5); The Math class The Math class contains many static methods which perform common mathematical functions, e.g. Math.min(int, int) Returns the greater of two values (either int or double). Returns the smaller of two values (either int or double). Math.max(int, int) For example: Math.min(double, double) Math.max(double, double) 25.1 14 double x = Math.pow(4, 2); The Math class continued Math.pow(double, double) Returns the first number to the power of the second number. Math.sqrt(double) Returns the square root of a double value. Math.random() Returns a random number between 0.0 and 1.0 (excluding 1.0). For example: 16.0 15 Math.random() Produces a random double value greater than or equal to 0.0 and less than 1.0 0.0 0.9999999 double d = Math.random(); For example: In this case, the variable, d, will store a number which is greater than or equal to 0.0 and less than 1.0.
You've reached the end of this preview.
• Summer '12
• AdrianaFerraro
• Computer Science, Order of precedence, String literal, public void

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern