JAVA
JAVA
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.
•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:
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:
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:
For classes we use PascalConventions i.e. First alphabet capital for the
word.
: FirstYearStudents
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 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.
Conditional Statement:
if
if-else
Switch
}
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
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 designed with the aim to be shared among all objects created from the same
class.
Declaration of static
method
Static Methods Instance Methods
Can be called without the object of the class. Require an object of the class.
Can only access static attributes of the class. Can access all the attributes of the class.