SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
Page 1 of 11
Class Notes on Basic concepts of OOP (Part - 2)
Contents:-Class & Object Overview Basic concepts of Java programming-advantages of Java, byte-code & JVM,
data types, Basic idea of inheritance, encapsulation, polymorphism, How to compile and execute Java
Programs
Class
In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as
class instances, class objects, instance objects or simply objects. A class defines constituent members which
enable its instances to have state and behavior. Data field members (member variables or instance variables)
enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of
class instances. Classes define the type of their instances.
Objects
An object is a location in memory having a value and referenced by an identifier. An object can be a variable,
function, or data structure. With the introduction of object-oriented programming the same word, "object,"
refers to a particular instance of a class.
Basic concepts of Java Programming
Java platform overview
Java technology is used to develop applications for a wide range of environments, from consumer devices to
heterogeneous enterprise systems.
Like any programming language, the Java language has its own structure, syntax rules, and programming
paradigm. The Java language's programming paradigm is based on the concept of object-oriented
programming (OOP), which the language's features support.
The Java language is a C-language derivative, so its syntax rules look much like C's: for example, code blocks
are modularized into methods and delimited by braces ({and }), and variables are declared before they are
used.
The java Compiler
When you program for the Java platform, you write source code in .java files and then compile them. The
compiler checks your code against the language's syntax rules, then writes out bytecodes in .class files.
Bytecodes are standard instructions targeted to run on a Java virtual machine (JVM). In adding this level of
abstraction, the Java compiler differs from other language compilers, which write out instructions suitable for
the CPU chipset the program will run on.
Page 2 of 11
The JVM
At run time, the JVM reads and interprets .class files and executes the program's instructions on the native
hardware platform for which the JVM was written. The JVM interprets the bytecodes just as a CPU would
interpret assembly-language instructions. The difference is that the JVM is a piece of software written
specifically for a particular platform. The JVM is the heart of the Java language's "write-once, run-anywhere"
principle. Your code can run on any chipset for which a suitable JVM implementation is available. JVMs are
available for major platforms like Linux and Windows, and subsets of the Java language have been
implemented in JVMs for mobile phones and hobbyist chips.
The garbage collector
Rather than forcing you to keep up with memory allocation the Java platform provides memory management
out of the box. When your Java application creates an object instance at run time, the JVM automatically
allocates memory space for that object from the heap, which is a pool of memory set aside for your program
to use. The Java garbage collector runs in the background, keeping track of which objects the application no
longer needs and reclaiming memory from them. This approach to memory handling is called implicit memory
management because it doesn't require you to write any memory-handling code. Garbage collection is one of
the essential features of Java platform performance.
The Java Development Kit
When you download a Java Development Kit (JDK), you get — in addition to the compiler and other tools — a
complete class library of prebuilt utilities that help you accomplish just about any task common to application
development.
The Java Runtime Environment
The Java Runtime Environment (JRE; also known as the Java runtime) includes the JVM, code libraries, and
components that are necessary for running programs written in the Java language. It is available for multiple
platforms. You can freely redistribute the JRE with your applications, according to the terms of the JRE license,
to give the application's users a platform on which to run your software. The JRE is included in the JDK.
Object-oriented programming concepts
The Java language is object-oriented. If you haven't used an object-oriented language before, its concepts
might seem strange at first. This section is a short introduction to OOP language concepts, using structured
programming as a point of contrast.
What is an object?
Structured programming languages like C and COBOL follow a very different programming paradigm from
object-oriented ones. The structured-programming paradigm is highly data-oriented, which means that you
Page 3 of 11
have data structures on one hand, and then program instructions that act on that data. Object-oriented
languages like the Java language combine data and program instructions into objects.
An object is a self-contained entity that contains attributes and behavior, and nothing more. Rather than
having a data structure with fields (attributes) and passing that structure around to all of the program logic
that acts on it (behavior), in an object-oriented language, data and program logic are combined. This
combination can occur at vastly different levels of granularity, from fine-grained objects like a Number, to
coarse-grained objects such as a Funds Transfer service in a large banking application.
Parent and child objects
A parent object is one that serves as the structural basis for deriving more-complex child objects. A child object
looks like its parent but is more specialized. The object-oriented paradigm allows you to reuse the common
attributes and behavior of the parent object, adding to its child objects attributes and behavior that differ.
Object communication and coordination
Objects talk to other objects by sending messages (method calls in the Java language). Furthermore, in an
object-oriented application, program code coordinates the activities among objects to perform tasks within
the context of the given application domain.
Object summary
A well-written object:
 Has crisp boundaries
 Does a finite set of activities
 Knows only about its data and any other objects that it needs to accomplish its activities
In essence, an object is a discrete entity that has only the necessary dependencies on other objects to perform
its tasks. Now you'll see what an object looks like.
The Person object
I'll start with an example that is based on a common application-development scenario: an individual being
represented by a Person object.
Going back to the definition of an object, you know that an object has two primary elements: attributes and
behavior. You'll see how these apply to the Person object.
Attributes
What attributes can a person have? Some common ones include:
 Name
 Age
 Height
 Weight
 Eye color
 Gender
Page 4 of 11
You can probably think of more (and you can always add more attributes later), but this list is a good start.
Behavior
An actual person can do all sorts of things, but object behaviors usually relate to some kind of application
context. In a business-application context, for instance, you might want to ask your Person object, "What is
your age?" In response, Person would tell you the value of its Age attribute.
More-complex logic could be hidden inside of the Person object, but for now suppose that Person has the
behavior of answering these questions:
 What is your name?
 What is your age?
 What is your height?
 What is your weight?
 What is your eye color?
 What is your gender?
State and string
State is an important concept in OOP. An object's state is represented at any moment in time by the value of
its attributes.
In the case of Person, its state is defined by attributes such as name, age, height, and weight. If you wanted to
present a list of several of those attributes, you might do sousing a String class, which I'll talk more about later
in the class.
Together, the concepts of state and string allow you to say to Person: tell me who you are by giving me a
listing (or String) of your attributes.
Principles of OOP using JAVA
If you come from a structured-programming background, the OOP value proposition might not be clear yet.
After all, the attributes of a person and any logic to retrieve (and convert) their values could be written in C.
This section clarifies the benefits of the OOP paradigm by explaining its defining principles: encapsulation,
inheritance, and polymorphism.
Encapsulation
Recall that an object is above all discrete, or self-contained. This is the principle of encapsulation at work.
Hiding is another term that is sometimes used to express the self-contained, protected nature of objects.
Regardless of terminology, what's important is that the object maintains a boundary between its state and
behavior, and the outside world. Like objects in the real world, objects used in computer programming have
various types of relationships with different categories of objects in the applications that use them.
On the Java platform, you can use access specifiers (which I'll introduce later in the class) to vary the nature of
object relationships from public to private. Public access is wide open, whereas private access means the
object's attributes are accessible only within the object itself.
The public/private boundary enforces the object-oriented principle of encapsulation. On the Java platform,
you can vary the strength of that boundary on an object-by-object basis, depending on a system of trust.
Encapsulation is a powerful feature of the Java language.
Inheritance
Page 5 of 11
In structured programming, it is common to copy a structure, give it a new name, and add or modify the attributes
that make the new entity (such as an Account record) different from its original source. Over time, this approach
generates a great deal of duplicated code, which can create maintenance issues.
OOP introduces the concept of inheritance, whereby specialized objects — without additional code — can "copy"
the attributes and behavior of the source objects they specialize. If some of those attributes or behaviors need to
change, then you simply override them. You only change what you need to change in order to create specialized
objects. As you know from the Object-oriented programming concepts section, the source object is called the
parent, and the new specialization is called the child.
Inheritance at work
Suppose you are writing a human-resources application and want to use the Person object as the basis for a
new object called Employee. Being the child of Person, Employee would have all of the attributes of a Person
object, along with additional ones, such as:
 Taxpayer identification number
 Hire date
 Salary
Inheritance makes it easy to create the new Employee class of the object without needing to copy all of the
Person code manually or maintain it.
You'll see plenty of examples of inheritance in Java programming later in the class and in my class notes.
Polymorphism
Polymorphism is a harder concept to grasp than encapsulation and inheritance. In essence, it means that
objects that belong to the same branch of a hierarchy, when sent the same message (that is, when told to do
the same thing), can manifest that behavior differently.
To understand how polymorphism applies to a business-application context, return to the Person example.
Remember telling Person to format its attributes into a String? Polymorphism makes it possible for Person to
represent its attributes in a variety of ways depending on the type of Person it is.
Polymorphism is one of the more complex concepts you'll encounter in OOP on the Java platform and not
within the scope of an introductory tutorial.
Advantages of JAVA
JAVA offers a number of advantages to developers.
Java is simple: Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn
than other programming languages. The reason that why Java is much simpler than C++ is because Java uses
automatic memory allocation and garbage collection where else C++ requires the programmer to allocate
memory and to collect garbage.
Java is object-oriented: Java is object-oriented because programming in Java is centered on creating objects,
manipulating objects, and making objects work together. This allows you to create modular programs and
reusable code.
Page 6 of 11
Java is platform-independent: One of the most significant advantages of Java is its ability to move easily from
one computer system to another.
The ability to run the same program on many different systems is crucial to World Wide Web software, and
Java succeeds at this by being platform-independent at both the source and binary levels.
Java is distributed: Distributed computing involves several computers on a network working together. Java is
designed to make distributed computing easy with the networking capability that is inherently integrated into
it.
Writing network programs in Java is like sending and receiving data to and from a file. For example, the
diagram below shows three programs running on three different systems, communicating with each other to
perform a joint task.
Java is interpreted: An interpreter is needed in order to run Java programs. The programs are compiled into
Java Virtual Machine code called bytecode.
The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With
Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on
any platform.
Java is secure: Java is one of the first programming languages to consider security as part of its design. The
Java language, compiler, interpreter, and runtime environment were each developed with security in mind.
Java is robust: Robust means reliable and no programming language can really assure reliability. Java puts a
lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that
would first show up during execution time in other languages.
Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously
within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other
languages, operating system-specific procedures have to be called in order to enable multithreading.
Multithreading is a necessity in visual and network programming.
Disadvantages of JAVA
Performance: Java can be perceived as significantly slower and more memory-consuming than natively
compiled languages such as C or C++.
Look and feel: The default look and feel of GUI applications written in Java using the Swing toolk it is very
different from native applications. It is possible to specify a different look and feel through the pluggable look
and feel system of Swing.
Java bytecode
Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one
byte in length, although some require parameters, resulting in some multi-byte instructions. A Java
programmer does not need to be aware of or understand Java bytecode at all.
Page 7 of 11
Java virtual machine
A Java virtual machine (JVM) is a virtual machine that can execute Java bytecode. It is the code execution
component of the Java platform. Sun Microsystems has stated that there are over 5.5 billion JVM-enabled
devices.
A Java virtual machine is a program which executes certain other programs, namely those containing Java
bytecode instructions. JVMs are most often implemented to run on an existing operating system, but can also
be implemented to run directly on hardware. A JVM provides a run-time environment in which Java bytecode
can be executed, enabling features such as automated exception handling, which provides root-cause
debugging information for every software error (exception). A JVM is distributed along with Java Class Library,
a set of standard class libraries (in Java bytecode) that implement the Java application programming interface
(API). These libraries, bundled together with the JVM, form the Java Runtime Environment (JRE).
JVMs are available for many hardware and software platforms. The use of the same bytecode for all JVMs on
all platforms allows Java to be described as a write once, run anywhere programming language, versus write
once, compile anywhere, which describes cross-platform compiled languages. Thus, the JVM is a crucial
component of the Java platform.
Java bytecode is an intermediate language which is typically compiled from Java, but it can also be compiled
from other programming languages. For example, Ada source code can be compiled to Java bytecode and
executed on a JVM.
Java Basic Data Types
Variables are nothing but reserved memory locations to store values. This means that when you create a
variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored
in the reserved memory. Therefore, by assigning different data types to variables, you can store integers,
decimals, or characters in these variables.
There are two data types available in Java:
 Primitive Data Types
 Reference/Object Data Types
Primitive Data Types
There are eight primitive data types supported by Java. Primitive data types are predefined by the language
and named by a key word. Let us now look into detail about the eight primitive data types.
byte:
 Byte data type is a 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
Page 8 of 11
 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)
 Maximum value is 32,767(inclusive) (2^15 -1)
 Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an
int
 Default value is 0.
 Example : short s= 10000 , short r = -20000
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 : int a = 100000L, int b = -200000L
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.
Page 9 of 11
 Example : double d1 = 123.4
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.
 Example : boolean one = true
char:
 char data type is a single 16-bit Unicode character.
 Minimum value is 'u0000' (or 0).
 Maximum value is 'uffff' (or 65,535 inclusive).
 Char data type is used to store any character.
 Example . char letterA ='A'
Reference Data Types:
 Reference variables are created using defined constructors of the classes. They are used to access
objects. These variables are declared to be of a specific type that cannot be changed. For example,
Employee, Puppy etc.
 Class objects, and various type of array variables come under reference data type.
 Default value of any reference variable is null.
 A reference variable can be used to refer to any object of the declared type or any compatible type.
 Example : Animal animal = new Animal("giraffe");
Java Literals:
A literal is a source code representation of a fixed value. They are represented directly in the code without any
computation.
Literals can be assigned to any primitive type variable. For example:
byte a = 68;
char a = 'A'
byte, int, long, and short can be expressed in decimal(base 10),hexadecimal(base 16) or octal(base 8) number
systems as well.
Prefix 0 is used to indicates octal and prefix 0x indicates hexadecimal when using these number systems for
literals. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String literals in Java are specified like they are in most other languages by enclosing a sequence of characters
between a pair of double quotes. Examples of string literals are:
Page 10 of 11
"Hello World"
"twonlines"
""This is in quotes""
String and char types of literals can contain any Unicode characters. For example:
char a = 'u0001';
String a = "u0001";
Java language supports few special escape sequences for String and char literals as well. They are:
Notation Character represented
n Newline (0x0a)
r Carriage return (0x0d)
f Formfeed (0x0c)
b Backspace (0x08)
s Space (0x20)
t tab
" Double quote
' Single quote
 backslash
ddd Octal character (ddd)
uxxxx Hexadecimal UNICODE character (xxxx)
A Simple Java Program
Now that the basic object-oriented underpinning of Java has been discussed, let’s look at some actual Java
programs. Let’s start by compiling and running the short sample program shown here. As you will see, this
involves a little more work than you might imagine.
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
About the Program
For most computer languages, the name of the file that holds the source code to a program is arbitrary.
However, this is not the case with Java. The first thing that you must learn about Java is that the name you
give to a source file is very important. For this example, the name of the source file should be Example.java.
Let’s see why? In Java, a source file is officially called a compilation unit. It is a text file that contains one or
more class definitions. The Java compiler requires that a source file use the .java filename extension. Notice
that the file extension is four characters long. As you might guess, your operating system must be capable of
supporting long filenames. This means that DOS and Windows 3.1 are not capable of supporting Java.
However, Windows 95/98 and Windows NT/2000/XP work just fine. As you can see by looking at the program,
Page 11 of 11
the name of the class defined by the program is also Example. This is not a coincidence. In Java, all code must
reside inside a class. By convention, the name of that class should match the name of the file that holds the
program. You should also make sure that the capitalization of the filename matches the class name. The
reason for this is that Java is case-sensitive. At this point, the convention that filenames correspond to class
names may seem arbitrary. However, this convention makes it easier to maintain and organize your programs.
Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of the source file on the
command line, as shown here:
C:>javac Example.java
The javac compiler creates a file called Example.class that contains the bytecode version of the program. As
discussed earlier, the Java bytecode is the intermediate representation of your program that contains
instructions the Java interpreter will execute. Thus, the output of javac is not code that can be directly
executed. To actually run the program, you must use the Java interpreter, called java. To do so, pass the class
name Example as a command-line argument, as shown here:
C:>java Example
When the program is run, the following output is displayed:
This is a simple Java program.
When Java source code is compiled, each individual class is put into its own output file named after the class
and using the .class extension. This is why it is a good idea to give your Java source files the same name as the
class they contain—the name of the source file will match the name of the .class file. When you execute the
Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter
to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it
will execute the code contained in the specified class.

Contenu connexe

Tendances

Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
farhan amjad
 
OOP programming
OOP programmingOOP programming
OOP programming
anhdbh
 

Tendances (20)

Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
OOP Java
OOP JavaOOP Java
OOP Java
 
Ah java-ppt2
Ah java-ppt2Ah java-ppt2
Ah java-ppt2
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
Unit 1 OOSE
Unit 1 OOSE Unit 1 OOSE
Unit 1 OOSE
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1
 
Java features
Java  features Java  features
Java features
 
Oops
OopsOops
Oops
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programming
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
OOP programming
OOP programmingOOP programming
OOP programming
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
 
General OOP Concepts
General OOP ConceptsGeneral OOP Concepts
General OOP Concepts
 

Similaire à Class notes(week 2) on basic concepts of oop-2

Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
BalamuruganV28
 
Unit1 jaava
Unit1 jaavaUnit1 jaava
Unit1 jaava
mrecedu
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
Komal Singh
 
Object-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdfObject-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdf
Bharath Choudhary
 

Similaire à Class notes(week 2) on basic concepts of oop-2 (20)

Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
 
Unit1 jaava
Unit1 jaavaUnit1 jaava
Unit1 jaava
 
Cs8392 oops 5 units notes
Cs8392 oops 5 units notes Cs8392 oops 5 units notes
Cs8392 oops 5 units notes
 
Java chapter 3 - OOPs concepts
Java chapter 3 - OOPs conceptsJava chapter 3 - OOPs concepts
Java chapter 3 - OOPs concepts
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
MCA NOTES.pdf
MCA NOTES.pdfMCA NOTES.pdf
MCA NOTES.pdf
 
Java PPT
Java PPTJava PPT
Java PPT
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
Java1
Java1Java1
Java1
 
Java
Java Java
Java
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.ppt
 
Object-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdfObject-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdf
 
Introduction to OOP.pptx
Introduction to OOP.pptxIntroduction to OOP.pptx
Introduction to OOP.pptx
 
Java pdf
Java   pdfJava   pdf
Java pdf
 
Unit1 introduction to Java
Unit1 introduction to JavaUnit1 introduction to Java
Unit1 introduction to Java
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 

Plus de Kuntal Bhowmick

Plus de Kuntal Bhowmick (20)

Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loopsMultiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
 
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
1. introduction to E-commerce
1. introduction to E-commerce1. introduction to E-commerce
1. introduction to E-commerce
 
Computer graphics question for exam solved
Computer graphics question for exam solvedComputer graphics question for exam solved
Computer graphics question for exam solved
 
DBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsDBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental concepts
 
Operating system Interview Questions
Operating system Interview QuestionsOperating system Interview Questions
Operating system Interview Questions
 
Computer Network Interview Questions
Computer Network Interview QuestionsComputer Network Interview Questions
Computer Network Interview Questions
 
C interview questions
C interview  questionsC interview  questions
C interview questions
 
C question
C questionC question
C question
 
Distributed operating systems cs704 a class test
Distributed operating systems cs704 a class testDistributed operating systems cs704 a class test
Distributed operating systems cs704 a class test
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
CS291(C Programming) assignment
CS291(C Programming) assignmentCS291(C Programming) assignment
CS291(C Programming) assignment
 
C programming guide new
C programming guide newC programming guide new
C programming guide new
 

Dernier

Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Dernier (20)

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 

Class notes(week 2) on basic concepts of oop-2

  • 1. Page 1 of 11 Class Notes on Basic concepts of OOP (Part - 2) Contents:-Class & Object Overview Basic concepts of Java programming-advantages of Java, byte-code & JVM, data types, Basic idea of inheritance, encapsulation, polymorphism, How to compile and execute Java Programs Class In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior. Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes define the type of their instances. Objects An object is a location in memory having a value and referenced by an identifier. An object can be a variable, function, or data structure. With the introduction of object-oriented programming the same word, "object," refers to a particular instance of a class. Basic concepts of Java Programming Java platform overview Java technology is used to develop applications for a wide range of environments, from consumer devices to heterogeneous enterprise systems. Like any programming language, the Java language has its own structure, syntax rules, and programming paradigm. The Java language's programming paradigm is based on the concept of object-oriented programming (OOP), which the language's features support. The Java language is a C-language derivative, so its syntax rules look much like C's: for example, code blocks are modularized into methods and delimited by braces ({and }), and variables are declared before they are used. The java Compiler When you program for the Java platform, you write source code in .java files and then compile them. The compiler checks your code against the language's syntax rules, then writes out bytecodes in .class files. Bytecodes are standard instructions targeted to run on a Java virtual machine (JVM). In adding this level of abstraction, the Java compiler differs from other language compilers, which write out instructions suitable for the CPU chipset the program will run on.
  • 2. Page 2 of 11 The JVM At run time, the JVM reads and interprets .class files and executes the program's instructions on the native hardware platform for which the JVM was written. The JVM interprets the bytecodes just as a CPU would interpret assembly-language instructions. The difference is that the JVM is a piece of software written specifically for a particular platform. The JVM is the heart of the Java language's "write-once, run-anywhere" principle. Your code can run on any chipset for which a suitable JVM implementation is available. JVMs are available for major platforms like Linux and Windows, and subsets of the Java language have been implemented in JVMs for mobile phones and hobbyist chips. The garbage collector Rather than forcing you to keep up with memory allocation the Java platform provides memory management out of the box. When your Java application creates an object instance at run time, the JVM automatically allocates memory space for that object from the heap, which is a pool of memory set aside for your program to use. The Java garbage collector runs in the background, keeping track of which objects the application no longer needs and reclaiming memory from them. This approach to memory handling is called implicit memory management because it doesn't require you to write any memory-handling code. Garbage collection is one of the essential features of Java platform performance. The Java Development Kit When you download a Java Development Kit (JDK), you get — in addition to the compiler and other tools — a complete class library of prebuilt utilities that help you accomplish just about any task common to application development. The Java Runtime Environment The Java Runtime Environment (JRE; also known as the Java runtime) includes the JVM, code libraries, and components that are necessary for running programs written in the Java language. It is available for multiple platforms. You can freely redistribute the JRE with your applications, according to the terms of the JRE license, to give the application's users a platform on which to run your software. The JRE is included in the JDK. Object-oriented programming concepts The Java language is object-oriented. If you haven't used an object-oriented language before, its concepts might seem strange at first. This section is a short introduction to OOP language concepts, using structured programming as a point of contrast. What is an object? Structured programming languages like C and COBOL follow a very different programming paradigm from object-oriented ones. The structured-programming paradigm is highly data-oriented, which means that you
  • 3. Page 3 of 11 have data structures on one hand, and then program instructions that act on that data. Object-oriented languages like the Java language combine data and program instructions into objects. An object is a self-contained entity that contains attributes and behavior, and nothing more. Rather than having a data structure with fields (attributes) and passing that structure around to all of the program logic that acts on it (behavior), in an object-oriented language, data and program logic are combined. This combination can occur at vastly different levels of granularity, from fine-grained objects like a Number, to coarse-grained objects such as a Funds Transfer service in a large banking application. Parent and child objects A parent object is one that serves as the structural basis for deriving more-complex child objects. A child object looks like its parent but is more specialized. The object-oriented paradigm allows you to reuse the common attributes and behavior of the parent object, adding to its child objects attributes and behavior that differ. Object communication and coordination Objects talk to other objects by sending messages (method calls in the Java language). Furthermore, in an object-oriented application, program code coordinates the activities among objects to perform tasks within the context of the given application domain. Object summary A well-written object:  Has crisp boundaries  Does a finite set of activities  Knows only about its data and any other objects that it needs to accomplish its activities In essence, an object is a discrete entity that has only the necessary dependencies on other objects to perform its tasks. Now you'll see what an object looks like. The Person object I'll start with an example that is based on a common application-development scenario: an individual being represented by a Person object. Going back to the definition of an object, you know that an object has two primary elements: attributes and behavior. You'll see how these apply to the Person object. Attributes What attributes can a person have? Some common ones include:  Name  Age  Height  Weight  Eye color  Gender
  • 4. Page 4 of 11 You can probably think of more (and you can always add more attributes later), but this list is a good start. Behavior An actual person can do all sorts of things, but object behaviors usually relate to some kind of application context. In a business-application context, for instance, you might want to ask your Person object, "What is your age?" In response, Person would tell you the value of its Age attribute. More-complex logic could be hidden inside of the Person object, but for now suppose that Person has the behavior of answering these questions:  What is your name?  What is your age?  What is your height?  What is your weight?  What is your eye color?  What is your gender? State and string State is an important concept in OOP. An object's state is represented at any moment in time by the value of its attributes. In the case of Person, its state is defined by attributes such as name, age, height, and weight. If you wanted to present a list of several of those attributes, you might do sousing a String class, which I'll talk more about later in the class. Together, the concepts of state and string allow you to say to Person: tell me who you are by giving me a listing (or String) of your attributes. Principles of OOP using JAVA If you come from a structured-programming background, the OOP value proposition might not be clear yet. After all, the attributes of a person and any logic to retrieve (and convert) their values could be written in C. This section clarifies the benefits of the OOP paradigm by explaining its defining principles: encapsulation, inheritance, and polymorphism. Encapsulation Recall that an object is above all discrete, or self-contained. This is the principle of encapsulation at work. Hiding is another term that is sometimes used to express the self-contained, protected nature of objects. Regardless of terminology, what's important is that the object maintains a boundary between its state and behavior, and the outside world. Like objects in the real world, objects used in computer programming have various types of relationships with different categories of objects in the applications that use them. On the Java platform, you can use access specifiers (which I'll introduce later in the class) to vary the nature of object relationships from public to private. Public access is wide open, whereas private access means the object's attributes are accessible only within the object itself. The public/private boundary enforces the object-oriented principle of encapsulation. On the Java platform, you can vary the strength of that boundary on an object-by-object basis, depending on a system of trust. Encapsulation is a powerful feature of the Java language. Inheritance
  • 5. Page 5 of 11 In structured programming, it is common to copy a structure, give it a new name, and add or modify the attributes that make the new entity (such as an Account record) different from its original source. Over time, this approach generates a great deal of duplicated code, which can create maintenance issues. OOP introduces the concept of inheritance, whereby specialized objects — without additional code — can "copy" the attributes and behavior of the source objects they specialize. If some of those attributes or behaviors need to change, then you simply override them. You only change what you need to change in order to create specialized objects. As you know from the Object-oriented programming concepts section, the source object is called the parent, and the new specialization is called the child. Inheritance at work Suppose you are writing a human-resources application and want to use the Person object as the basis for a new object called Employee. Being the child of Person, Employee would have all of the attributes of a Person object, along with additional ones, such as:  Taxpayer identification number  Hire date  Salary Inheritance makes it easy to create the new Employee class of the object without needing to copy all of the Person code manually or maintain it. You'll see plenty of examples of inheritance in Java programming later in the class and in my class notes. Polymorphism Polymorphism is a harder concept to grasp than encapsulation and inheritance. In essence, it means that objects that belong to the same branch of a hierarchy, when sent the same message (that is, when told to do the same thing), can manifest that behavior differently. To understand how polymorphism applies to a business-application context, return to the Person example. Remember telling Person to format its attributes into a String? Polymorphism makes it possible for Person to represent its attributes in a variety of ways depending on the type of Person it is. Polymorphism is one of the more complex concepts you'll encounter in OOP on the Java platform and not within the scope of an introductory tutorial. Advantages of JAVA JAVA offers a number of advantages to developers. Java is simple: Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. The reason that why Java is much simpler than C++ is because Java uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage. Java is object-oriented: Java is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.
  • 6. Page 6 of 11 Java is platform-independent: One of the most significant advantages of Java is its ability to move easily from one computer system to another. The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels. Java is distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it. Writing network programs in Java is like sending and receiving data to and from a file. For example, the diagram below shows three programs running on three different systems, communicating with each other to perform a joint task. Java is interpreted: An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called bytecode. The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform. Java is secure: Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind. Java is robust: Robust means reliable and no programming language can really assure reliability. Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages. Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other languages, operating system-specific procedures have to be called in order to enable multithreading. Multithreading is a necessity in visual and network programming. Disadvantages of JAVA Performance: Java can be perceived as significantly slower and more memory-consuming than natively compiled languages such as C or C++. Look and feel: The default look and feel of GUI applications written in Java using the Swing toolk it is very different from native applications. It is possible to specify a different look and feel through the pluggable look and feel system of Swing. Java bytecode Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. A Java programmer does not need to be aware of or understand Java bytecode at all.
  • 7. Page 7 of 11 Java virtual machine A Java virtual machine (JVM) is a virtual machine that can execute Java bytecode. It is the code execution component of the Java platform. Sun Microsystems has stated that there are over 5.5 billion JVM-enabled devices. A Java virtual machine is a program which executes certain other programs, namely those containing Java bytecode instructions. JVMs are most often implemented to run on an existing operating system, but can also be implemented to run directly on hardware. A JVM provides a run-time environment in which Java bytecode can be executed, enabling features such as automated exception handling, which provides root-cause debugging information for every software error (exception). A JVM is distributed along with Java Class Library, a set of standard class libraries (in Java bytecode) that implement the Java application programming interface (API). These libraries, bundled together with the JVM, form the Java Runtime Environment (JRE). JVMs are available for many hardware and software platforms. The use of the same bytecode for all JVMs on all platforms allows Java to be described as a write once, run anywhere programming language, versus write once, compile anywhere, which describes cross-platform compiled languages. Thus, the JVM is a crucial component of the Java platform. Java bytecode is an intermediate language which is typically compiled from Java, but it can also be compiled from other programming languages. For example, Ada source code can be compiled to Java bytecode and executed on a JVM. Java Basic Data Types Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two data types available in Java:  Primitive Data Types  Reference/Object Data Types Primitive Data Types There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the eight primitive data types. byte:  Byte data type is a 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
  • 8. Page 8 of 11  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)  Maximum value is 32,767(inclusive) (2^15 -1)  Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int  Default value is 0.  Example : short s= 10000 , short r = -20000 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 : int a = 100000L, int b = -200000L 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.
  • 9. Page 9 of 11  Example : double d1 = 123.4 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.  Example : boolean one = true char:  char data type is a single 16-bit Unicode character.  Minimum value is 'u0000' (or 0).  Maximum value is 'uffff' (or 65,535 inclusive).  Char data type is used to store any character.  Example . char letterA ='A' Reference Data Types:  Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.  Class objects, and various type of array variables come under reference data type.  Default value of any reference variable is null.  A reference variable can be used to refer to any object of the declared type or any compatible type.  Example : Animal animal = new Animal("giraffe"); Java Literals: A literal is a source code representation of a fixed value. They are represented directly in the code without any computation. Literals can be assigned to any primitive type variable. For example: byte a = 68; char a = 'A' byte, int, long, and short can be expressed in decimal(base 10),hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicates octal and prefix 0x indicates hexadecimal when using these number systems for literals. For example: int decimal = 100; int octal = 0144; int hexa = 0x64; String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are:
  • 10. Page 10 of 11 "Hello World" "twonlines" ""This is in quotes"" String and char types of literals can contain any Unicode characters. For example: char a = 'u0001'; String a = "u0001"; Java language supports few special escape sequences for String and char literals as well. They are: Notation Character represented n Newline (0x0a) r Carriage return (0x0d) f Formfeed (0x0c) b Backspace (0x08) s Space (0x20) t tab " Double quote ' Single quote backslash ddd Octal character (ddd) uxxxx Hexadecimal UNICODE character (xxxx) A Simple Java Program Now that the basic object-oriented underpinning of Java has been discussed, let’s look at some actual Java programs. Let’s start by compiling and running the short sample program shown here. As you will see, this involves a little more work than you might imagine. /* This is a simple Java program. Call this file "Example.java". */ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } } About the Program For most computer languages, the name of the file that holds the source code to a program is arbitrary. However, this is not the case with Java. The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be Example.java. Let’s see why? In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension. Notice that the file extension is four characters long. As you might guess, your operating system must be capable of supporting long filenames. This means that DOS and Windows 3.1 are not capable of supporting Java. However, Windows 95/98 and Windows NT/2000/XP work just fine. As you can see by looking at the program,
  • 11. Page 11 of 11 the name of the class defined by the program is also Example. This is not a coincidence. In Java, all code must reside inside a class. By convention, the name of that class should match the name of the file that holds the program. You should also make sure that the capitalization of the filename matches the class name. The reason for this is that Java is case-sensitive. At this point, the convention that filenames correspond to class names may seem arbitrary. However, this convention makes it easier to maintain and organize your programs. Compiling the Program To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: C:>javac Example.java The javac compiler creates a file called Example.class that contains the bytecode version of the program. As discussed earlier, the Java bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute. Thus, the output of javac is not code that can be directly executed. To actually run the program, you must use the Java interpreter, called java. To do so, pass the class name Example as a command-line argument, as shown here: C:>java Example When the program is run, the following output is displayed: This is a simple Java program. When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. This is why it is a good idea to give your Java source files the same name as the class they contain—the name of the source file will match the name of the .class file. When you execute the Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class.