Docsity

Type Compatibility - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Type Compatibility - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Type Compatibility, Conversion,

Assignment and Casts

Variables 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.

Identifiers

  • 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.

Type 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; //invalid

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); } }

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;//cast m=i;//widening m=(long)d;//cast d=m;widening

Type 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 double

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.

Son of Type Casting

• It is generally safer to perform wider casts,

and the JVM will handle them implicitly.

• Narrowing requires explicit type casting, and

may result in a ClassCastException.

  • Object thing = new String();
  • String stringThing = (String) thing;
  • Thread bogus = (Thread) thing; // raises exception!

Type Conversion

• It most cases it makes sense to consider an

Object in the context of several different types.

• An integer may be represented as a long, a

float, a double or a String.

• Objects may be represented as integers when

using Container classes.