Chapter 1
Chapter 1
Questions:
1. Explain Any 4 Features Of Java.
2. Explain: Platform Independence, Complied And Interpreted Features Of Java.
3. What Is JVM? What Is Byte Code?
4. Define JDK. List The Tools Available In JDK. Explain One In Details?
5. What Is Byte Code? Explain Any Tools Available In JDK.
Java Features
1. Compiled and Interpreted
2. Platform – Independent and Portable
3. Object – Oriented
4. Robust and Secure
5. Distributed
6. Dynamic and Extensible
3. Object – Oriented
● Java is strictly or truly object-oriented language due to the following reasons.
o Everything in Java is considered as the object including the source
program because it is written in a class itself.
o The object model in Java is simple and easy to extend, while simple
types, such as integers, are kept as high-performance non objects.
5. Distributed
● Java is designed as a distributed language for creating applications on
networks. It has the ability to share both data and programs.
● Java applications can open and access remote objects on Internet as easily as
they can do in local system. This enables multiple programmers at a multiple
remote locations to collaborate and work together on a single project.
Byte Code
o The Java compiler creates a byte code. This is the machine independent code so, it
can be executed on any machine.
Documentation Section
Package Statement
Import Statements
Interface Statements
Class Definitions
Identifiers:
Identifiers are the programmer-designed tokens. They are used for naming
variables, classes, methods, objects, labels, packages and interfaces in a program.
Java identifiers follow following rules:
1. They can have alphabets, digits, and the underscore and dollar sign
characters.
2. They can have be of any length.
3. They must not begin with a digit.
4. Uppercase and Lowercase letters are distinct.
5. They must not be the keywords.
Keywords:
Keywords are an essential part of a language definition. These are also called
as reserved words. Java has given a special meaning for certain words. These
words are reserved for only the defined purpose in the programming. They
cannot be used for any other purpose. We cannot use them for the name of
variables, classes or methods.
All keywords are to be written only in lower case letters. Since, Java is case
sensitive; we can use these words as name of variables, classes or methods by
changing their case. However, being a good programmer it should be
avoided.
Literals
Literals in Java are a sequence of characters (digits, letters, and other characters)
that represent constant values to be stored in variables. Java language specifies
following type of literals:
● Integer literals
● Floating Point literals
● Character literals
● String literals
● Boolean Literals
● Null literal
Operators
An operator is a symbol that takes one or more arguments and operates on
them to produce a result.
Separators
Separators are symbols used to indicate where group of code are divided and
arranged. They basically define the shape and function of our code.
i. Integer constants
● Integers are probably the most commonly used type in the typical program. Any
whole number value is an integer literal.
● Examples are 1, 2, 3, and 42.
● Octal values are denoted in Java by a leading zero.
● Examples are 025, 041, 02 etc.
● Normal decimal numbers cannot have a leading zero. Thus, the seemingly valid
value 09 will produce an error from the compiler, since 9 is outside of octal’s 0 to
7 range. A more common base for numbers used by programmers is hexadecimal,
which matches cleanly with modulo 8 word sizes, such as 8, 16, 32, and 64 bits.
● You signify a hexadecimal constant with a leading zero-x, (0x or 0X). The range
of a hexadecimal digit is 0 to 15, so A through F (or a through f) are substituted
for 10 through 15.
● Examples are 0x4A, 0x85, 0xF9 etc.
● We rarely use the octal and hexadecimal integers in a program. Integer literals
create an int value, which in Java is a 32-bit integer value.
ii. Floating-point Constants
● These are also called as real constants. Floating-point numbers represent decimal
values with a fractional component. They can be expressed in either standard or
scientific notation. Standard notation consists of a whole number component
followed by a decimal point followed by a fractional component.
● For example, 2.000, 53.19259, and 0.6667 represent valid standard-notation
floating-point numbers.
● Scientific notation uses a standard-notation, floating-point number plus a suffix
that specifies a power of 10 by which the number is to be multiplied. The
exponent is indicated by an E or e followed by a decimal number, which can be
positive or negative. It is expressed in following format:
Mantissa e exponent
● Where mantissa is either a real number expressed in decimal notation or integer.
The exponent is an integer with an optional + or – sign.
Escape Meaning
\n New Line
\t Tab
\b Back Space
\r Carriage Return
\\ Form – Feed
\’ Single Quote
\” Double Quote
Table: Escape Characters
● There is also a mechanism for directly entering the value of a character in octal or
hexadecimal. For octal notation use the backslash followed by the three-digit
number. For example, ‘\141’ is the letter ‘a’. For hexadecimal, you enter a
backslash-u (\u), then exactly four hexadecimal digits. For example, ‘\u0900’ to
‘\u97f’ is used for Devanagari character display. ‘\ua432’ is a Japanese Katakana
character.
v. Boolean Constants
● There are only two logical values that a boolean value can have i.e. true and false.
● The values of true and false do not convert into any numerical representation. The
true constant in Java does not equal 1, nor does the false constant equal 0. In Java,
they can only be assigned to variables declared as boolean, or used in expressions
with Boolean operators.
Data Types
Declaration of variable:
Syntax:
Type vatiable1, variable2 ………….. variableN;
Assignment statement
1. Variable_name = value;
2. Type variable_name = value;
Scope of variable
● Instance variables
● Class variables
● Local variables
1. Instance Variables:
Instance variables are created when the objects are instantiated and therefore they are
associated with the objects. They take different values for each objects.
2. Class Variables:
Class variable are global to s class and belong to the entire set of objects that class
creates. Only one memory location is created for each class variable.
3. Local Variables:
Variables declared and used inside methods are called local variables. They are called so
because they are not available for use outside the method definition.
Type casting:
The process of converting one data type to another is called casting or type casting. If the two
types are compatible, then java will perform the conversion automatically. It is possible to
assign an int value to long variable. However if the two types of variables are not compatible,
the type conversions are not implicitly allowed, hence the need for type casting.
Syntax:
Type variable1 = (type) variable2;
Examples:
int m = 50;
byte n = (byte) m;
long count = (long) m;
● The process of assigning a larger type into a smaller one is called narrowing.
● For some types, it is possible to assign a value of one type to a variable of a different
type without a cast. Java does the conversion of the assigned value automatically.
This is known as automatic – type conversion. It is possible if the destination type
has enough precession to store the source value.
Casting is necessary when a value of one type is to be assigned to a different type of variable.
It may also be needed when a method returns a type different than the one we require. Generic
type casting helps in the retrieval of elements from a collection as each element in a collection
is considered to be an object.
2) Relational
They are used to compare two quantities.
< , > , <= , >= , == , !=
3) Logical
The logical operator && and || are used when we want to form compound conditions by
combining two or more relations.
&& , || , !
4) Assignment
They are used to assign the value of an expression to a variable.
= , += , *= , -= , /= etc.
5) Increment / Decrement
These operators are used to increment and decrement value by 1.
It has two types:
1. Pre INC \ DEC ++a / --a
2. Post INC \ DEC a++ / a--
6) Conditional
Also called as ternary operator.
?:
Syntax:
Condition? Expression1: Expression2
It takes three arguments. The first argument is comparison argument. The second is the
result upon a true comparison and the third is the result upon a false comparison.
7) Bitwise
Java has a distinction of supporting special operators known as bitwise operators for
manipulation of data at values of bit level.
1. Bitwise NOT (~): Called bitwise complement the unary NOT operator, inverts all of
the bits of its operand.
2. Bitwise AND (&): It produce a 1 bit if both operands are also 1, a zero is produced
in all the cases.
E.g. – 0101 (decimal 5) & 0011 (decimal 3)
0001 (decimal 1)
3. Bitwise OR (|): it combines bit such that if either of the bits in the operand is a 1 then
the resultant bit is a 1.
4. Bitwise XOR (^)
5. The left shift (<<)
6. The right shift (>>)
7. Unsigned right shift (>>>>)
8) Special
a) instanceOf Operator
It is an object reference operator and returns true if the object on the left – hand
side is an instance of the class given on the right- hand side. It allows us to
determine whether the object belongs to a particular class or not.
Evaluation of Expressions:
Expressions are evaluated using assignment statement.
Variable = expression;
Example: x = a * b – c;
y= b / c * a;
Mathematical Functions:
Sr. Method Description
No.
1. static double cbrt(double arg) Returns cube root of arg
2. static double pow(double y, double x) Returns y raised to x
3. static double sqrt(double arg) Returns square root of arg
4. static int abs(int arg) Returns absolute value of arg
static long abs(long arg)
static double abs(double arg)
static float abs(float arg)
5. static int max(int arg1, int arg2) Returns maximum value between arg1 and arg2
3) If else if ladder
Syntax:
if (text expression1)
{
//Statements
}
else if (text expression 2)
//Statement
.
.
.
else if (text expression N)
//statement N
else
{
//Statement;
}
Statement – X;
4) Switch statement
switch ( expression)
{
Case value – 1:
Block 1;
break;
Case value – 2:
Block 2;
break;
……….
………
default:
Default Block;
break;
5) The ?: operator
The java language has an unusual operator, useful for making two – way decisions. This
operator is a combination of ? and : and takes three operands.
Conditional expression? Expression1: expression2;
6) While statement
Initialization;
while (test condition)
{
Body of the loop
}
7) Do statement
Initialization;
do{
Body of loop;
}while(condition);
While Do – while
It is a looping construct that will execute It is a looping construct that will execute
only if the test condition is true. at least once even if the test condition is
false.
It is an entry – controlled loop. It is an exit – controlled loop.
It is generally used for implementing It is typically used for implementing
common looping situations. menu based programs where, the menu is
required to be printed at least once.
8) For statement
for(initialization; test condition; increment/decrement)
{
Body of loop
}
With break;
Loop1:
for (………)
{
for(…..)
{
………
break Loop1;
}
}
…………..
With continue
Loop1:
for (………)
{
for(…..)
{
………
continue Loop1;
}
}