15CS45 OOC Notes Module 2 PDF

Title 15CS45 OOC Notes Module 2
Author Chandrashekhar kuri
Course Java and j2ee
Institution Visvesvaraya Technological University
Pages 34
File Size 656.6 KB
File Type PDF
Total Downloads 35
Total Views 120

Summary

• Java was developed by Sun Microsystems Inc. in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It took 18 months to develop the first working version. (The initial name was Oak but it was renamed to Java in 1995 as OAK was a registered trademark ...


Description

Module 1 Overview of Java 

Java is an object-oriented programming language with its own runtime environment.



Java is a programming language and a platform. Java is a high level, robust, secured and object-oriented programming language.  Platform: Any hardware or software environment in which a program runs is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform.



Java was developed by Sun Microsystems Inc. in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It took 18 months to develop the first working version. (The initial name was Oak but it was renamed to Java in 1995 as OAK was a registered trademark of another Tech company).



Java programs are platform independent which means they can be run on any operating system with any type of processor as long as the Java interpreter is available on that system.  A Java interpreter executes lines of byte code as commands to be executed. The byte code is executed. The JVM takes the byte code and generates machine code. The byte code is compiled to machine code, and the machine code is executed.



Java code that runs on one platform does not need to be recompiled to run on another platform, it’s called “Write Once, Run Anywhere” (WORA).



Java Virtual Machine (JVM) executes Java code, but is written in platform specific languages such as C/C++/ASM etc. JVM is not written in Java and hence cannot be platform independent and Java interpreter is actually a part of JVM.



The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.

History of Java Technology? 

History of Java programming language is usually associated with origin predates the web.



James Gosling, Patrick Naughton, Chris Warth, Mike Sheridan and Ed Frank initiated the Java language project in June 1991. The idea was to develop a language which was platform-independent and which could create embedded software for consumer electronic devices. The language took 18 months to develop and had an initial name as “Oak” which was renamed to Java in 1995, due to copyright issues. Originally, developed by James Gosling at Sun Microsystems (which has since merge into Oracle Corporation) and released in 1995. JDK 1.0 released In January 23, 1996.

Types of Java Applications 

Web Application: Java is used to create server-side web applications. Currently, servlets, jsp, struts, jsf etc. technologies are used.



Standalone Application: It is also known as desktop application or windowbased application. An application that need to be installed on every machine or server such as media player, antivirus etc. AWT and Swing are used in Java for creating standalone application.



Enterprise Application: An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.



Mobile Application: Java is used to create application for mobile devices. Currently Java ME is used for creating applications for small devices.

Features of Java 

Object Oriented – In java everything is an Object. Object oriented programming is a way of organizing programs as collection of objects, each of which represents an instance of a class.



Platform independent –During compilation, the compiler converts java program to its byte code. This byte code can run on any platform such as

Windows, Linux, Mac / OS etc. which mean a program that is compiled on windows can run on Linux and vice-versa. This is why java is known as platform independent language. 

Easy to learn – Java is simple because, syntax is based on C or C++.



Secure – It provides a virtual firewall between the application and the computer. Java codes are confined within Java Runtime Environment (JRE). Thus, it does not grant unauthorized access on the system resources.



Architectural-neutral – Java compiler generates an architecture- neutral object file format which makes the compiled code to be executable on many processors, with the presence of java runtime system.



Portable – Java code that is written on one machine can run on another machine. The platform independent byte code can be carried to any platform for execution that makes java code portable.

 Multi-threaded – With Java’s multi-threaded feature it is enabled a program to perform several tasks simultaneously. 

High Performance – With the use of Just-In-Time compilers Java enables high performance.

Basic Concepts of Object Oriented Programming Object Oriented Programming Language is methodology that simplifies software development

and

maintenance

using

concepts class,

Object,

Abstraction,

Inheritance, Polymorphism and Encapsulation:

Object  An object is anything that really exists in the world and can be distinguished from others.

 For example, every human being, a book, a tree, and so on 

Every object has properties and exhibits certain behavior.



Taking an example of dog, it has properties like name, height; color, 'age, etc. These properties are represented by variables.

 Now, the object dog will have some actions like running, barking, eating, etc. These actions are represented by various methods in our programming

Class 

A group of objects exhibiting same behavior (properties + actions) will come under the same group called as a class.



Collection of objects is called class. It is a logical entity.



A class can also be defined as a blueprint from which you can create an individual object.

Abstraction A process of identifying the essential details to be known and ignoring the non essential details from the perspective of end user Two Types 1. Partial Abstraction: This can be achieved by using abstract class 2. Full Abstraction: This can be achieved by using interface

Inheritance When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism  If one task is performed in different ways, it is known as polymorphism.  To achieve polymorphism we are using Method Overloading (Compile Time Polymorphism) and Method Overriding (Run Time Polymorphism)

Encapsulation Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines

Environment Setup and Java Program Structure Before Installation and setting up of environment variable, let us know about Java SE Development Kit (JDK). It contains 1. JDK (Java Development Kit) 2. JRE (Java Run Time Environment) 3. JVM (Java Virtual Machine) Java Development Kit (JDK)  JDK contains everything that will be required to develop and run Java application.  JDK includes a complete JRE (Java Runtime Environment) plus tools for Developing, Debugging, and Monitoring Java applications. JDK is needed to develop Java applications and Applets as well as run them.

Java Runtime Environment  JRE contains everything required to run Java application which has already been compiled.  It doesn’t contain the code library required to develop Java application. Java Virtual Machine (JVM)

The Java Virtual Machine is called JVM, is program or an abstract computing machine or virtual machine that converts byte code to machine code and executes the java program. 

JVM is platform independent.



JVM is responsible to allocate the necessary memory needed by the java program during run time.



The JVM is the heart of the Java language’s.

Java Program Structure

Documentation Section The documentation section is an important section but optional for a Java program. To write the statements in the documentation section, we use comments. The comments may be single-line, multi-line, and documentation comments. 

Single-line Comment: It starts with a pair of forwarding slash (//)



Multi-line Comment: It starts with a /* and ends with */.



Multi-line Comment: It starts with a /* and ends with */.

Package Declaration The package declaration is optional. It is placed just after the documentation section. We use the keyword package to declare the package name. For example: package student; Import Statements

The package contains the many predefined classes and interfaces. If we want to use any class of a particular package, we need to import that class. We use the import keyword to import the class. For Example: import java.util.Scanner; //it imports the Scanner class only import java.util.*; //it imports all the class of the java.util package Interface Section It is an optional section. We can create an interface in this section if required. We use the interface keyword to create an interface. An interface is a slightly different from the class. It contains only constants and method declarations. Class Definition In this section, we define the class. It is very important part of a Java program. Without the class, we cannot create any Java program. A Java program may conation more than one class definition. We use the class keyword to define the class. The class is a blueprint of a Java program. It contains information about userdefined methods, variables, and constants. Every Java program has at least one class that contains the main() method. Main Method Class In this section, we define the main() method. It is essential for all Java programs. Because the execution of all Java programs starts from the main() method. In other words, it is an entry point of the class. It must be inside the class.

First Program in Java Let us create HelloWorld.java program, class HelloWorld { public static void main(String args[]) { System.out.println("HelloWorld!!!"); } }

In the above program, 

class is a keyword used to declare a class in java.



public is a keyword used to specify access modifier. Public means it is visible to all.



static is a keyword, if we declare any method as static, it is known as static method. The advantage of static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require creating object to invoke the main method.



void is the return type of the method, it means it doesn't return any value.



main represents beginning of the program.



String args [ ] is used for command line argument.

 System.out.println() is used print statement. Note: Name of the file & name of a class must be same.

How to Compile and Execute java program 

To Compile: javac helloWorld.java



To Execute: java helloWorld

Java Naming Convention Identifiers Naming Rules Type Class

Interface

Method



It should start with the uppercase letter.



Use appropriate words, instead of acronyms.

   

It should start with the uppercase letter. It should be an adjective such as Runnable, Remote, ActionListener. Use appropriate words, instead of acronyms. It should start with lowercase letter.



It should be a verb such as main(), print(), println( ).



If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().

Variable

 

It should start with a lowercase letter such as id, name. It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).

Examples public class Employee { //code snippet } interface Printable { //code snippet } class Employee { // method void draw() { //code snippet } } class Employee { // variable

 

If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as firstName, lastName. Avoid using one-character variables such as x, y, z. It should be a lowercase letter such as java, lang.



If the name contains multiple words, it should be



Package

separated by dots (.) such as java.util, java.lang.

Constant

  

It should be in uppercase letters such as RED, YELLOW. If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY. It may contain digits but not as the first letter.

int id; //code snippet } //package package com.javatpoint; class Employee { //code snippet } class Employee { //constant static final int MIN_AGE = 18; //code snippet }

Data types and other tokens Data type: It specifies type of data to be stored in variables. Whereas variables are the names given to memory locations in which values can be stored. There are two different data types in Java, namely  Primitive Data type  Reference / Object Data type Primitive Data Type By default, primitive data types are supported by Java. Each type is represented using keyword. There are eight different primitive data types, namely  byte  short  int  long  float  double  char  boolean The following should be understood for every data type: 1. Memory size allocated. 2. Default value.

3. Range of values it can represent. 4. Minimum values can be represented. 5. Maximum value can be represented. Boolean 1. Allocated Memory Size: 1 bit 2. Default value: False 3. Keyword: boolean 4. Example: boolean a = TRUE; 5. It represents either TRUE or FALSE Byte 

Allocated Memory Size: 1 byte or 8 bits



Default value: Zero



Keyword: byte



Example: byte a = 100



Minimum Value can be represented: -128 (-27)



Maximum Value can be represented: 127

Char 

Allocated Memory Size: 2 bytes or 16 bits



Default value: '\u0000'



Keyword: char



Example: char ch = 'A';



Minimum Value can be represented: '\u0000' (0)



Maximum Value can be represented: '\uffff' (65,535)

Short 

Allocated Memory Size: 2 bytes or 16 bits



Default value: Zero



Keyword: short



Example: short s = 1000;



Minimum Value can be represented: -32,768 (215)



Maximum Value can be represented: 32,767 (215 - 1)

Int



Allocated Memory Size: 4 bytes or 32 bits



Default value: Zero



Keyword: int



Example: int a = 10;



Minimum Value can be represented: - 2,147,483,648 (-231)



Maximum Value can be represented: 2,147,483,647 (231)

Float 

Allocated Memory Size: 4 bytes or 32 bits



Default value: 0.0f



Keyword: float



Example: float x = 2.3f;



Minimum Value can be represented: 1.4E-45



Maximum Value can be represented: 3.4028235E38

Long 

Allocated Memory Size: 8 bytes or 64 bits



Default value: 0L



Keyword: long



Example: long b = 100000L;



Minimum Value can be represented: -9223372036854775808



Maximum Value can be represented: 9223372036854775807

Double 

Allocated Memory Size: 8 bytes or 16 bits



Default value: 0.0d



Keyword: double



Example: double d = 3.1428571



Minimum Value can be represented: 4.9E-324



Maximum Value can be represented: 1.7976931348623157E308

Tokens Tokens are group of characters combined to form meaningful word. There are five types of tokens, namely 1. Keywords

2. Identifiers 3. Literals 4. Operators 5. Separators Keywords 

Keywords are also called as reserved words.



Keywords have predefined meaning in Java language.



There are 49 keywords are currently available in Java language.

abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

extends

false

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

package

private

protected

public

return

short

static

super

switch

synchronized

this

throw

transient

true

try

void

volatile

while

Identifiers Identifiers are the names given for variables, classes, functions, objects, packages, interfaces in Java program.

Java Variables A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type. Variable is a name of memory location.

There are three types of variables in java: 1. local variable 2. instance variable 3. static variable 1 Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method. Note: A local variable cannot be defined with "static" keyword 2 Instance Variables A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static. 3 Static variables A variable that is declared as static is called a static variable. It cannot be local. Memory allocation for static variables happens only once when the class is loaded in the memory.

Rules to write variables 1. It may consists of letters, digits or underscore ( _ ). 2. First character must be either letter or underscore. 3. Identifiers are case sensitive. 4. Identifiers can be of any length.

Literals (Constants) It is sequence of characters (includes letters, digits, special characters) that represent constant values to be stored in variable. There are five types of literals, namely 1. Integer Literals 2. Boolean Literals ...


Similar Free PDFs