You are on page 1of 60

Java Introduction

Sandip Sagare

Mr. Sandip Sagare


What is JAVA?

• Java is a programming language and


computing platform first released by Sun
Microsystems in 1995.
• There are lots of applications and websites
that will not work unless you have Java
installed, and more are created every day.
• Java is fast, secure, and reliable. From laptops
to datacenters, game consoles to scientific
supercomputers, cell phones to the Internet,
Java is everywhere!
Mr. Sandip Sagare
Introduction To Java
• In late 1995, the java programming language burst onto
the Internet scene and in minimum time becomes popular.
• It is an extremely solidly engineered language that has
gained acceptance by all major vendors, except for
Microsoft.
• Its built-in security and safety features are reassuring both
to programmers and to the users of java programs.
• Java even has built-in support that makes advanced
programming tasks such as network programming,
database connectivity and multithreading etc.

Mr. Sandip Sagare


JAVA Buzzwords
• Simple
– Syntax- No need of header files, pointer syntax,
structures, unions, operator overloading, virtual
base classes, and so on.
– Another aspect of being simple is being
small.(J2ME)
• Object Oriented
– Simply stated, object-oriented design is a
technique for programming that
– focuses on the data (= objects) and on the
interfaces to that object.
Mr. Sandip Sagare
• Distributed
– Java has an extensive library of routines for
coping with TCP/IP protocols like HTTP and
FTP.
– Java applications can open and access
objects across the Net via URLs with the same
ease as when accessing a local file system.

– We have found the networking capabilities of


Java to be both strong and easy to use

Mr. Sandip Sagare


• Robust
– Java is intended for writing programs that
must be reliable in a variety of ways.
– The Java compiler detects many problems that,
in other languages, would show up only at run
time.
• Secure
– Java is intended to be used in networked/
distributed environments. Toward that end, a
lot of emphasis has been placed on security.
Java enables the construction of virus-free,
tamper-free systems
Mr. Sandip Sagare
• Here is a sample of what Java's security
features are supposed to keep a Java
program from doing:
1. Overrunning the runtime stack, a common
attack of worms and viruses.
2. Corrupting memory outside its own process
space
3. Reading or writing local files when invoked
through a security-conscious class loader, like a
Web browser that has been programmed to forbid
this kind of access.

Mr. Sandip Sagare


• Architecture Neutral
– The compiler generates an architecture-
neutral object file format—the compiled code is
executable on many processors, given the
presence of the Java run time system.

• Portable
– Unlike C and C++, there are no
“implementation-dependent” aspects of the
specification.
– The sizes of the primitive data types are
specified, as is the behavior of arithmetic on
them.(e.g. int 32 bit)
Mr. Sandip Sagare
• Interpreted
– The Java interpreter can execute Java byte
codes directly on any machine to which the
interpreter has been ported.

– Since linking is a more incremental and


lightweight process, the development
process can be much more rapid and
exploratory.

Mr. Sandip Sagare


• High Performance

– While the performance of interpreted


bytecodes is usually more than adequate,
there are situations where higher
performance is required.

– The bytecodes can be translated on the fly


(at run time) into machine code for the
particular CPU the application is running on.

Mr. Sandip Sagare


• Multithreaded
– Benefits of multithreading are better
interactive responsiveness and real time
behavior.
• Dynamic
– In a number of ways, Java is a more dynamic
language than C or C++.
– It was designed to adapt to an evolving
environment. Libraries can freely add new
methods and instance variables without any
effect on their clients.
– In Java,finding out run time type information is
straightforward.
Mr. Sandip Sagare
JAVA Compilation and
Execution

Mr. Sandip Sagare


Mr. Sandip Sagare
Java Platform

• A platform is the hardware or software


environment in which a program runs.
• The Java platform has two components:
– The Java Virtual Machine
– The Java Application Programming Interface (API)

Mr. Sandip Sagare


JVM

Mr. Sandip Sagare


Run-time data areas

Mr. Sandip Sagare


Run-time data areas
 Method area: Method area is the memory block, which stores the class code,
code of the variables, and code of the methods in the Java program. (Method
means functions written in a class)
 Heap: This is the area where objects are created. Whenever JVM loads a class, a
method and a heap area are immediately created in it.
 Java Stacks: Method code is stored on Method area.But while running a method,
it needs some more memory to store the data and results. This memory is allotted
on Java stacks. So, Java stacks are memory areas where Java methods are
executed.
 While executing methods, a separate frame will be created in the Java stack, where the
method is executed.
 JVM uses a separate thread (or process) to execute each method. .
 PC (Program Counter) registers: These are the registers (memory areas), which
contain memory address of the instructions of the methods. If there are 3
methods, 3 PC registers will be used to track the instructions of the methods.
 Native method stacks: Java methods are executed on Java stacks. Similarly,
native methods (for example CjC++ functions) are executed on Native method
stacks. To execute the native methods, generally native method libraries (for
example CjC++ header mes) are required.
 These header mes are located and connected to JVM by a program, called Native
method interface.

Mr. Sandip Sagare


Why Combination of Interpreter & JIT Compiler?
 Execution engine contains interpreter and JIT (Just In Time) compiler, which are
responsible for converting the byte code instructions into machine code so that
the processor will execute them.
 Most of the JVM implementations use both the interpreter and JIT compiler
simultaneously to convert the byte code. This technique is also called adaptive
optimizer.
 Generally, any language (like C/C++, Fortran, COBOL, etc.) will use either an
interpreter or a compiler to translate the source code into a machine code.
 But in JVM, we got interpreter and JIT compiler both working at the same time
on byte code to translate it into machine code.
 Now, the main question is why both are needed and how both work
simultaneously?
 To understand this, let us take some sample code. Assume these are byte code
instructions
print a;
print b;
repeat the following 10 times by changing values from 1 to 10:
print a;
Mr. Sandip Sagare
Why Combination of Interpreter & JIT Compiler?
 When, the-interpreter starts execution from 1st instruction,
it converts print a; into machine code and gives it to the microprocessor.
 For this, say the interpreter has taken 2 nanoseconds time.
 The processor takes it, executes it, and the value of a is displayed.
 Now, the interpreter comes back into memory and reads the 2nd instruction print b;
 To convert this into machine code, it takes another 2 nanoseconds.
 Then the instruction is given to the processor and it executes it.
 Next, the interpreter comes to the 3rd instruction, which is a looping statement
print a;
 This should be done 10 times and this is known to the interpreter.
 Hence, the first time it converts print a into machine code.
 It takes 2·nanoseconds for this. after giving this instruction to the processor, it comes back
to memory and reads the print a instruction the 2nd time and converts it to machine code.
 This will take another 2 nanoseconds. This will be given to the processor and the interpreter
comes back and reads print a again and converts it 3rd time taking another 2 nanoseconds.
 Like this, the interpreter will convert the print a instruction for 10 times,
 consuming a total of 10 x 2 = 20 nanoseconds. This procedure is not efficient in terms of
time.
 That is the reason why JVM does not allocate this code to the interpreter.
 It allots this code to the JIT compiler.
Mr. Sandip Sagare
JAVA Jargon (Expression used by professionals)

• JDK
• JRE
• Java Platform Edition
– J2SE or SE
– J2EE or EE
– J2ME or ME
• JVM- Integral part of jre
• JRE-(private and public)
• IDE- Netbean, Eclipse
Mr. Sandip Sagare
Path and Classpath

• path- path is operating system


environment variable which is used to
locate javac file

• Classpath- It is java platforms


environment variable which is used to
locate .class file.

Mr. Sandip Sagare


First Java Program
FirstProgram.java
class FirstProgram
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
Mr. Sandip Sagare
main() - method

• It must be public and static


• return type must be void
• Parameter must be String args[];
• Main thread - invoke main method
• Daemon thread – It is responsible for
reclaiming the object.(which provides
service to the application.)
– e.g. garbage collector.
Mr. Sandip Sagare
Primitive Data Types

Mr. Sandip Sagare


Mr. Sandip Sagare
Operators:

Mr. Sandip Sagare


Wrapper Classes:
• If we want to convert primitive data
type into object type then we have to
take help of wrapper classes.
Primitive Wrapper Class
boolean Boolean
char Character
byte Byte
Short Short
int Integer
long Long
float Float
Mr. Sandip Sagare

double Double
Boxing and Unboxing
• Boxing – it is process of converting value of
primitive data type into object data type .
– e.g.

• Unboxing: It is process of converting object


data type into primitive data type.
– E.g.

Mr. Sandip Sagare


Mr. Sandip Sagare
Command Line argument
CmdLineProgram.java

Mr. Sandip Sagare


How to take input using
Scanner Class:

Mr. Sandip Sagare


Widening and Narrowing

• Widening - Process of converting value of


narrower data type into wider data type.
• E.g.
int number2=100;
double number2=number1;

Mr. Sandip Sagare


Mr. Sandip Sagare
• Narrowing -Process of converting value
of wider data type into narrower data
type.
• E.g.
double number1=10.5;
int number2=(int)number1;

Mr. Sandip Sagare


Variable and Datatype in Java:

• Variable is name of reserved area allocated


in memory.

Mr. Sandip Sagare


• Local Variable-
– A variable that is declared inside the method is
called local variable.
• Instance Variable-
– A variable that is declared inside the class but
outside the method is called instance variable .
– It is not declared as static.
• Static Variable-
– A variable that is declared as static is called static
variable. It cannot be local.

Mr. Sandip Sagare


Mr. Sandip Sagare
Access Specifiers:

1. Private
2. Public
3. Protected
4. Default (or Package scope or top level
scope)

Mr. Sandip Sagare


 Private: The access level of a private modifier is only within the
class. It cannot be accessed from outside the class.
 Default: The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you
do not specify any access level, it will be the default.
 Protected: The access level of a protected modifier is within the
package and outside the package through child class. If you do not
make the child class, it cannot be accessed from outside the
package.
 Public: The access level of a public modifier is everywhere. It can
be accessed from within the class, outside the class, within the
package and outside the package.

Mr. Sandip Sagare


Types of Constructors:

1. Default constructor(compiler)
2. Parameterless constructor(Instance)
3. Parametrized constructor

Mr. Sandip Sagare


JAVA Platform

1. JAVA API-Methods which are


implemented by java platform to
create java application
– Java doc – java platform API
specification.
2. JVM

Mr. Sandip Sagare


Array in JAVA -

• Homogenous elements in contiguous


memory location.
• In java arrays are object.
• Types of array
1. Single Dimensional
2. Multi-Dimensional
3. Ragged array

Mr. Sandip Sagare


1. Single Dimensional
E.g.
int[] arr=new int[5];
Or
int[] arr=new int[]{1,2,3,4,5};
2. Multi Dimensional
E.g.
int[][] arr=new int[5][5];

Mr. Sandip Sagare


3. Ragged Array
-is array of array whose column size is
not fixed.
e.g.
int[][] arr=new int[3][];
arr[0]=new int[2];
arr[1]=new int[3];
arr[2]=new int[4];

Mr. Sandip Sagare


for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
System.out.println(“arr =”+arr[i][j]);
}
}

Mr. Sandip Sagare


Types of loop

• 4-types
1. for
2. for-each
3. while
4. do-while

Mr. Sandip Sagare


for-each loop in java

Mr. Sandip Sagare


String class
• String is reference type not a primitive type
• String class belongs to java.lang package.
• String class is final class.
– We can create object of string in two types.
1. String s1 = new String(“Hello”); //managed heap
2. String s2 = “Hello”; //String pool
• == :- equal to operator always compares
object reference.
• equals() :- method is always used to check
state of the Object for equality.

Mr. Sandip Sagare


Mr. Sandip Sagare
String literal pool:
– The JVM performs some tricky operations while instantiating
string object to increase performance and decrease memory
overhead.
– So, the String class keeps a pool of string.

Mr. Sandip Sagare


• Methods for String class
– charAt(int index)
– compareTo(String str)
– compareToIgnoreCase(String str)
– concat(String str)
– contains(CharSeqience s)
– length()
– toString()
– trim()
– toUpperCase()
• etc ….
Mr. Sandip Sagare
String are Immutable:
• Immutable simply means unmodifiable or
unchangeable
• Once string object is created its data or state can't be
changed but a new string object is created.
class Testimmutablestring{
public static void main(String args[]){
String s=“DKTE";
s.concat(" ICHALKARANJI");
//concat() method appends the string at the end
System.out.println(s);
}
}
Mr. Sandip Sagare
StringBuffer -class
• It belongs to java.lang package
• It is final class
• It is mutable class

Mr. Sandip Sagare


• Study below classes (Homework)
1. StringTokenizer
2. StringBuilder

• What is difference between StringBuffer


and StringBuilder class?

Mr. Sandip Sagare


Nested & Inner Classes:
• Nested Class:
-When we write class inside class is
called as Nested Class.
And nested class either static or non-static
1. Static nested classes simply called as
”nested class”
2. Non-static Nested class is called as
“Inner class”.

Mr. Sandip Sagare


//Inner class Example or non-static nested class.
Class Outer {
private int number1=10;
private int number2=20;
class Inner {
private int a=40;
private int b=50;
public void print() {
System.out.println(“a = ”+this.a);
System.out.println(“b = ”+this.b);
}
} // Inner class
public void print() {
System.out.println(“number1 = ”+this.number1);
System.out.println(“number2 = ”+this.number2);
}
} //outer class
Mr. Sandip Sagare
public class Program
{
public static void main(String args[])
{
Outer out =new outer();
out.print();

Outer.Inner in=new outer().new Inner();


in.print();
}
}

Mr. Sandip Sagare


Static Nested Class
• We can declare Nested Class as Static
Class Outer {
private int number1=10;
private static int number2=20;
static class Nested{
private int a=40;
private static int b=50;
public void print() {
System.out.println(“a = ”+this.a);
System.out.println(“b = ”+this.b);
Outer out=new Outer();
System.out.println(“number1 = ”+out.number1);
System.out.println(“number2 = ”+number2);
}
} // Inner class
Mr. Sandip Sagare
public void print()
{ System.out.println(“number1 = ”+this.number1);
System.out.println(“number2 = ”+this.number2);
Nested n=new Nested();
System.out.println(“a = ”+n.a);
System.out.println(“b = ”+Nested.b);

}
} //outer class

• Outer out=new Outer();//outer class object


• Outer.Nested n=new Outer.Nested();//nested class object
Mr. Sandip Sagare
When to use?
• nested classes enable you to logically
group classes that are only used in one
place, increase the use of encapsulation,
and create more readable and
maintainable code.
• Inner classes can be hidden from other
classes in the same package.

Mr. Sandip Sagare

You might also like