VXL It-Java-1
VXL It-Java-1
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
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)
2006)
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.
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.
• Embedded Systems
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
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.
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:
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
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
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.
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.