Object Oriented Programming Language (JAVA) : Course Code: CSE 06132133 Credit: 3.0
Object Oriented Programming Language (JAVA) : Course Code: CSE 06132133 Credit: 3.0
Language (JAVA)
Lecture 03
● Every variable has a type, every expression has a type, and every type is strictly defined.
● All assignments, whether explicit or via parameter passing in method calls, are checked for type
compatibility.
● There are no automatic coercions or conversions of conflicting types as in some languages.
● The Java compiler checks all expressions and parameters to ensure that the types are compatible.
The Primitive Types
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
The primitive types are also commonly referred to as simple types. These can be put in four groups:
● Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
● Floating-point numbers: This group includes float and double, which represent numbers with
fractional precision.
● Characters: This group includes char, which represents symbols in a character set, like letters and
numbers.
● Boolean: This group includes boolean, which is a special type for representing true/false values.
The Primitive Types
Java defines four integer types: byte, short, int, and long. All of these are signed, positive and
negative values. Java does not support unsigned, positive-only integers.
Floating-point numbers, also known as real numbers, are used when evaluating expressions that require
fractional precision. There are two kinds of floating-point types, float and double, which represent single-
and double-precision numbers, respectively.
● Java has a primitive type, called boolean, for logical values. It can have only one of two possible
values, true or false.
● This is the type returned by all relational operators, as in the case of a < b. boolean is also the type
required by the conditional expressions that govern the control statements such as if and for.
Integer Literals
Decimal Values 1, 3, 42
● Standard notation: decimal point followed by a fractional component 2.0, 3.14159, 0.6667
● Scientific notation: standard-notation, floating-point number plus a suffix by an E or e followed by a
decimal number, which can be positive or negative 6.022E23, 314159E–05, 2e+100.
● Default to double precision (appending a D or d is optional and redundant)
● For a float literal, you must append an F or f to the constant
● There is no Binary (0b/0B), Octal (0) representation in floating point literals
● Hexadecimal floating-point literals are supported, but must be in a form similar to scientific notation,
but with a P or p 0x12.2P2 represents 72.5 (0x12.2 = 18.125 and P2 = 2^2 = 4, then 18.125*4=72.5)
● The value following the P, called the binary exponent, indicates the power-of-two by which the
number is multiplied.
● One or more underscores can be embedded in a floating-point literal (only between two digits)
9_423_497.1_0_9
Boolean Literals
● Boolean literals are simple. There are only two logical values that a boolean value can have, true
and false.
● The values of true and false do not convert into any numerical representation.
● The true literal in Java does not equal 1, nor does the false literal equal 0.
● In Java, the Boolean literals can only be assigned to variables declared as boolean or used in
expressions with Boolean operators.
\ddd Octal character (ddd)
\\ Backslash
● A literal character is represented inside a pair of single
quotes. All of the visible ASCII characters can be directly \r Carriage return
entered inside the quotes, such as 'a', 'z', and '@'.
\n New line (also known as line feed)
● For characters that are impossible to enter directly, they
can be entered using escape sequences '\'', '\n' \f Form feed
● For octal notation, use the backslash followed by the
three-digit number. For example, '\141' is the letter 'a'. \t Tab
● String literals in Java are specified as enclosing a sequence of characters between a pair of double
quotes.
● Must be in a Single Line. Examples:
● For multi-lline string a text block (added in JDK 15) is needed. Examples:
"""
This is a
Multiline
String"""
Variables
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is
● A block defines a scope. Thus, each time you start a new block, you are creating a new scope.
● A scope determines what objects are visible to other parts of your program. It also determines the
lifetime of those objects.
● Traditional scopes like global and local, do not fit well with Java’s strict, object-oriented model.
● In Java, the two major scopes are those defined by a class and those defined by a method.
● The scope defined by a method begins with its opening curly brace. However, if that method has
parameters, they too are included within the method’s scope. A method’s scope ends with its closing
curly brace. This block of code is called the method body.
● As a general rule, variables declared inside a scope are not visible (that is, accessible) to code that is
defined outside that scope.
● A variable declared within a block is called a local variable.
The Scope and Lifetime of Variables
● Although blocks can be nested, you cannot declare a variable to have the same name as one in an
outer scope.
Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type conversion will take place if
the following two conditions are met:
When these two conditions are met, a widening conversion takes place. For widening conversions, the numeric
types, including integer and floating-point types, are compatible with each other. However, there are no
automatic conversions from the numeric types to char or boolean. Also, char and boolean are not compatible
with each other. Java also performs an automatic type conversion when storing a literal integer constant into
variables of type byte, short, long, or char. (As long as destination size is in within range)
Casting Incompatible Types
● If you want to assign an int value to a byte variable which will not be performed automatically,
because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing
conversion, since you are explicitly making the value narrower so that it will fit into the target type.
● To create a conversion between two incompatible types, you must use a cast. A cast is simply an
explicit type conversion. It has this general form: (target-type) value
● If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of
an integer division by the) byte’s range.
● A different type of conversion will occur when a floating-point value is assigned to an integer type:
truncation. When a floating-point value is assigned to an integer type, the fractional component is
lost or truncated.
Casting Incompatible Types
Outputs:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
Automatic Type Promotion in Expressions
● Java automatically promotes each byte, short, or char operand to int when evaluating an
expression. This means that the subexpression a*b is performed using integers—not bytes.
● As useful as the automatic promotions are, they can cause confusing compile-time errors.
●
●
The Type Promotion Rules
● An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can
be created and may have one or more dimensions. A specific element in an array is accessed by its index.
● The general form of a one-dimensional array declaration is type[ ] var-name;
● Although this declaration establishes the fact that its an array variable, no array actually exists. To link it
with an actual, physical array of that type, must allocate one using new. new is a special operator that
allocates memory.
● The general form of new as it applies to one-dimensional arrays appears as follows:
array-var = new type [size];
● The elements in the array allocated by new will automatically be initialized to zero (for numeric types),
false (for boolean), or null (for reference types will be discussed later)
● In Java all arrays are dynamically allocated.
Arrays
Multidimensional Arrays
There is a second form that may be used to declare an array: type var-name[ ];
Introducing Type Inference with Local Variables
● Beginning with JDK 10, a new feature called local variable type inference was added to the Java
language. The compiler infer the type of a local variable based on the type of its initializer, thus
avoiding the need to explicitly specify the type.
● To support local variable type inference, the context-sensitive keyword var was added.
● double avg = 10.0; Using type inference, var avg = 10.0;
● var can be used to declare an array. For example: var myArray = new int[10];
● Even though it is context-sensitive, there are a few places in which the use of var is illegal. It cannot
be used as the name of a class,
Introducing Type Inference with Local Variables
Output:
Value of avg: 10.0
Value of var: 1
Value of k: -1
Some var Restrictions
1. var can be used only to declare local variables. It cannot be used when declaring instance variables,
parameters, or return types, for example.
2. Only one variable can be declared at a time; a variable cannot use null as an initializer; and the variable
being declared cannot be used by the initializer expression.
3. As mentioned earlier, var cannot be used as the name of a class. It also cannot be used as the name of
other reference types, including an interface, enumeration, or annotation, or as the name of a generic
type parameter, all of which are described later in this book.
4. Here are two other restrictions that relate to Java features described in subsequent chapters but
mentioned here in the interest of completeness. Local variable type inference cannot be used to declare
the exception type caught by a catch statement. Also, neither lambda expressions nor method references
can be used as initializers.
Some var Restrictions
A Few Words About Strings
● As you may have noticed, in the preceding discussion of data types and arrays there has been no
mention of strings or a string data type. It is just that Java’s string type, called String, is not a
primitive type. Nor is it simply an array of characters. Rather, String defines an object, and a full
description of it requires an understanding of several object-related features.
● The String type is used to declare string variables. You can also declare arrays of strings. A quoted
string constant can be assigned to a String variable. A variable of type String can be assigned to
another variable of type String.