NJIT CS 602 - Type Compatibility, Conversion, Assignment and Casts

Unformatted text preview:

Type Compatibility, Conversion, Assignment and CastsVariables and TypesVariable (a memory location)IdentifiersSample Code StandardsJava Primitive TypesType CompatibilityType Conversion in Primitive TypesCommon misunderstandingSlide 10Slide 11Type Casting in Primitive TypesAssignmentType Conversion in ExpressionsPromotion rules exampleJava Reference TypesThe Object TypeType Casting ReturnsSon of Type CastingType ConversionConversion of Reference typesSample code fragmentPolymorphic AssignmentProper ways of DowncastingContinue..Conversion FunctionsInteger Class: UsageInteger Class: Static MethodsType AssignmentSlide 30Managing Class VersionsReferencesReferences, continuedSlide 34Type Compatibility, Conversion,Assignment and CastsGeorge Blank, Mike Meehan, Asra SultanaVariables and Types•A variable is a location in memory where values can be stored and referenced.•Variables are associated with types, which have particular data sizes and a restricted set of valid operations that can be performed on them.•Variable declarations assign an identifier, a type, and may declare an initial value. Many types have a default value. For example, an uninitialized integer defaults to 0.Variable (a memory location)addressa value(a default value)a name (identifier)a typeThe address or value may be passed as a parameterIdentifiers•Identifiers are used in Java to give a name to classes, methods and variables that can be used to access them. Java identifiers begin with a “letter,” followed by letters or digits. A Java “letter” includes the letters of any alphabet in Unicode plus the underscore _ and dollar sign $.•There is a Java standard naming convention for the use of identifiers, which can be found at http://java.sun.com/docs/codeconv/Sample Code Standards•Here are a few examples of code standards from the section on declarations:–6.1 Use one declaration per line to encourage comments–6.2 Try to initialize local variables where they are declared–6.3 Put declarations only at the beginning of blocks•There are a lot of details to learn, and it will take a while to be proficient in Java coding.Java Primitive Types•Boolean: boolean•Integer: byte, short, int, long•Character: char•Floating Point: float, doubleType Compatibility•Two Types are compatible if values of one type can appear wherever values of the other type are expected, and vice versa.•Java is a strongly typed language and from there comes it’s safety and robustness.•Every variable has a type which is strictly defined.•All assignments ,whether explicit or via parameter passing in method calls ,are checked for type compatibility and any mismatches result in errors .Type Conversion in Primitive Types•Widening– When one type of data is assigned to another type of variable, an automatic(implicit) type conversion will take place if,the two types are compatible and the destination type is larger than the source type.–Example: int i; byte b; i=b; //valid conversion•Narrowing–Converting a value of larger range to value of smaller range. –Example: int i; byte b; b=i; //invalidCommon misunderstanding•Implicit and explicit typecasting of primitive types is frequently misunderstood.•The next slide is a small test program, overloaded with multiple constructors to show when and how type conversion promotion is done with implicit casting.•In this example, since the third value is an integer, but there is no constructor that takes an integer, its value and the float value automatically convert to double. (Case 5: double, double, double)public class typetest { typetest(double a, double b, short c){ System.out.println("1 (dbl dbl short)");} typetest(float a, byte b, long c) { System.out.println("2 (float byte long)"); }typetest(long a, long b, long c) { System.out.println("3 (long long long)"); }typetest(float a, long b, short c) { System.out.println("4 (float long short)"); } typetest(double a, double b, double c) { System.out.println("5 (dbl dbl dbl)"); } public static void main(String[] args) { typetest t = new typetest(); t.typetest(3.4, 3L, 3); } }Type Casting in Primitive Types•A cast is an explicit type conversion of incompatible types.•General form : (target-type)valueTarget-type is the desired type to convert the value to.•A different type of conversion called truncation will occur when a floating point value is assigned to an integer type. •If the size of the whole number is too large to fit into the target int type, then that value will be reduced modulo the target type’s range.Assignment•For type conversion and casting the Assignment Operator is used between the two values.•General form: var = exp •Example: int i=10;long m=10000L;double d=Math.PI;//PI=3.1415….i=(int)m;//castm=i;//wideningm=(long)d;//castd=m;wideningType Conversion in Expressions•In addition to assignments, type conversions may occur in expressions.•Promotion rules that apply to expressions:– all byte and short values are promoted to int–If one operand is long,the whole expression is promoted to long–If one operand is a float ,the whole expression is promoted to float–If one operand is double ,the whole expression is promoted to doublePromotion rules exampleclass Promote {Public static void main(String args[]){ byte b=12;char c=‘a’;short s=1004;int i=20000; float f=5.67f; double d=.1234;double res=(f*b)+(i/c)-(d*s); System.out.println(“result :” +res);}}Java Reference Types•In addition to the primitive types, Java allows types that are objects, and which can be user defined with a class definition.•Since objects can have a hierarchy, with supertypes and subtypes, some operations can be inherited by subtypes. Guidelines for this must be understood to avoid problems.The Object Type•All reference types are subclasses of the java.lang.Object type. •Any object may be referred to by a reference variable of type Object.•However, Objects have limited functionality, and it is advantageous to convert the generic Object to a more specific class, if possible.Type Casting Returns•Type casting comes in two flavors: narrowing and widening.•Narrowing casts force a variable of one type into a more restrictive structure (e.g. a parent class casted to a child class, or an interface casted to an implementing class).• Widening casts do the opposite, moving from a child class to a parent class.Son of Type Casting•It is generally safer to


View Full Document

NJIT CS 602 - Type Compatibility, Conversion, Assignment and Casts

Download Type Compatibility, Conversion, Assignment and Casts
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Type Compatibility, Conversion, Assignment and Casts and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Type Compatibility, Conversion, Assignment and Casts 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?