A constant must be declared and initialized in the same statement The word

A constant must be declared and initialized in the

This preview shows page 26 - 38 out of 54 pages.

A constant must be declared and initialized in the same statement. The word final is a Java keyword for declaring a constant. Syntax: final datatype CONSTANTNAME = value; Example: final double PI = 3.14159 ; 11/08/2019 OBJECT ORIENTED PROGRAMMING 26
Image of page 26

Subscribe to view the full document.

Constants in a class Constants in a class should be declared as final static . For example, the constant PI in the Math class is defined as: final static double PI = 3.14159265358979323846 ; It is because usually constants in a class are shared by all objects of the class 11/08/2019 OBJECT ORIENTED PROGRAMMING 27
Image of page 27
Type Casting 11/08/2019 OBJECT ORIENTED PROGRAMMING 28
Image of page 28

Subscribe to view the full document.

Type Casting In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. Type casting can be categorized into two types Up-casting Converting a smaller data type into bigger one Implicit – we don’t have to do something special No loss of information Examples of Primitives int a = 10; double b = a; 11/08/2019 OBJECT ORIENTED PROGRAMMING 29
Image of page 29
Type Casting Down-casting Converting a bigger data type into smaller one Explicit – need to mention Possible loss of information Examples of Primitives double a = 7.65; int b = (int) a; 11/08/2019 OBJECT ORIENTED PROGRAMMING 30
Image of page 30

Subscribe to view the full document.

Common Error 11/08/2019 OBJECT ORIENTED PROGRAMMING 31
Image of page 31
Unintended Integer Division When two operands are integers, the / operator performs an integer division. The result of the operation is an integer. The fractional part is truncated. To force two integers to perform a floating-point division, make one of the integers into a floating-point number. For example, the code in (a) displays that average is 1 and the code in (b) displays that average is 1.5 . 11/08/2019 OBJECT ORIENTED PROGRAMMING 32
Image of page 32

Subscribe to view the full document.

Wrapper classes 11/08/2019 OBJECT ORIENTED PROGRAMMING 33
Image of page 33
Wrapper classes Owing to performance considerations, primitive data type values are not objects in Java. Because of the overhead of processing objects, the language’s performance would be adversely affected if primitive data type values were treated as objects. In development, we come across situations where we need to use objects instead of primitive data types. In-order to achieve this Java provides wrapper classes Each primitive data type has a corresponding object (wrapper class). These wrapper classes provides additional functionality (conversion, size checking etc.), which a primitive data type cannot provide. By using a wrapper class, you can process primitive data type values as objects 11/08/2019 OBJECT ORIENTED PROGRAMMING 34
Image of page 34

Subscribe to view the full document.

Wrapper classes 11/08/2019 OBJECT ORIENTED PROGRAMMING 35
Image of page 35
11/08/2019 OBJECT ORIENTED PROGRAMMING 36
Image of page 36

Subscribe to view the full document.

Wrapper Use You can create an object of Wrapper class using a String or a primitive data type Integer num = new Integer(4); or Integer num = new Integer(“4”); Note: num is an object over here not a primitive data type You can get a primitive data type from a Wrapper using the corresponding value function int primNum = num.intValue(); 11/08/2019 OBJECT ORIENTED PROGRAMMING 37
Image of page 37
Image of page 38
  • Fall '14
  • Dr Haroon

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes