01_1_Java_Intro
01_1_Java_Intro
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!)
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
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.
Early Binding
Fast Execution
Late Binding
Slow Execution
History :
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.
Application
According to Sun, 3 billion devices run Java.
1) Standalone Application
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
4) Mobile Application
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.
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
Bytecode Verifier: Checks the code fragments for illegal code that can
violate access rights to objects.
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
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.
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.
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:
3. System/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.
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.
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.
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
class Simple
{
public static void main(String
args[])
{ To
javac Simple.java
compile:
System.out.println("Hello Jav
a"); To
java Simple
execute:
}
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
() 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
5 | bitwise OR left-to-right
3 || logical OR left-to-right
2 ?: ternary right-to-left
= += -=
*= /= %=
1 &= ^= |= assignment right-to-left
<<= >>= >>>=
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.
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 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.