You are on page 1of 41

GLS UNIVERSITY

Faculty of Computer Applications & Information Technology

Integrated MCA Programme

Semester III

OBJECT ORIENTED PROGRAMMING USING JAVA

Unit 1: Introduction to Java and Classes


JAVA Basics


Java is an object-oriented programming language developed by
Sun Microsystems and released in 1995.

● Java was originally developed by James Gosling at Sun


Microsystems (which has since merge into Oracle Corporation).


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.

● 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 Basics – JDK

The Java Development Kit (JDK) is a cross-platformed software development
environment that offers a collection of tools and libraries necessary for developing
Java-based software applications and applets.

It is a core package used in Java, along with the JVM (Java Virtual Machine) and
the JRE (Java Runtime Environment).
JAVA Basics – JVM

The Java Development Kit (JDK) is a cross-platformed software development
environment that offers a collection of tools and libraries necessary for developing
Java-based software applications and applets.

It is a core package used in Java, along with the JVM (Java Virtual Machine) and
the JRE (Java Runtime Environment).

JDK contains:

Java Runtime Environment (JRE),

An interpreter/loader (Java),

A compiler (javac),

An archiver (jar) and many more.
JAVA PROGRAM COMPILATION PROCESS
JAVA PROGRAM COMPILATION PROCESS
CLASS
● A class — in the context of Java — is a template used to create
objects and to define object data types and methods.

Classes are categories, and objects are items within each
category.
● A class in Java is a logical template to create objects that share
common properties and methods.
● All class objects should have the basic class properties.
Properties
Java Essentials – Character Set

Java language uses the character sets as the building block to form the basic
elements such as identifiers, variables, array, etc in the program.
Java Essentials – Variables

In Java, Variables are the data containers that save the data values during
Java program execution.

Every Variable in Java is assigned a data type that designates the type and
quantity of value it can hold.

Java variable is a name given to a memory location.

It is the basic unit of storage in a program.

The value stored in a variable can be changed during program execution.

In Java, all variables must be declared before use.
Types of Variables in Java

Local Variables: These are variables that are declared inside the body of a method.

Instance Variables: These are defined without the STATIC keyword. They are defined
Outside a method declaration. They are Object specific and are known as instance
variables.
Static Variables: These are initialized only once, at the start of the program execution.
These variables should be initialized first, before the initialization of any instance
variables.
Java Essentials – DataTypes

● Data Types in Java are defined as


specifiers that allocate different
sizes and types of values that can
be stored in the variable or an
identifier.


Java has a rich set of data types.
Java Essentials – DataTypes
Java Essentials – Comments
Structure of
Java Program
Structure of Java Program

class keyword is used to declare a class in java.

public keyword is an access modifier which represents visibility, it means it is visible to
all.

static is a keyword, if we declare any method as static, it is known as static method.

The core advantage of static method is that there is no need to create object to
invoke the static method.

main represents startpoint of the program.

The main method is executed by the JVM, so it doesn't require to create object to
invoke the main method. So it saves memory.

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

String[ ] args is used for command line argument.

System.out.println( ) is used print statement.
Reading : Using scanner

● The Scanner class is used to get user input, and it is found in the java.util
package.

The java.util.Scanner class is a simple text scanner which can parse
primitive types and strings using regular expressions.
● Following are the important points about Scanner:

● A Scanner breaks its input into tokens using a delimiter pattern, which
by default matches whitespace.

● A scanning operation may block waiting for input.


Reading : Using scanner (Numeric and String Methods)

• int nextInt() - Returns the next token as an int. If the next token is not
an integer, InputMismatchException is thrown.
• long nextLong() - Returns the next token as a long. If the next token is
not an integer, InputMismatchException is thrown.
• float nextFloat() - Returns the next token as a float. If the next token is
not a float or is out of range, InputMismatchException is thrown.
• double nextDouble() - Returns the next token as a long. If the next
token is not a float or is out of range, InputMismatchException is
thrown.
Reading : Using scanner (Numeric and String Methods)

• String next() - Finds and returns the next complete token from this
scanner and returns it as a string; a token is usually ended by
whitespace such as a blank or line break. If not token exists,
NoSuchElementException is thrown.
• String nextLine() - Returns the rest of the current line, excluding any
line separator at the end.
• void close() - Closes the scanner.
JOptionPane
• The class JOptionPane is a component which provides standard
methods to pop up a standard dialog box for a value or informs the user
of something.
• public class JOptionPane
extends JComponent
implements Accessible
JoptionPane: Constructors

JOptionPane(): It's used to make a JOptionPane that displays a
test message.

● JOptionPane(Object message): It's used to make a JOptionPane


instance for displaying a message.


JOptionPane(Object message, messageType): It's used to make a
JOptionPane instance that displays a message with the message
type and default options supplied.
import javax.swing.JOptionPane;


AWT: Abstract Window Toolkit (its a Package)


It provides various components classes for Label, Button,
TextField


Swing is the part of JFC (Java Foundation Classes) built on
the top of AWT and written entirely in Java.


The javax.swing API provides all the component classes like
JButton, JTextField, JCheckbox, JMenu, etc.
Java Swing Hierarchy
JoptionPane: JFrame Class
● JFrame class is a type of container inheriting the java. awt. Frame
class.


Whenever a Graphical Use Interface (GUI) is created with Java Swing
functionality, a container is required where components like labels,
buttons, and text fields are added to create a Graphical User
Interface(GUI) and are known as JFrame.
JoptionPane: Types of Dialog Boxes
● JDialog createDialog (String title): It is used to create and return a new parentless JDialog
with the specified title.
● static void showMessageDialog (Component parentComponent, Object message): It is used to
create an information-message dialog.

static void showMessageDialog (Component parentComponent, Object message, String title,
int messageType): It is used to create a message dialog with given title and messageType.
● static int showConfirmDialog (Component parentComponent, Object message): It is used to
create a dialog with the options Yes, No and Cancel; with the title, Select an Option.
● static String showInputDialog (Component parentComponent, Object message): It is used to
show a question-message dialog requesting input from the user parented to
parentComponent.
● void setInputValue (Object newValue): It is used to set the input value that was selected or
input by the user
Type Casting
Assigning a value of one type to a variable of another type is known as
Type Casting.
1.Implicit Casting

2.Explicitly Casting
Operator Precedence
Classes and Objects

Class are a blueprint or a set of instructions to build a specific type of object.

It is a basic concept of Object-Oriented Programming which revolve around the
real-life entities.

Class in Java determines how an object will behave and what the object will
contain.

Object is an instance of a class.

An object in OOPS is a self-contained component which consists of methods and
properties to make a particular type of data useful.

From a programming point of view, an object in OOPS can include a data
structure, a variable, or a function. It has a memory location allocated.

Java Objects are designed as class hierarchies.
Classes and Objects

Class are a blueprint or a set of instructions to build a specific type of object.

It is a basic concept of Object-Oriented Programming which revolve around the
real-life entities.

Class in Java determines how an object will behave and what the object will
contain.

Object is an instance of a class.

An object in OOPS is a self-contained component which consists of methods and
properties to make a particular type of data useful.

From a programming point of view, an object in OOPS can include a data
structure, a variable, or a function. It has a memory location allocated.

Java Objects are designed as class hierarchies.
Ref: Internet Reference
Constructors

In Java, a constructor is a block of codes similar to the method.

It is called when an instance of the class is created.

At the time of calling constructor, memory for the object is allocated in
the memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one
constructor is called.

It calls a default constructor if there is no constructor available in the
class. In such case, Java compiler provides a default constructor by
default.
Constructors

Rules for creating Java constructors:

Constructor name must be the same as its class name

A Constructor must have no explicit return type

A Java constructor cannot be abstract, static, final, and
synchronized
Types of Constructors

Default Constructor

Parameterised Constructor

Copy Constructor

You might also like