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

01_1_Java_Intro

The document provides an overview of Java as a programming language and platform, highlighting its features such as platform independence, simplicity, robustness, security, and object-oriented principles. It covers the history of Java's development, its naming conventions, and the architecture of the Java Virtual Machine (JVM), including its components like the classloader and execution engine. Additionally, it outlines the types of applications that can be developed using Java and the various editions available for different use cases.

Uploaded by

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

01_1_Java_Intro

The document provides an overview of Java as a programming language and platform, highlighting its features such as platform independence, simplicity, robustness, security, and object-oriented principles. It covers the history of Java's development, its naming conventions, and the architecture of the Java Virtual Machine (JVM), including its components like the classloader and execution engine. Additionally, it outlines the types of applications that can be developed using Java and the various editions available for different use cases.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Index

Introduction
History
Naming Conventions, CamelCase, Basic Rules
Java Architecture: (JVM, JRE, JDK)
Java Syntax
Precedence
Java is an programming language and a platform
 Platform/architecture independent (Write once run anywhere!)

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.

 Simple & robust(“robust” strong error checking during the compilation/runtime)

 concurrent (capability to run multiple operations simultaneously)

 Secure (JVM, Bytecode compile)


 Automatic memory management (Garbage Collection: Java's garbage collector
automatically manages memory, preventing memory leaks and buffer
overflows.)
 Inherent Multi threaded support
 Object Oriented support -- Encapsulation, Inheritance & polymorphism ,
 abstraction
 Excellent I/O support
 Inherent networking support for TCP/IP , UDP/IP programming & for URLs
 Adds functional programming support."Simple, Robust, Portable, Platform-
independent, Secured, High Performance, Multithreaded, Architecture Neutral,
Object-Oriented, Interpreted, and Dynamic
 Statically typed language,
 automatic garbage collection,
 simplifies pointers; no directly accessible
 pointer to memory,
 simplified network access,
 multi-threading!
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.
Basic concepts of OOPs are:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation

Platform Independent

 write once, run anywhere


 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.
It has two components:
 Runtime Environment
 API(Application Programming Interface)

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 pointer
Java Programs run inside a virtual machine sandbox
Classloader:
Classloader in Java is a part of the Java Runtime Environment (JRE)
Which 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.
Java language provides these securities by default. Some security can also be
provided by an application developer explicitly through SSL, JAAS, Cryptography, etc.

Robust (is capacity of a computer system to handle the errors during execution
and manage the incorrect input of data)
 It uses strong memory management.
 No pointers security issues are prevented.
 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.
 Java has exception handling and type checking mechanism. All these points
make Java robust.

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

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

High-supported language
 Java use bytecode.
 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.

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(Anything that happens at run time is considered as 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).

Early Binding
Fast Execution
Late Binding
Slow Execution

History :

 Developed by Sun Microsystems (which is now the subsidiary of Oracle) in the


year 1995.

 James Gosling father of Java.

 Before Java, its name was Oak.

 But Oak was already register to company, so James Gosling and his team changed
the name from Oak to Java.

1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. The small team of sun engineers called Green Team.
2) Initially it was designed for small, embedded systems in electronic appliances like
set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many
countries like the U.S.A., France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.

Why Java Programming named "Java"?


7)The team gathered to choose a new name. The suggested words were "dynamic",
"revolutionary", "Silk", "jolt", "DNA", etc. They wanted something that reflected the
essence of the technology: revolutionary, dynamic, lively, cool, unique, and easy to
spell, and fun to say.
According to James Gosling, "Java was one of the top choices along with Silk". Since
Java was so unique, most of the team members preferred Java than other names.
8) Java is an island in Indonesia where the first coffee was produced (called Java
coffee). Java name was chosen by James Gosling while having a cup of coffee nearby
his office.
9) Initially developed by James Gosling at Sun Microsystems (which is now a
subsidiary of Oracle Corporation) and released in 1995.
10) In 1995, Time magazine called Java one of the Ten Best Products of 1995.
12) JDK 1.0 was released on January 23, 1996.

Java Version History


JDK Alpha and Beta (1995)
JDK 1.0 (23rd Jan 1996)
Java SE 8 (18th Mar 2014)
Java SE 11 (September 2018)
Java SE 17 (September 2021)
Java SE 18 (to be released by March 2022)
Since Java SE 8 release, the Oracle corporation follows a pattern in which every even
version is release in March month and an odd version released in September month.

Application
According to Sun, 3 billion devices run Java.

1. Desktop Applications such as acrobat reader, media player, antivirus, etc.


2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.

Types of Java Applications


There are mainly 4 types of applications that can be created using Java programming:

1) Standalone Application

Standalone applications are also known as desktop applications or window-based


applications. Media player, antivirus, etc. AWT and Swing are used in Java for
creating standalone applications.

2) Web Application

An application that runs on the server side and creates a dynamic page is called a
web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.

3) Enterprise Application

An application that is distributed in nature, such as banking applications, etc. is


called an enterprise application. It has advantages like high-level security, load
balancing, and clustering. In Java, EJB is used for creating enterprise applications.

4) Mobile Application

An application which is created for mobile devices is called a mobile application.


Currently, Android and Java ME are used for creating mobile applications.

Java Platforms / Editions


There are 4 platforms or editions of Java:

1) Java SE (Java Standard Edition)

It is a Java programming platform. It includes Java programming APIs such as


java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core
topics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream,
Networking, AWT, Swing, Reflection, Collection, etc.
2) Java EE (Java Enterprise Edition)

It is an enterprise platform that is mainly used to develop web and enterprise


applications. It is built on top of the Java SE platform. It includes topics like Servlet,
JSP, Web Services, EJB, JPA, etc.

3) Java ME (Java Micro Edition)

It is a micro platform that is dedicated to mobile applications.

4) JavaFX
It is used to develop rich internet applications. It uses a lightweight user interface API.

Naming Conventions
Class/Interface/enum public class Employee{
start with the uppercase letter, //code
and then follow camel case notation. }
Method(function)/ class Employee{
Members(variables) // method
should start with lowercase letter, void draw()
and then follow camel case notation. {
//code
}
}
Package //package
It should be a lowercase letter such as package com.javatpoint;
java, lang. class Employee{
If the name contains multiple words, it //code snippet
should be separated by dots (.) such as }
java.util, java.lang.
Constant class Employee{
All uppercase letters //constant
eg. PI, RED, YELLOW. static final int MIN_AGE = 18;
If the name contains multiple words, it //code snippet
should be separated by an underscore(_) }
such as MAX_PRIORITY.
It may contain digits but not as the first
letter.

CamelCase in Java naming conventions


Java follows camel-case syntax for naming the class, interface, method, and variable.
If the name is combined with two words, the second word will start with uppercase
letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.

Basic rules
1. Java compiler doesn't allow accessing of uninitialized data members.
2. A file can have more than one non public class.
3. There can be only one public class per source code file.
4. File name and class name must be same
Example.java
public class Example {....}
5. Java compiler doesn't allow accessing of un-initialized vars.
eg : int n;
sop(n);//error

Execution of Java Application?


At runtime, the following steps are performed:

Classloader: It is the subsystem of JVM that is used to


load class files.

Bytecode Verifier: Checks the code fragments for illegal code that can
violate access rights to objects.

Interpreter: Read bytecode then execute the


instructions.

Q) Can you save a Java source file by another name than the class
name?(works only on cmd)
Yes, if the class is not public. It is explained in the figure given below:
To compile: javac Hard.java

To execute: java Simple

Q) Can you have multiple classes in a java source file?


Yes, like the figure given below illustrates:

JVM (Java Virtual Machine) is an abstract machine.


 It is called a virtual machine because it doesn't physically exist.
 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.
 JVM, JRE, and JDK are platform dependent because the configuration of
each OS is different from each other.
 There are three notions of the JVM: specification, implementation,
and instance.

What JVM does


The JVM performs the following main tasks:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment

JVM provides definitions for the:


 Memory area
 Class file format
 Register set
 Garbage-collected heap
 Fatal error reporting etc.

What is JVM
1. The JVM is loaded with many specifications that work to run a Java program.
2. It is platform-dependent by nature
i.e for each type of OS there is a different JVM (Java Virtual Machine).
3. It comes built-in with the JDK package.
4. The platform-independent feature of Java is made possible by JVM.
The JVM is the one that can convert the Java bytecode into machine code.
The conversion of Java Bytecode to machine code takes place in multiple
specifications inside a JVM.
5. An implementation is known as JRE (Java Runtime Environment).
6. When java command written on cmd to run the java class, an instance of JVM is
created.

JVM Architecture (The conversion of Java Bytecode to machine code takes


place in multiple specifications inside a JVM)

Java Memory
Runtime allocated by JVM
System
1) Classloader

Classloader is a subpart of JVM which load class files. Whenever we run the java
program, it is loaded first by the classloader.

There are three built-in classloaders in Java.

1. Bootstrap ClassLoader:

 This is the first classloader which is the super class of Extension classloader.
 It loads rt.jar file which contains all class files of Java Standard Edition like
java.lang package classes, java.net package classes, java.util package classes,
java.io package classes, java.sql package classes etc.

2. Extension ClassLoader:

 This is the child classloader of Bootstrap and parent classloader of System


classloader.
 It load jar files located inside $JAVA_HOME/jre/lib/ext directory.

3. System/Application ClassLoader:

 This is the child classloader of Extension classloader.


 It loads the classfiles from classpath.
 By default, classpath is set to current directory.
 You can change the classpath using "-cp" or "-classpath" switch.
 It is also known as Application classloader

Method Area
stores per-class structures such as the runtime constant pool, field and method data,
the code for methods.

Heap
It is the runtime data area in which objects are allocated.

Stack
 Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return.
 A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.
Program Counter Register
PC contains the address of the JVM instruction currently being executed.

Native Method Stack


It contains all the native methods used in the application.

Execution Engine
 A virtual processor
 Interpreter: Read bytecode stream then execute the instructions.
 Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles
parts of the byte code that have similar functionality at the same time, and hence
reduces the amount of time needed for compilation. Here, the term "compiler"
refers to a translator from the instruction set of a Java virtual machine (JVM) to the
instruction set of a specific CPU.

Java Native Interface(JNI)


 communicate with another application written in different language like C, C++,
Assembly etc.
 Java uses JNI framework to send output to the Console or interact with OS libraries.

JRE
 Java Runtime Environment.
 It is also written as Java RTE.
 The JRE is a set of software tools which are used for developing Java
applications.
 It is the implementation of JVM.
 It physically exists.

JRE= set of libraries + other files that JVM uses at runtime.


JDK
 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

JDK contains ((private JVM)+few other resource interpreter/loader (java),compiler


(javac),archiver (jar),document generator (Javadoc),etc). to complete the
development of a Java Application.
Syntax

class Simple
{
public static void main(String
args[])
{ To
javac Simple.java
compile:
System.out.println("Hello Jav
a"); To
java Simple
execute:
}

Simple class name


class is a keyword used to declare a class in Java.
public keyword is access modifier that represents visible to all.
static is a keyword.
In how many ways we can write a Java program?
Changing the sequence of the modifiers, method prototype
static public void main(String args[])
2) The subscript notation in the Java array can be used after type, before the
variable or after the variable.

Let's see the different codes to write the main method.


public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
3) You can provide var-args support to the main() method by passing 3 ellipses (dots)
public static void main(String... args)
4) Having a semicolon at the end of class is optional in Java.
Let's see the simple code.
class A{
static public void main(String... args){
System.out.println("hello java4");
}
};

Valid Java main() method signature


public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)
final strictfp public static void main(String[] args)

Invalid Java main() method signature


public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)
Precedence(3 rules)
operator precedence, operator associativity, order of operand evalution

Operator precedence.
1 + 2 * 3 is treated as 1 + (2 * 3),
1 * 2 + 3 is treated as (1 * 2) + 3 because the multiplication operator has a higher
precedence than the addition operator.
You can use parentheses to override the default operator precedence rules.

Operator associativity.
1. 72 / 2 / 3 is treated as (72 / 2) / 3 left-to-right
2. You can use parentheses to override the default operator associativity rules.
3. x = y = z = 17 is treated as x = (y = (z = 17))
4. x <= y <= z and x++-- invalid expressions

Level Operator Description Associativity

() parentheses
16 [] array access left-to-right
. member access

++ unary post-increment
15 -- left-to-right
unary post-decrement
+ unary plus
- unary minus
14 ! right-to-left
unary logical NOT
~ unary bitwise NOT

() cast
13 new right-to-left
object creation

12 * / % multiplicative left-to-right

+ - additive
11 + left-to-right
string concatenation

<< >>
10 >>> shift left-to-right

< <=
9 > >= relational left-to-right
instanceof

==
8 != equality left-to-right

7 & bitwise AND left-to-right

6 ^ bitwise XOR left-to-right

5 | bitwise OR left-to-right

4 && logical AND left-to-right

3 || logical OR left-to-right

2 ?: ternary right-to-left

= += -=
*= /= %=
1 &= ^= |= assignment right-to-left
<<= >>= >>>=

0 -> lambda expression arrow right-to-left


Exercises.
1. What is the result of the following code fragment? Explain.

System.out.println("1 + 2 = " + 1 + 2);


System.out.println("1 + 2 = " + (1 + 2));

Answer: 1 + 2 = 12 and 1 + 2 = 3, respectively. If either (or both) of the operands of


the + operator is a string, the other is automatically cast to a string. String concatenation and
addition have the same precedence and are left-to-right associative. The parentheses in the
second statement ensures that the second + operator performs addition instead of string
concatenation.

2. What does the following code fragment print?

System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);

Answer: 3abc and abc12, respectively. The + operator is left-to-right associative, whether it is
string concatenation or addition.
3. Add parentheses to the following expression to make it more clear to other programmers.

year % 4 == 0 && year % 100 != 0 || year % 400 == 0

Answer: LeapYear.java shows a variety of equivalent expressions, including the following


reasonable alternative:

((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)


4. What is the value of the expression 1 <= 2 <= 3? Explain.

Answer: It leads to a compile-time error. The compiler parses the expression 1 <= 2 <=
3 as (1 <= 2) <= 3. This leads to a compile-time error because you can't use the <= operator
to compare a boolean to an int. So, while the relational operators have left-to-right
associativity, this property is not useful.
5. What is the result of the following code fragment? Explain.

boolean a = false;
boolean b = false;
boolean c = true;
System.out.println(a == b == c);

Answer: It prints true. The equality operator is left-to-right associative, so a == b evaluates


to true and this result is compared to c, whihc yields true.

6. What is the value of the expression +-~17? Explain.

Hint: The unary operators are right-to-left associative.


7. What is the value of the expression --17? Explain.

Answer: It leads to a compile-time error because Java parses -- as the pre-decrement operator
(and not two unary minus operators).
8. What is the value of z after executing the following code fragment? Explain.

int x = 5;
int y = 10;
int z = ++x * y--;

9. What is the value of y after executing the following code fragment? Explain.

int x = 10;
int y = x+++x;

10. What is the value of x after executing the following code fragment? Explain.

int x = 10;
++++x;

Answer: It leads to a compile-time error. Java parses it as ++(++x). The ++x term increments
the variable x and evaulates to the integer 11. This leads to a compile-time error becasue you
the pre-increment operator must be applied to a variable (not an integer). So, while the pre-
increment operator has right-to-left associativity, this property is not useful.

You might also like