ch03-4 - Typecasting typecast:convertfromonetypetoanother....

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Type casting type cast :  convert from one type to another. Common uses: To promote an  int  into a  double  to achieve exact division. To truncate a  double  from a real number to an integer. type cast general syntax: (   <type>   )   <expression> Examples: double result = (double) 19 / 5; // 3.8 int result2 = (int) result; // 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 More about type casting Type casting has  high  precedence only casts item immediately after it. double x = (double) 1 + 1 / 2; // 1.0 double y = 1 + (double) 1 / 2; // 1.5 Use parentheses to force evaluation order. double average = (double) (a + b + c) / 3; conversion to  double  can be achieved in other ways. double average = 1.0 * (a + b + c) / 3;
Background image of page 2
3 Objects So far methods --  represent behavior variables --  represent data (categorized by  types ) Can create new types that are combinations of the existing  types. These new types are called  object types  or  reference types . object-oriented  programming languages:  languages (such as Java) that  allow this
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Objects and classes object:  An entity (a thing) that contains data and behavior. Variables inside the object, hold its data. Methods inside the object, carry out its behavior. class : A program, or. .. A type of objects. Examples: The class  String  represents objects that store text characters. The class  Point  represents objects that store (x, y) data. The class  Scanner  represents objects that read information from the  keyboard, files, and other sources.
Background image of page 4
Constructing objects construct : create a new object in RAM. use the  new  keyword. Most objects must be constructed before they can be used. Constructing objects, general syntax: <type> <name> = new <type>   (   <parameters>   ); Examples: Point p = new Point(7, -4); DrawingPanel window = new DrawingPanel(300, 200); Color orange = new Color(255, 128, 0); Classes' names are usually uppercase (e.g.  P oint C olor ). Strings are also objects, but can be constructed without 
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/15/2010 for the course COMPSCI Comp 142 taught by Professor Topham during the Spring '06 term at Shoreline.

Page1 / 18

ch03-4 - Typecasting typecast:convertfromonetypetoanother....

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online