0% found this document useful (0 votes)
16 views20 pages

Unit 4.1

Java is a versatile, object-oriented programming language developed by Sun Microsystems in 1995, known for its platform independence and wide applications in various fields. Key concepts include object-oriented programming principles such as abstraction, encapsulation, inheritance, and polymorphism, along with essential Java terminology like JVM, JDK, and JRE. The document also outlines the steps to create, compile, and run Java programs, along with data types and features that make Java robust, secure, and suitable for distributed applications.

Uploaded by

Amol Rajpure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

Unit 4.1

Java is a versatile, object-oriented programming language developed by Sun Microsystems in 1995, known for its platform independence and wide applications in various fields. Key concepts include object-oriented programming principles such as abstraction, encapsulation, inheritance, and polymorphism, along with essential Java terminology like JVM, JDK, and JRE. The document also outlines the steps to create, compile, and run Java programs, along with data types and features that make Java robust, secure, and suitable for distributed applications.

Uploaded by

Amol Rajpure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to Java

What is Java?
• Developed by Sun Microsystems in 1995, Java is a highly
popular, object-oriented programming language.
• This platform independent programming language is utilized
for Android development, web development, artificial
intelligence, cloud applications, and much more.
• Java is one of the most popular and widely used
programming languages and platforms.
• Java is fast, reliable, and secure.
• Steps to Implement Java Program
Implementation of a Java application program involves the
following step. They include:
1. Creating the program
2. Compiling the program
3. Running the program
Introduction to OOP concepts in java
• Object-oriented programming aims to
implement real-world entities like inheritance,
hiding, polymorphism, etc. in programming..
1.Data Abstraction is the property by virtue of which
only the essential details are displayed to the user.

2.Encapsulation
• It is defined as the wrapping up of data under a single
unit.
3.Inheritance is an important pillar of OOP (Object
Oriented Programming). It is the mechanism in Java by
which one class is allowed to inherit the features (fields
and methods) of another class.

4. Polymorphism :The ability to appear in many forms is


called polymorphism.
Java Terminology
Before learning Java, one must be familiar with these common terms of Java.
1. Java Virtual Machine(JVM): This is generally referred to as JVM. There
are three execution phases of a program. They are written, compile and run the
program.
• Writing a program is done by a java programmer like you and me.
• The compilation is done by the JAVAC compiler which is a primary Java
compiler included in the Java development kit (JDK). It takes the Java
program as input and generates bytecode as output.
• In the Running phase of a program, JVM executes the bytecode generated
by the compiler. Now, we understood that the function of Java Virtual
Machine is to execute the bytecode produced by the compiler. Every
Operating System has a different JVM but the output they produce after the
execution of bytecode is the same across all the operating systems. This is
why Java is known as a platform-independent language.
• 2. Bytecode in the Development Process: As discussed, the Javac
compiler of JDK compiles the java source code into bytecode so that it can
be executed by JVM. It is saved as .class file by the compiler. To view the
bytecode, a disassembler like javap can be used.
• 3. Java Development Kit(JDK): While we were using the term JDK when
we learn about bytecode and JVM. So, as the name suggests, it is a
complete Java development kit that includes everything including compiler,
Java Runtime Environment (JRE), java debuggers, java docs, etc. For the
program to execute in java, we need to install JDK on our computer in
order to create, compile and run the java program.
4. Java Runtime Environment (JRE): JDK includes JRE. JRE
installation on our computers allows the java program to run,
however, we cannot compile it. JRE includes a browser, JVM, applet
support, and plugins. For running the java program, a computer needs
JRE.

5. Garbage Collector: In Java, programmers can’t delete the objects.


To delete or recollect that memory JVM has a program called Garbage
Collector. Garbage Collectors can recollect the objects that are not
referenced. So Java makes the life of a programmer easy by handling
memory management. However, programmers should be careful about
their code whether they are using objects that have been used for a
long time. Because Garbage cannot recover the memory of objects
being referenced.

6. ClassPath: The classpath is the file path where the java runtime
and Java compiler look for .class files to load. By default, JDK
provides many libraries. If you want to include external libraries they
should be added to the classpath.
Primary/Main Features of Java
1. Platform Independent: Compiler converts source code to bytecode and
then the JVM executes the bytecode generated by the compiler. This
bytecode can run on any platform be it Windows, Linux, or macOS which
means if we compile a program on Windows, then we can run it on Linux
and vice versa. Each operating system has a different JVM, but the output
produced by all the OS is the same after the execution of the bytecode.
That is why we call java a platform-independent language.

2. Object-Oriented Programming Language: Organizing the program in the


terms of a collection of objects is a way of object-oriented programming, each
of which represents an instance of the class.
• The four main concepts of Object-Oriented programming are:
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism

3. Simple: Java is one of the simple languages as it does not have complex
features like pointers, operator overloading, multiple inheritances, and Explicit
memory allocation.
4. Robust: Java language is robust which means reliable. It is
developed in such a way that it puts a lot of effort into checking errors
as early as possible, that is why the java compiler is able to detect
even those errors that are not easy to detect by another programming
language. The main features of java that make it robust are garbage
collection, Exception Handling, and memory allocation.

5. Secure: In java, we don’t have pointers, so we cannot access


out-of-bound arrays i.e it shows ArrayIndexOutOfBound
Exception if we try to do so. That’s why several security flaws like
stack corruption or buffer overflow are impossible to exploit in Java.
Also, java programs run in an environment that is independent of the
os(operating system) environment which makes java programs more
secure.

6. Distributed: We can create distributed applications using the java


programming language. Remote Method Invocation and Enterprise
Java Beans are used for creating distributed applications in java. The
java programs can be easily distributed on one or more systems that
are connected to each other through an internet connection.
7. Multithreading: Java supports multithreading. It is a
Java feature that allows concurrent execution of two or
more parts of a program for maximum utilization of the
CPU.

8. Portable: As we know, java code written on one


machine can be run on another machine. The
platform-independent feature of java in which its
platform-independent bytecode can be taken to any
platform for execution makes java portable.

9. High Performance: Java architecture is defined in such


a way that it reduces overhead during the runtime and at
some times java uses Just In Time (JIT) compiler where the
compiler compiles code on-demand basics where it only
compiles those methods that are called making applications
to execute faster.
10. Dynamic flexibility: Java being completely object-oriented gives
us the flexibility to add classes, new methods to existing classes, and
even create new classes through sub-classes. Java even supports
functions written in other languages such as C, C++ which are
referred to as native methods.
11. Sandbox Execution: Java programs run in a separate space that
allows user to execute their applications without affecting the
underlying system with help of a bytecode verifier. Bytecode verifier
also provides additional security as its role is to check the code for any
violation of access.
12. Write Once Run Anywhere: As discussed above java application
generates a ‘.class’ file that corresponds to our applications(program)
but contains code in binary format. It provides ease t
architecture-neutral ease as bytecode is not dependent on any machine
architecture. It is the primary reason java is used in the enterprising IT
industry globally worldwide.
13. Power of compilation and interpretation: Most languages are
designed with the purpose of either they are compiled language or
they are interpreted language. But java integrates arising enormous
power as Java compiler compiles the source code to bytecode and
JVM executes this bytecode to machine OS-dependent executable
code.
class : class keyword is used to declare classes in Java
public : It is an access specifier. Public means this function is visible
to all.
static : static is again a keyword used to make a function static. To
execute a static function you do not have to create an Object of the
class. The main() method here is called by JVM, without creating any
object for class.
void : It is the return type, meaning this function will not return
anything.
main : main() method is the most important method in a Java
program. This is the method which is executed, hence all the logic
must be inside the main() method. If a java class is not having a
main() method, it causes compilation error.
String[] args : This is used to signify that the user may opt to enter
parameters to the Java Program at command line. We can use both
String[] args or String args[]. Java compiler would accept both forms.
System.out.println : This is used to print anything on the console like
“printf” in C language.
1. Creating Programs in Java
• We can create a program using Text Editor
(Notepad) or IDE (NetBeans).
class Test
{
public static void main(String []args)
{
System.out.println("My First Java Program.");
}
};
File Save: d:\Test.java
2. Compiling the Program in Java
• To compile the program, we must run the Java
compiler (javac), with the name of the source file on
the “command prompt” like as follows
• If everything is OK, the “javac” compiler creates a
file called “Test.class” containing the byte code of the
program
• We need to use the Java Interpreter to run a program.
1. Create the program by typing it into a text editor and
saving it to a file – Test.java.
2. Compile it by typing “javac Test.java” in the
terminal window.
3. Execute (or run) it by typing “java Test” in the
terminal window.
Syntax public static void
main(String[] args)
• Public: So that JVM can execute the method from
anywhere.
• Static: The main method is to be called without an
object. The modifiers are public and static can be
written in either order.
• Void: The main method doesn’t return anything.
• Main(): Name configured in the JVM. The main
method must be inside the class definition. The
compiler executes the codes starting always from the
main function.
• String[]: The main method accepts a single argument,
i.e., an array of elements of type String.
System.out.println("My First Java Program.");

• This line outputs the string “Hello, World” followed


by a new line on the screen. Output is accomplished
by the built-in println( ) method. The System is a
predefined class that provides access to the system
and out is the variable of type output stream
connected to the console.
Data Types in Java
Type Description Default Size Example Literals Range of values

boolean true or false false 8 bits true, false true, false

byte twos-complement integer 0 8 bits (none) -128 to 127

characters representation of
‘a’, ‘\u0041’, ‘\101’, ‘\\’, ‘\’, ‘\n’,
char Unicode character \u0000 16 bits
‘β’
ASCII values
0 to 255

short twos-complement integer 0 16 bits (none) -32,768 to 32,767

-2,147,483,648
int twos-complement intger 0 32 bits -2,-1,0,1,2 to
2,147,483,647

-9,223,372,036,854,775,808
long twos-complement integer 0 64 bits -2L,-1L,0L,1L,2L to
9,223,372,036,854,775,807

1.23e100f , -1.23e-100f , .3f


float IEEE 754 floating point 0.0 32 bits
,3.14F
upto 7 decimal digits

1.23456e300d ,
double IEEE 754 floating point 0.0 64 bits
-123456e-300d , 1e1d
upto 16 decimal digits
Non-Primitive Data Type
1. Strings
Strings are defined as an array of characters.
Syntax: Declaring a string
<String_Type> <string_variable> = <sequence_of_string>”;
2. Class
A class is a user-defined blueprint or prototype from which objects are created.
It represents the set of properties or methods that are common to all objects of
one type.
• Modifiers: A class can be public or has default access. Refer to access
specifiers for classes or interfaces in Java
• Class name: The name should begin with an initial letter (capitalized by
convention).
• Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
• Interfaces(if any): A comma-separated list of interfaces implemented by the
class, if any, preceded by the keyword implements. A class can implement
more than one interface.
• Body: The class body is surrounded by braces, { }.
3. Object
• An Object is a basic unit of Object-Oriented Programming and
represents real-life entities. A typical Java program creates many
objects, which as you know, interact by invoking methods. An
object consists of :
• State: It is represented by the attributes of an object. It also reflects
the properties of an object.
• Behavior: It is represented by the methods of an object. It also
reflects the response of an object to other objects.
• Identity: It gives a unique name to an object and enables one
object to interact with other objects.
4. Interface
• Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only
method signature, no body).
5. Array
• An Array is a group of like-typed variables that are referred to by a
common name

You might also like