0% found this document useful (0 votes)
15 views

Unit1

The document provides an overview of Object-Oriented Programming (OOP) concepts and introduces Java as a high-level, robust programming language developed by Sun Microsystems in 1995. It explains the differences between JDK, JRE, and JVM, and covers Java class libraries, data types, variables, arrays, typecasting, operators, and control statements. Additionally, it includes examples of Java syntax and programming constructs such as loops and conditionals.

Uploaded by

rohitdamer2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit1

The document provides an overview of Object-Oriented Programming (OOP) concepts and introduces Java as a high-level, robust programming language developed by Sun Microsystems in 1995. It explains the differences between JDK, JRE, and JVM, and covers Java class libraries, data types, variables, arrays, typecasting, operators, and control statements. Additionally, it includes examples of Java syntax and programming constructs such as loops and conditionals.

Uploaded by

rohitdamer2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit-1

Object-Oriented Programming or OOPs refers to languages that use objects in programming.


Object-oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc in programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code can access this data except
that function.
OOPs Concepts:
1.Class
2.Objects
3.Data Abstraction
4.Encapsulation
5.Inheritance
6.Polymorphism
7.Dynamic Binding
8.Message Passing

What is Java?
Java is a programming language and a platform. Java is a high level, robust, object-
oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak.
Since Oak was already a registered company, so James Gosling and his team changed the
name from Oak to Java.

Platform: Any hardware or software environment in which a program runs, is known as a


platform. Since Java has a runtime environment (JRE) and API, it is called a platform.
Difference between JDK, JRE, and JVM

JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it
doesn't physically exist. It is a specification that provides a runtime environment in which
Java bytecode can be executed. It can also run those programs which are written in other
languages and compiled to Java bytecode.

JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are
platform dependent because the configuration of each OS is different from each other.
However, Java is platform independent. There are three notions of the
JVM: specification, implementation, and instance.

The JVM performs the following main tasks:

o Loads code
o Verifies code
o Executes code
o Provides runtime environment

JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of
JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun
Micro Systems.

JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applications and applets. It
physically exists. It contains JRE + development tools.

JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:

o Standard Edition Java Platform


o Enterprise Edition Java Platform
o Micro Edition Java Platform

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as
an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation
generator (Javadoc), etc. to complete the development of a Java Application.
Java Class Libraries
Java class libraries, also known as Java APIs (Application Programming Interfaces), are pre-built
sets of classes and interfaces that provide a wide range of functionality for developing Java
applications. These libraries are organized into packages, each containing related classes and
interfaces.
Here are some important and commonly used Java class libraries:
1.Java Standard Library (Java SE):
java.lang: Fundamental classes and basic language features.
java.util: Utility classes for data structures and collections (e.g., lists, maps, queues).etc
2. Java Enterprise Edition (Java EE):
javax.servlet: Classes for creating web applications using Servlets.
javax.ejb: Enterprise JavaBeans for building enterprise-level applications.
3. JavaFX:
javafx.scene: Classes for creating rich graphical user interfaces (GUIs).
4.Apache Commons: A collection of reusable Java components and libraries that extend the
functionality provided by the Java Standard Library. Examples include Apache Commons
Collections, Apache Commons Lang, and Apache Commons IO.
5.Spring Framework: A popular open-source framework that provides a comprehensive
programming and configuration model for modern Java-based enterprise applications.
6.Hibernate: An object-relational mapping (ORM) library that simplifies database interaction in
Java applications.
7.JUnit: A widely used testing framework for Java that simplifies the process of writing and
running unit tests.
8.Log4j: A logging utility for Java that provides flexible logging within applications.
9.Jackson: A library for working with JSON data, providing JSON parsing and serialization
capabilities
Creating Hello World Example
Let's create the hello java program:

class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

Parameters used in First Java Program


Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().

o class keyword is used to declare a class in Java.


o public keyword is an access modifier that represents visibility. It means it is visible
to all.
o static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the 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 an object to invoke the main() method. So, it saves
memory.
o void is the return type of the method. It means it doesn't return any value.
o main represents the starting point of the program.
o String[] args or String args[] is used for command line argument. We will discuss
it in coming section.
o System.out.println() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class.
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long,
float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.

Java Primitive Data Types


In Java language, primitive data types are the building blocks of data manipulation. These
are the most basic data types available in Java language.

There are 8 types of primitive data types:

o boolean data type


o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
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: local,
instance and static.

Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a name
of the memory location. It is a combination of "vary + able" which means its value can be
changed.

int data=50;//Here data is variable

Types of Variables
There are three types of variables in Java:

Local variable: A variable declared inside the body of the method is called local variable.
You can use this variable only within that method and the other methods in the class
aren't even aware that the variable exists. A local variable cannot be defined with "static"
keyword

Instance variable: A variable declared inside the class but outside the body of the method,
is called an instance variable. It is not declared as static. It is called an instance variable
because its value is instance-specific and is not shared among instances.

Static variable: A variable that is declared as static is called a static variable. It cannot be
local. You can create a single copy of the static variable and share it among all the
instances of the class. Memory allocation for static variables happens only once when the
class is loaded in the memory.
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous
memory location.

Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.

Types of Array in java


There are two types of array.

o Single Dimensional Array


o Multidimensional Array

Single Dimensional Array in Java


class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
/traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Multidimensional Array in Java
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}

Typecasting
Typecasting in Java is the process of converting a value from one data type to another. There are
two types of typecasting: implicit (automatic) and explicit (manual).
1.Implicit Typecasting (Widening Conversion):
- This occurs when the target data type can hold all possible values of the source data type.
- Java automatically performs this type of conversion without the need for an explicit cast.
For example: int x = 10;
double y = x; // Implicit casting from int to double
2. Explicit Typecasting (Narrowing Conversion):
- This is when you convert a value from a larger data type to a smaller data type.
- This type of conversion requires an explicit cast to avoid potential loss of information.
Syntax: (targetDataType) value
For example: double a = 10.5;
int b = (int) a; // Explicit casting from double to int
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in Java which are given below:

o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator

Java Operator Precedence


Java Control Statements
Java provides three types of control flow statements.

1. Decision Making statements


o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement

Decision-Making statements:
1) If Statement:
In Java, there are four types of if-statements given below:

1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement

1) Simple if statement:
if(condition) {
statement 1; //executes when condition is true
}
2) if-else statement
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
3) if-else-if ladder:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
4. Nested if-statement
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
Switch Statement:
switch (expression){
case value1:
statement1;
break;
. . .
case valueN:
statementN;
break;
default:
default statement;
}
Loop Statements

Java for loop


for(initialization, condition, increment/decrement) {
//block of statements
}

Java while loop


while(condition){
//looping statements
}
Java do-while loop
do
{
//statements
} while (condition);

Jump Statements
Jump statements are used to transfer the control of the program to the specific statements. In
other words, jump statements transfer the execution control to the other part of the program.
There are two types of jump statements in Java, i.e., break and continue.

Java break statement


As the name suggests, the break statement is used to break the current flow of the
program and transfer the control to the next statement outside a loop or switch
statement. However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the Java program, i.e., it can only
be written inside the loop or switch statement.

public class BreakExample {


public static void main(String[] args) {
for(int i = 0; i<= 10; i++) {
System.out.println(i);
if(i==6) {
break;
}
}
}
}

Java continue statement


Unlike break statement, the continue statement doesn't break the loop, whereas, it skips
the specific part of the loop and jumps to the next iteration of the loop immediately.

Consider the following example to understand the functioning of the continue statement
in Java.

public class ContinueExample {

public static void main(String[] args) {


for(int i = 0; i<= 2; i++) {
for (int j = i; j<=5; j++) {
if(j == 4) {
continue;
}
System.out.println(j);
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy