Core Java
Dinesh Kumar Kushwaha
What is Java?
• Java is a high-level, general-purpose, object-oriented, and secure
programming language developed by James Gosling at Sun Microsystems,
Inc. in 1991. It is formally known as OAK. In 1995, Sun Microsystem changed
the name to Java. In 2009, Sun Microsystem takeover by Oracle Corporation.
Editions of Java
• Each edition of Java has different capabilities. There are three editions of
Java:
• Java Standard Editions (JSE): It is used to create programs for a desktop
computer.
• Java Enterprise Edition (JEE): It is used to create large programs that run
on the server and manages heavy traffic and complex transactions.
• Java Micro Edition (JME): It is used to develop applications for small
devices such as set-top boxes, phone, and appliances.
Types of Java Applications
There are four types of Java applications that can be created using Java
programming:
• Standalone Applications: Java standalone applications uses GUI
components such as AWT, Swing, and JavaFX. These components contain
buttons, list, menu, scroll panel, etc. It is also known as desktop
alienations.
• Enterprise Applications: An application which is distributed in nature is
called enterprise applications.
• Web Applications: An applications that run on the server is called web
applications. We use JSP, Servlet, Spring, and Hibernate technologies for
creating web applications.
• Mobile Applications: Java ME is a cross-platform to develop mobile
applications which run across smartphones. Java is a platform for App
Development in Android.
Features of Java
• Simple: Java is a simple language because its syntax is simple, clean,
and easy to understand. Complex and ambiguous concepts of C++ are
either eliminated or re-implemented in Java. For example, pointer and
operator overloading are not used in Java.
• Object-Oriented: In Java, everything is in the form of the object. It
means it has some data and behavior. A program must have at least one
class and object.
• Robust: Java makes an effort to check error at run time and compile
time. It uses a strong memory management system called garbage
collector. Exception handling and garbage collection features make it
strong.
• Secure: Java is a secure programming language because it has no
explicit pointer and programs runs in the virtual machine. Java contains a
security manager that defines the access of Java classes.
Conti..
• Platform-Independent: Java provides a guarantee
that code writes once and run anywhere. This byte code
is platform-independent and can be run on any
machine.
Conti.
• Portable: Java Byte code can be carried to any platform. No
implementation-dependent features. Everything related to
storage is predefined, for example, the size of primitive data
types.
• High Performance: Java is an interpreted language. Java
enables high performance with the use of the Just-In-Time
compiler.
• Distributed: Java also has networking facilities. It is designed
for the distributed environment of the internet because it
supports TCP/IP protocol. It can run over the internet.
• Multi-threaded: Java also supports multi-threading. It means to
handle more than one job a time
JDK
What is JRE?
• JRE (Java Runtime Environment) is a software package
that provides Java class libraries, Java Virtual Machine
(JVM), and other components that are required to run
Java applications.
• JRE is the superset of JVM.
What is JVM?
• JVM (Java Virtual Machine) is an abstract machine that
enables your computer to run a Java program.
• When you run the Java program, Java compiler first
compiles your Java code to bytecode. Then, the JVM
translates bytecode into native machine code (set of
instructions that a computer's CPU executes directly).
• Java is a platform-independent language. It's because
when you write Java code, it's ultimately written for JVM
but not your physical machine (computer). Since JVM
executes the Java bytecode which is platform-
independent, Java is platform-independent.
JVM
Relationship between JVM, JRE,
and JDK.
Structure of Java Program
class <classname>
{
public static void main(String args[])
{
-------------
---------
}
}
Explanation
1.class <classname>-: This statement defines the class declaration.
2.public-: It is access specifier defines the accessibility of method means it
specify member can access anywhere.
Note-: If we use private, protected, and default before the main()
method, it will not be visible to JVM.
3.static-: We should call the main() method without creating an
object. Static methods are the method which invokes without
creating the objects, so we do not need any object to call the
main() method.
4. void: In Java, every method has the return type. Void keyword
acknowledges the compiler that main() method does not return
any value.
Conti..
5main(): It is a default signature which is predefined in
the JVM. It is called by JVM to execute a program line by
line and end the execution after completion of this
method. We can also overload the main() method.
6. String args[]: The main() method also accepts some
data from the user. It accepts a group of strings, which is
called a string array.
Note-: String is a class located in java.lang package
that declares an array which is used to accept command
line argument.
What is class
• A class is logical entity that contains data member(variables) and
member function(methods).
• It is implementation of encapsulation that is feature of object
oriented language.
To declare any class in java-
class <classname>
{
---------//data member
---------//member function
}
class
To access member of class, objects are needed
• In java, object are created in two ways-:
step1:declare of object
<classname> <objectname>
step2: Instance creation
<objectname>=new <class name>
OR
<class name> objectname=new class name();
To access member of class
<object name> . Member name;
Eg:
class First
{
public static void main(String args[])
{
System.out.println(“Hello”);
}
}
Note-:
System-: It is a class located in java.lang package
out-: It is the object of printstream class defines in System class.
print/println(): this is method of printstream class.