



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Implicit, Conversions, Char, Int, Float, Double, Explicit, Range, Java, Acess, C
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Type Conversion
Java is much stronger than C++ in the type conversions that are allowed. Here we discuss conversions among primitive types. Conversions among class objects will be discussed later.
Booleans cannot be converted to other types. For the other primitive types (char, byte, short, int, long, float, and double), there are two kinds of conversion: implicit and explicit.
Implicit conversions:
An implicit conversion means that a value of one type is changed to a value of another type without any special directive from the programmer. A char can be implicitly converted to an int, a long, a float, or a double. For example, the following will compile without error:
char c = 'a'; int k = c; long x = c; float y = c; double d = c;
For the other (numeric) primitive types, the basic rule is that implicit conversions can be done from one type to another if the range of values of the first type is a subset of the range of values of the second type. For example, a byte can be converted to a short, int, long or float; a short can be converted to an int, long, float, or double, etc.
Explicit conversions:
Explicit conversions are done via casting : the name of the type to which you want a value converted is given, in parentheses, in front of the value. For example, the following code uses casts to convert a value of type double to a value of type int, and to convert a value of type double to a value of type short:
double d = 5.6; int k = (int)d; short s = (short)(d * 2.0);
Casting can be used to convert among any of the primitive types except boolean. Note, however, that casting can lose information; for example, floating-point values are truncated when they are cast to integers (e.g., the value of k in the code fragment given above is 5), and casting among integer types can produce wildly different values (because upper bits, possibly including the sign bit, are lost).
JAVA CLASSES
Java classes contain fields and methods. A field is like a C++ data member, and a method is like a C++ member function. Each field and method has an access level : private: accessible only in this class (package): accessible only in this package protected: accessible only in this package and in all subclasses of this class public: accessible everywhere this class is available
Similarly, each class has one of two possible access levels: (package): class objects can only be declared and manipulated by code in this package public: class objects can be declared and manipulated by code in any package
Note: for both fields and classes, package access is the default, and is used when no access is specified.
A Simple Example Class
In the following example, a List is defined to be an ordered collection of items of any type:
class List { // fields private Object [ ] items; // store the items in an array private int numItems; // the current # of items in the list // methods
// constructor function public List() { items = new Object[10]; numItems = 0; }
// AddToEnd: add a given item to the end of the list public void AddToEnd(Object ob) { ... } }
In Java, all classes (built-in or user-defined) are (implicitly) subclasses of the class Object. Using an array of Object in the List class allows any kind of Object (an instance of any class) to be stored in the list. However, primitive types (int, char, etc) cannot be stored in the list as they are not inherited from Object.
Constructor function:
Final Fields and Methods
Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned to again.
Some Useful Built-in Classes
Following is a list of some useful classes in Java. These classes are not really part of the language; they are provided in the package java.lang
1. String
String Creation:
String S1 = "hello", // initialize from a string literal S2 = new String("bye"), // use new and the String constructor S3 = new String(S1); // use new and a different constructor
String concatenation
String S1 = “hello ” + “world”; String S2 = S1 + “!”; String S3 = S1 + 10;
2. Object
Object is the Base class for all Java Classes.
3. Classes for primitive types
In Java, we have classes also for primitive types. These are: Boolean, Integer, Double, etc. Note that variable of bool, int, etc types are not objects whereas variable of type Boolean, Integer, etc are objects.
Packages
Every class in Java is part of some package. All classes in a file are part of the same package. You can specify the package using a package declaration :
package name ;
as the first (non-comment) line in the file. Multiple files can specify the same package name. If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files). If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
You can access public classes in another (named) package using:
package-name.class-name
You can access the public fields and methods of such classes using:
package-name.class-name.field-or-method-name
You can avoid having to include the package-name using import package-name .* or import package-name.class-name at the beginning of the file (after the package declaration). The former imports all of the classes in the package, and the second imports just the named class.
You must still use the class-name to access the classes in the packages, and class-name.field-or-method-name to access the fields and methods of the class; the only thing you can leave off is the package name.
Inheritance
Java directly supports single inheritance. To support concept of a interface class is used. Inheritance is achieved as shown below:
class SuperClass { … }
class SubClass extends SuperClass { … }
When a class is inherited, all fields and methods are inherited. When the final reserved word is specified on a class specification, it means that class cannot be the parent of any class. Private fields are inherited, but cannot be accessed by the methods of the subclass. fields can be defined to be protected , which means that subclass methods can access them
Each superclass method (except its constructors) can be inherited, overloaded, or overridden. These are elaborated in the following paragraphs:
inherited : If no method with the same name is (re)defined in the subclass, then the subclass has that method with the same implementation as in the superclass.
overloaded : If the subclass defines a method with the same name, but with a different number of arguments or different argument types, then the subclass has two methods with that name: the old one defined by the superclass, and the new one it defined.
overridden : If the subclass defines a method with the same name, and the same number and types of arguments, then the subclass has only one method with that name: the new one it defined. A method cannot be overridden if it is defined as final in the superclass
Dynamic Binding
In C++, a method must be defined to be virtual to allow dynamic binding. In Java all method calls are dynamically bound unless the called method has been defined to be final , in which case it cannot be overridden and all bindings are static.