Nothing Special   »   [go: up one dir, main page]

JAVA

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

JAVA : CORE

UNIT I : INTRODUCTION TO JAVA


History of Java

The history of Java starts with the Green Team. Java team members (also known as Green Team), initiated this
project to develop a language for digital devices such as set-top boxes, televisions, etc. However, it was best
suited for internet programming. Later, Java technology was incorporated by Netscape.

The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured,
High Performance, Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and Dynamic".

Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team
members started the project in the early '90s.

Development Year: 1991

First Version of Java: 1996


Java originated at Sun micro January 27, 2010
Java was developed by systems in 1991 The acquisition of Sun
James Gosling at Sun Microsystems by Oracle
micro systems Sir Vinod Khosla was Co – Corporation was completed
founder of Sun micro systems. on January 27, 2010.
He was an IIT Delhi graduate
JAVA

•Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
•It is one of the most popular programming language in the world
•It has a large demand in the current job market
•It is easy to learn and simple to use
•It is open-source and free
•It is secure, fast and powerful
•It has a huge community support (tens of millions of developers)
•Java is an object oriented language which gives a clear structure to programs and allows code to be
reused, lowering development costs
•As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
Simple
Java is very easy to learn, and its syntax is simple, clean and
easy to understand. According to Sun Microsystem, Java
language is a simple programming language because:

Java syntax is based on C++ (so easier for programmers to


learn it after C++).
Java has removed many complicated and rarely-used features,
for example, explicit pointers, operator overloading, etc.
There is no need to remove unreferenced objects because
there is an Automatic Garbage Collection in Java.

Object-oriented:
Java is an object-oriented programming language. Everything
in Java is an object. Object-oriented means we organize our
software as a combination of different types of objects that
incorporate both data and behavior. Object-oriented
programming (OOPs) is a methodology that simplifies
software development and maintenance by providing some
rules.
Basic concepts of OOPs are:
Object, Class, Inheritance, Polymorphism, Abstraction,
Encapsulation
Platform Independent:
Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform
specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in
which a program runs.

There are two types of platforms software-based and hardware-based. Java provides a software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on top of other
hardware-based platforms. It has two components:

Runtime Environment
API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is
compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it can be run on
multiple platforms, i.e., Write Once and Run Anywhere (WORA).

Secured:
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured because:
•No explicit pointer
•Java Programs run inside a virtual machine sandbox
•Class loader: It in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into the Java
Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that
are imported from network sources.
•Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to objects.
•Security Manager: It determines what resources a class can access such as reading and writing to the local disk
Robust
The English mining of Robust is strong. Java is robust because:
•It uses strong memory management.
•There is a lack of pointers that avoids security problems.
•Java provides automatic garbage collection which runs on the Java Virtual Machine to get rid of objects which are not being
used by a Java application anymore.
•There are exception handling and the type checking mechanism in Java. All these points make Java robust

Architecture-neutral
Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is
fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit
architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.

Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation.

High-performance

Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is
still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than
compiled languages, e.g., C, C++, etc.
Distributed
Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating
distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the
internet.

Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by
defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a
common memory area. Threads are important for multi-media, Web applications, etc.

Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also
supports functions from its native languages, i.e., C and C++. Java supports dynamic compilation and automatic memory
management (garbage collection)

Flavor's of Java:

•Standard Edition Java Platform ( i.e. Core Java)


•Enterprise Edition Java Platform (i.e. Advance Java )
•Micro Edition Java Platform (i.e. Mobile App Development)
JDK > JRE > 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.
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
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:

Standard Edition Java Platform


Enterprise Edition Java Platform
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.
Compilation & Runtime procedure in Java
Anatomy / Structure for Java Code :
Naming Conventions in Java:

For classes we use PascalConventions i.e. First alphabet capital for the
word.
: FirstYearStudents

For Functions we use camelCaseConvention i.e. First small alphabet then


capital for further new words
: firstYearStudents

No space is allowed while making of functions or variables.


package com.firstpac; A package in Java is used to group related classes.
Assume package as an folder in an directory.
The main() function is the entry point into the
public class Gold { application
String type array; “args” is
variable of string type
public static void main(String[] args)
Access { void here states that no return value will be given
after calling of this function.
modifier

Because of static, main function can be executed


without making object, “independent method”
//code will be written here

System.out.println(“Hello FY_MCA”);
}
}
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 String, Classes, Interfaces, and Arrays.

Non-Primitive Data Types

Non-primitive data types or reference data types refer to instances or objects. They cannot store the value
of a variable directly in memory. They store a memory address of the variable. Unlike primitive data
types we define by Java, non-primitive data types are user-defined. Programmers create them and can be
assigned with null.
Primitive data types in Java
Variables in Java:

Java Variables

A variable is a container which holds/ stores 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.

Ex: int number = 10; [ Here “int” is datatype , “number” is variable name, “10” is value stored. ]

Reserved memory
were variable is declared
10 Rules for declaring a variable :
1. Should not begin with a digit/ number/ digit. → int 5xyz ;
2. Name is case sensitive → xyz & Xyz are two diff. variables
3. Should be a java keyword → void, System, etc
4. Space not allowed
RAM 5. Alphabets are allowed & numbers too , special characters are applicable
if above conditions are fulfilled.→ $ , _ .
6. Suggested: variables should always be readable , easy to understand in
large as well as small code.
Operators in Java
Operator in Java is a symbol that is used to perform operations.
For example: +, -, *, / etc.
Literals in Java:

In Java, literal is a notation that represents a fixed value in the source code. In lexical analysis, literals
of a given type are generally known as tokens. In this section, we will discuss the term literals in
Java.

Literals
In Java, literals are the constant values that appear directly in the program. It can be assigned directly
to a variable. Java has various types of literals.

The following figure represents a literal.


Decision Making / Switch/ looping Statements in Java:

A programming language uses control statements to control the flow of execution


of a program based on certain conditions. These are used to cause the flow of
execution to advance and branch based on changes to the state of a program.

Conditional Statement:

if

if-else

Switch

jump- break, continue, return.


if: if statement is the most simple decision-making statement. It is used to decide whether a certain statement
or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is
executed otherwise not.

Syntax: class IfDemo {


public static void main(String args[])
{
if(condition)
int i = 10;
{
// Statements to execute if if (i < 15)
// condition is true System.out.println("10 is less than 15");
}
}

}
if-else: The if statement alone tells us that if a condition is true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do something else if the condition is false? Here comes the else
statement. We can use the else statement with the if statement to execute a block of code when the condition is
false.
class IfElseDemo {
Syntax: public static void main(String args[])
{
if (condition) int age = 10;
{
// Executes this block if
// condition is true if (i < 15){
} System.out.println(“age is less than 15");
else }
{ else{
// Executes this block if System.out.println(“age is greater than 15");
// condition is false
}
}
}
}
if-else if- else: class IfElseifDemo {
public static void main(String args[])
Syntax: {
int age = 10;
if (condition)
{
// Executes this block if if (i < 15)
// condition is true & {
it will terminate System.out.println(“age is less than 15");
} }
else if(condition) elseif (i > 15)
{ {
// Executes this block if System.out.println(“age is greater than 15");
// condition is true }
} else
else
{
{
// Executes this block if System.out.println(“age is greater than 10");
// condition is falsw }
}
} }
switch-case: The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
Syntax: class GFG {
switch (expression) public static void main (String[] args) {
{ int num=20;
case value1: switch(num){
statement1; case 5 : System.out.println("It is 5");
break; break;
case value2: case 10 : System.out.println("It is 10");
statement2; break;
break; case 15 : System.out.println("It is 15");
. break;
. case 20 : System.out.println("It is 20");
case valueN: break;
statementN; default: System.out.println("Not present");
break;
default: }
statementDefault; }
} }
Java Instance Methods

Instance methods are methods that require an object of its class to be created
before it can be called. To invoke an instance method, we have to create an Object
of the class in which the method is defined.

•Instance method(s) belong to the Object of the class, not to the class i.e. they can
be called after creating the Object of the class.

•Instance methods are not stored on a per-instance basis, even with virtual
methods. They’re stored in a single memory location, and they only “know” which
object they belong to because this pointer is passed when you call them.
Object creation &
calling of instance
method

Non static method /


Instance method
Java Static Methods:

Static methods are the methods in Java that can be called without creating an object of
class. They are referenced by the class name itself or reference to the Object of that class.

•Static method(s) are associated with the class in which they reside i.e. they are called
without creating an instance of the class i.e. ClassName.methodName(args).

•They are declared with “static” keyword.

•They are designed with the aim to be shared among all objects created from the same
class.

•It uses class-name as reference

•They are implemented with a single copy throughout program.


Calling of static
methods with class
name

Declaration of static
method
Static Methods Instance Methods

Can be called without the object of the class. Require an object of the class.

Associated with the class. Associated with the objects.

Can only access static attributes of the class. Can access all the attributes of the class.

Declared with the static keyword. Do not require any keywords.

Exist as multiple copies depending on the


Exists as a single copy for a class.
number of instances.

You might also like