Documente Academic
Documente Profesional
Documente Cultură
Assistant Professor
Explanation of features:
i. Simple:
Java is Easy to write and more readable and eye catching.
Java has a concise, cohesive set of features that makes it easy to learn and use.
Most of the concepts are drew from C++ thus making Java learning simpler.
ii. Security: Explained in Page No 2
iii. Portability: Explained in Page No 3
iv. Object-oriented
Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behaviour.
Object-oriented programming(OOPs) is a methodology that simplify
software development and maintenance by providing some rules.
Basic concepts of OOPs are:
Objects
Classes
Inheritance
Polymorphism
Abstraction
Encapsulation
v. Robust:
Robust simply means strong. Java uses strong memory management. There are
lack of pointers that avoids security problem. There is automatic garbage
collection in java. There is exception handling and type checking mechanism
in java. All these points makes java robust.
vi. Multithreaded
A thread is like a separate program, executing concurrently. We can write Java
programs that deal with many tasks at once by defining multiple threads. The
main advantage of multi-threading is that it shares the same memory. Threads
are important for multi-media, Web applications etc.
vii. Architecture-neutral
Java is not tied to a specific machine or operating system architecture.
Machine Independent i.e Java is independent of hardware.
viii. Interpreted
Java supports cross-platform code through the use of Java bytecode.
Bytecode can be interpreted on any platform by JVM.
ix. High performance
Platform is the hardware or software environment in which a program runs. There are two
types of platforms software-based and hardware-based. Java provides software-based
platform. The java platform differs from most other platforms in the sense that it's a
software-based platform that runs on top of other hardware-based platforms.it has two
components:
1. Runtime environment
One way to think about encapsulation is as a protective wrapper that prevents the
code and data from being arbitrarily accessed by other code defined outside the
wrapper.
Access to the code and data inside the wrapper is tightly controlled through a well-
defined interface.
A class defines the structure and behavior (data and code) that will be shared by a
set of objects. Each object of a given class contains the structure and behavior
defined by the class, as if it were stamped out by a mold in the shape of the class.
For this reason, objects are sometimes referred to as instances of a class. Thus, a
class is a logical construct; an object has physical reality.
When you create a class, you will specify the code and data that constitute that
class. Collectively, these elements are called members of the class. Specifically, the
data defined by the class are referred to as member variables or instance variables.
The code that operates on that data is referred to as member methods or just
methods.
The public interface of a class represents everything that external users of the class
need to know, or may know. The private methods and data can only be accessed by
code that is a member of the class.
In Java terms, we would say that Animal, Dog, and Cat are all classes
as they have common characteristics.
Animal is the base class, superclass, or parent class as it represents the
animals in an abstract way that they have some attributes, such as size,
intelligence, and type of skeletal system. Animals also have certain behavioural
aspects; they eat, breathe, and sleep. This description of attributes and behavior
is the class definition for animals. Dog and Cat are derived classes, subclasses,
or child classes as they all describe a more specific class of animals, they would
have more specific attributes, such as type of teeth, and eyes. This is known as a
subclass of animals.
iii. Polymorphism:
Polymorphism is the ability of an object to take on many forms. The most common use
of polymorphism in OOP occurs when a parent class reference is used to refer to a
child class object.
When one task is performed by different ways i.e. known as polymorphism. For
example: to convense the students regarding shapes it’s necessary to draw different
shapes.So in this example shape is an object which is described in multiple forms.
Java program can be defined as a collection of objects that communicate via invoking
each other's methods.
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
Basic Structure of java program :
Class name:
braces are called member of the class. And note that all the program activities are
defined inside the class.
For all class names the first letter should be in Upper Case. If several words
are used to form a name of the class, each inner word's first letter should be in
Upper Case.
Comment Lines:
This line begins with main method as like functions or subroutines in other
languages. The program will start execute by calling this main method. Let us
see briefly about the other attributes declared in main method.
The keyword public is an access specifier.
The keyword static is a kind of modifier.
The keyword void means that the method main() does not return any value.
All the java program will start execute by calling the main method. In a
main() method there is only one parameter ,String args[] . args[] is a name of
the parameter that is an array of the objects of data type String. String
store sequences of characters and args will receive the command line
arguments.
All the method in java must be start with opening curly brace ({) and ends with
closing curly brace (}).
Body of the method:
a. 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.
b. INTEGERS: 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.
Byte:
Byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
Example: byte a = 100 , byte b = -50
short:
Short data type is a 16-bit signed two's complement integer.
Minimum value is -32,768 (-2^15)
int:
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Int is generally used as the default data type for integral values unless there is
a concern about memory.
The default value is 0.
Example: int a = 100000, int b = -200000
long:
Long data type is a 64-bit signed two's complement integer.
Minimum value is -9,223,372,036,854,775,808.(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
This type is used when a wider range than int is needed.
Default value is 0L.
Example: long a = 100000L, long b = -200000L
Example:
}
This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.
c. Floating point numbers:
Floating-point numbers, also known as real numbers, are used when evaluating expressions that
require fractional precision.
For example, calculations such as square root, or transcendentals such as sine and cosine, result in a
value whose precision requires a floating-point type.
Java implements the standard (IEEE–754) set of floating-point types and operators.
There are two kinds of floating-point types, float and double, which represent single- and double-
precision numbers, respectively. Their width and ranges are shown here:
Float:
Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point numbers.
Default value is 0.0f.
Float data type is never used for precise values such as currency.
Example: float f1 = 234.5f
double:
Double data type is a double-precision 64-bit IEEE 754 floating point.
This data type is generally used as the default data type for decimal values,
generally the default choice.
Double data type should never be used for precise values such as currency.
Default value is 0.0d.
Example: double d1 = 123.4
C.2. CHARACTER
Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative
chars. The standard set of characters known as ASCII still ranges from 0 to 127 as
always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
Char data type is a single 16-bit Unicode character.
Minimum value is '\u0000' (or 0).
C.3. BOOLEAN
Boolean data type represents one bit of information.
There are only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false.
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. Here is a program
that demonstrates the boolean type:
Whitespace : Java is a free-form language. This means that you do not need to
follow any special indentation rules.
Identifiers: Identifiers are used for class names, method names, and variable names.
An identifier may be any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters. They must not begin with a
number, lest they be confused with a numeric literal. Again, Java is case-sensitive, so
VALUE is a different identifier than Value.
Literals: A constant value in Java is created by using a literal representation of it. For
example, here are some literals:
Left to right, the first literal specifies an integer, the next is a floating-point value, the
third is a character constant, and the last is a string. Aliteral can be used anywhere a
value of its type is allowed.
Comments:
There are three types of comments defined by Java. You have already seen two:
single-line and multiline. The third type is called a documentation comment. This type
of comment is used to produce an HTMLfile that documents your program. The
documentation comment begins with a /** and ends with a */.
Separators :
In Java, there are a few characters that are used as separators. The most commonly
used separator in Java is the semicolon. As you have seen, it is used to terminate
statements. The separators are shown in the following table:
Java Keywords:
There are 50 keywords currently defined in the Java language.
These keywords, combined with the syntax of the operators and separators, form the
foundation of the Java language. These keywords cannot be used as names for a
variable, class, or method.
To create an array, you first must create an array variable of the desired type. The general
form of a one-dimensional array declaration is
type var-name[ ];
Here, type declares the base type of the array. The base type determines the data
type
of each element that comprises the array. Thus, the base type for the array
determines what type of data the array will hold.
Example:
The following declares an array named month_days with the type “array of int”:
int month_days[];
Multi-dimensional arrays:
In Java, multidimensional arrays are actually arrays of arrays.
Declaration and instantiation of multidimensional arrays:
To declare a multidimensional array variable, specify each additional index using
another set of square brackets.
New is the special operator used to allocate memory for multidimensional arrays.
Example:
The following declares a two dimensional array variable called twoD.
int twoD[][] = new int[4][5];