0% found this document useful (0 votes)
33 views41 pages

VXL It-Java-1

Uploaded by

korapatiusharani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views41 pages

VXL It-Java-1

Uploaded by

korapatiusharani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

VXL IT

Solutions
 JAVA:
 Java language suited for networks, ie internet..
 Inter connection of computers called network, network contain two or more computers
connected with or without a cable, ie they are connected with the help of satellite and the
data can be sent or received via satellite in the form of electromagnetic waves. so network
may or may not have a cable
 The primary purpose of network is resource sharing, and network follows
client –server architecture.
 In c-lanaguage:
 p.c---compile—p1.obj---execute—p.exe

 In java program first:


 p.java---compile—p.class---JVM---p.exe

 Here the processor cant understand byte code instructions and cant
execute them
 in java we have JVM –which is a program written to understand the
byte code instructions and convert into machine code
 Jvm is not a machine –it s a program which is available in internet
 Class file contains byte code instructions for the JVM and is they
are system independent
 .exe file contains machine language instructions for the processor
and they are system dependent..
 Java is a object oriented programming language and a
platform independent, high level, robust, secured.
 James Gosling, Mike Sheridan, and Patrick Naughton initiated
the Java language project in June 1991. The small team of sun
engineers called Green Team.
 Originally designed for small, embedded systems in electronic
appliances like set-top boxes.
 Firstly, it was called "Green talk" by James Gosling and file
extension was .gt.
 After that, it was called Oak and was developed as a part of the
Green project.
In 1995, Oak was renamed as "Java" because it was already a
trademark by Oak Technologies.
Java is an island of Indonesia where first coffee was produced
(called java coffee).
Originally developed by James Gosling at Sun Microsystems (which
is now a subsidiary of Oracle Corporation) and released in 1995.
In 1995, Time magazine called Java one of the Ten Best
Products of 1995.
 JDK 1.0 released in (January 23, 1996).
Java Version History:

 JDK Alpha and Beta (1995) JDK 1.0 (23rd Jan, 1996)

JDK 1.1 (19th Feb, 1997) J2SE 1.2 (8th Dec, 1998)

J2SE 1.3 (8th May, 2000) J2SE 1.4 (6th Feb, 2002)

J2SE 5.0 (30th Sep, 2004) Java SE 6 (11th Dec,

2006)

Java SE 7 (28th July, 2011) (oracle)

Java SE 8 (18th March, 2014) Java SE 9 (21st Sep

2017)
Java SE 10 (20th Mar 2018) Java SE 11 (September
2018)
Java SE 12 (March 2019) Java SE 13 (September
2019)
Java SE 14 (Mar 2020) Java SE 15 (September
The acquisition of Sun Microsystems by Oracle Corporation was
completed on January 27, 2010. Significantly, Oracle, previously
only a software vendor, now owned both hardware and software
product lines from Sun (e.g. SPARC Enterprise and Java,
respectively).
Parts of java:
Java SE
Java EE
 Java ME
Features of Java
 Simple
 Object-Oriented
 Platform Independent
 secured
 Robust
 Portable
 High Performance
 Distributed
 Multi-threaded
Object Oriented − In Java, everything is an Object. Java can be
easily extended since it is based on the Object model.

Platform Independent − Unlike many other programming


languages including C and C++, when Java is compiled, it is not
compiled into platform specific machine, rather into platform
independent byte code.
This byte code is distributed over the web and interpreted by
the Virtual Machine (JVM) on whichever platform it is being run on.

Simple − Java is designed to be easy to learn. If you understand the


basic concept of OOP Java, it would be easy to master.

Secure − With Java's secure feature it enables to develop virus-free,


tamper-free systems. Authentication techniques are based on public-
key encryption.

Robust − Java makes an effort to eliminate error prone situations by


emphasizing mainly on compile time error checking and runtime
checking.

Distributed − information is distributed on various computers on a


network, using java we can write a programs which capture the
Portable − Being architecture-neutral and having no
implementation dependent aspects of the specification makes Java
portable. Compiler in Java is written in ANSI C with a clean
portability boundary.

Multithreaded − With Java's multithreaded feature it is possible to


write programs that can perform many tasks simultaneously. This
design feature allows the developers to construct interactive
applications that can run smoothly.

Interpreted − Java byte code is translated on the fly to native


machine instructions and is not stored anywhere. The development
process is more rapid and analytical since the linking is an
incremental and light-weight process.

High Performance − With the use of Just-In-Time compilers, Java


enables high performance.
Applications of Java:

Java is widely used in every corner of world and of human life. Java
is not only used in software's but is also widely used in designing
hardware controlling software components. There are more than
930 million JRE downloads each year and 3 billion mobile phones
run java.

Following are some other usage of Java :


• Developing Desktop Applications

• Web Applications like Linkedin.com, Snapdeal.com etc

• Mobile Operating System like Android

• Embedded Systems

• Robotics and games etc.


Secured: Java is secured because:
1.No explicit pointer
2.Programs run inside virtual machine .
JDK AND JRE,JVM:
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.
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.

JRE: The Java Runtime Environment is a set of software tools which are used
for developing java applications. It is used to provide runtime environment. It
is the implementation of JVM. It physically exists. It contains set of libraries
+ other files that JVM uses at runtime.
JVM:
JVM (Java Virtual Machine) is an abstract machine. It is a
specification that provides runtime environment in which java byte
code can be executed.
JVMs are available for many hardware and software platforms (i.e.
JVM is platform dependent).
Java virtual machine which converts byte code instructions to object
code
The JVM performs following operation:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment
Class loader: Class loader is a subsystem of JVM that is used to load
class files.
Class (Method) Area: Class(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: Here normal variables are stored
Program Counter Register :PC (program counter) register. It contains
the address of the Java virtual machine instruction currently being
executed.
Native Method Stack : It contains all the native methods used in the
application.
Execution Engine:
It contains:
1) A virtual processor
Installation of java:
Install the JDK current version from oracle web site.
Set the path:
Temporary path :
To set the temporary path of JDK, you need to follow following steps:
Open command prompt
copy the path of jdk/bin directory
write in command prompt: set path= “copied_ path”;
for ex:
set path= C:\Program Files\Java\jdk1.6.0_23\bin;
Permanent path:
Go to My Computer properties -> advanced tab -> environment variables -
> new tab of user variable -> write path in variable name -> write path of bin folder in
variable value -> ok -> ok -> ok
Structure of java program

A Java program is a collection of classes. Classes group objects with


similar characteristics and behaviors. The structure of Java program
can be divided into the following sections: –
Documentation Section
Package statement:- The package statement identifies the package that the class
belongs to. We put classes into sensible groups with the help of packages.
Import statements: The import statements import other classes into the current class.
This allows the current class to use the methods and variables of the imported classes.
Class definition: The class definition defines the class. A class definition is
composed of –
 class name
 class variables
 methods
 constructors
Main method: The Java program starts with the main method.
Body of the class:- The body of the class contains the code for the class. The code
inside a class can be divided into methods, constructors, and variables.
Creating hello java example:
Import java.lang.System;
Import java.lana.String; //import java.lang.*;
class Firstprogram{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
----------------------------------------

Class keyword is used to declare a class in java.


Public keyword is an access modifier which represents visibility, it means it is visible to all.
Static is a keyword, if we declare any method as static, it is known as static method. The
advantage of static method is that there is no need to create object to invoke the static
method.
The main method is executed by the JVM, so it doesn't require creating object to invoke the
main method. So it saves memory.
String [] args is used for command line argument.
System.out.println () is used print statement in output screen.
Class Declaration:
“public class First Program” declares a class which refers to a Object Oriented
Concept. In Java, everything must be placed in a class. “First Program” is the identifier
that specifies the name of the class.

Brackets {}:
The contents of a java class starts with “{“ and ends with “}”. The statement
in between these brackets are used to define behavior of the Class.

Public Static void main(String args[]):

This line declares a method called main. This is similar to the main method in
C/C++. Every Java Application must include a main method. It acts as the starting
point of execution for an application. A Java application can have many classes, but only
one should contain this main method.

System.out.println:
This is similar to printf(). Since java is object oriented language, every
method must be part of an object. The println() method is a member of the out object,
which is a static data member of System. This line prints “Welcome to Java Class” to the
screen.
Integrated Development Environment(IDE): An integrated development
environment (IDE) is a software suite that consolidates basic tools required to write
and test software.
You can run your java file in any of the IDE’s below.
1. Notepad
2. Netbeans
3. Eclipse etc.
Internal Details of Hello Java Program
What happens at compile time?
At compile time, the Java file is compiled by Java Compiler (It does not interact
with OS) and converts the Java code into bytecode.
What happens at runtime?
Q)Can you save a Java source file by another name than the class name?
Yes, if the class is not public. It is explained in the figure
given below:

Javac Hard.java
To compile:

To execute: java Simple

Observe that, we have compiled the code with file name but running the program with
class name. Therefore, we can save a Java program other than class name.
Q) Can you have multiple classes in a java source file?
Yes, like the figure given below illustrates:
public class Computer
{
Computer()
{ System.out.println("Constructor of Computer class.");
}
void computer_method()
{
System.out.println("Power gone! Shut down your PC soon...");
}
public static void main(String[] args)
{ Computer c = new Computer();
Laptop l = new Laptop();
c.computer_method();
l.laptop_method();
}}
class Laptop
{ Laptop()
{
System.out.println("Constructor of Laptop class."); }
void laptop_method()
{
System.out.println("99% Battery available.");
} }
Data types define the type and value range of the data for the different
types of variables, constants, method parameters, returns type, etc.
The data type tells the compiler about the type of data to be stored and the
required memory. To store and manipulate different types of data, all variables
must have specified data types.
Java data types are categorized into two parts −
Primitive Data Types
Reference/Object Data Types
Java Primitive Data Types
Primitive data types are predefined by the language and named by a keyword.
There are eight primitive data types supported by Java. Below is the list of the
primitive data types:
Non-primitive data types: The non-primitive data types include
Classes, Interfaces, and Arrays.
Data Type Default Value Default size

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte


Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
•String - stores text, such as "Hello". String values are surrounded by double
quotes
•int - stores integers (whole numbers), without decimals, such as 123 or -123
•float - stores floating point numbers, with decimals, such as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
•boolean - stores values with two states: true or false
data type variable-name=value;
Examples :
int myNum = 5; String firstName = "John ";
float myFloatNum = 5.99f; String lastName = "Doe";
char myLetter = 'D'; String fullName = firstName + lastName;
boolean myBool = true;
System.out.println(fullName);
String myText = "Hello";

int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Java Variables
A variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
A variable is the name of a reserved area allocated in
memory. In other words, it is a name of the memory location.
It is a combination of "vary + able" which means its value can
be changed.
Variable is a name of memory location.
Types of Variables
Local Variable
A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even
aware that the variable exists.
A local variable cannot be defined with "static" keyword.
Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the
class. Memory allocation for static variables happens only once when the class is loaded
in the memory.
Example to understand the types of variables in java
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
Java Variable Example: Add Two Numbers
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Java Variable Example: Narrowing (Typecasting)
public class Simple{
public static void main(String[] args){
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}}
INPUT /OUTPUT STMTS:
 There are various ways to read input from the keyboard
 JAVA Scanner class is one of them to use read the data from the keyboard and which is
available in the package “java.util. Scanner”
Java Scanner class is widely used to parse text for string and primitive types using
regular expression.
Java Scanner class extends Object class and implements Iterator and Closable interfaces.
nextLine()---- it moves the scanner position to the next line and returns the value as a
string.

nextInt()---- it scans the next token as an int value.


nextFloat()--- it scans the next token as a float value.
next()-- it returns the next token from the scanner.
Scanner s=new Scanner(System.in );
Syntax to read data:
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
Syntax to print the data on console:

System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);


BufferedReader Class:
Java BufferedReader class is used to read the text from a
character-based input stream. It can be used to read data line by line
by readLine() method. It makes the performance fast.
Connect the keyboard to input stream object:
InputStreamReader ir= new InputStreamReader(System.in);
Connect the InputStreamReader to BufferedReader:
BufferedReader br=new BufferedReader(ir);
We can also write like this:

BufferedReader br=new BufferedReader(new InputStreamReader(System


.in));
Accepting a single character from keyboard:
Char ch=br.read();-- it returns ascii values
Char ch=(char)br.read(); --it converts into char type
Accepting string from keyboard:
String name=br.readLine();
Accepting integers and float values:
int n=Integer.parseInt(br.readLine());
float n=Float.parseFloat(br.readLine());
Java Keywords:

Keywords or Reserved words are the words in a language that are used for
some internal process or represent some predefined actions. These words are
therefore not allowed to use as a variable names or objects. Doing this will result
into a compile time error.

Abstract continue for new switch


assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

You might also like