Lab_01-OOP
Lab_01-OOP
Note: Submit this lab hand-out in the next lab with attached solved activities and exercises
Submission Profile
Name: Submission date (dd/mm/yy):
Enrollment ID: Receiving authority name and signature:
Comments:
__________________________________________________________________________
Instructor Signature
What is JAVA?
Java was developed by Sun Microsystems in 1995. It is a programming language as well
as platform. Java is among the most popular programming languages out there, mainly
because of how versatile and compatible it is. Java is general purpose programming language
as it is used for software development, mobile applications, web servers, and client-side web
applications. It is the native language of the Android operating system, which operates on
Android phones and tablets.
Versions of Java
There are many java versions that has been released.
JDK Alpha and Beta (1995)
JAVA 1.0 (23rd Jan, 1996)
JAVA 1.1 (19th Feb, 1997)
JAVA 1.2 (8th Dec, 1998)
JAVA 1.3 (8th May, 2000)
JAVA 1.4 (6th Feb, 2002)
JAVA 5.0 (30th Sep, 2004)
JAVA 6 (11th Dec, 2006)
JAVA 7 (28th July, 2011)
JAVA 8 (18th March, 2014)
JAVA 9 (21th Sep, 2017)
JAVA 10 (20th March, 2018)
JAVA 11 (25th Sep 2018)
JAVA 12 (19th March 2019)
JAVA 13 (17th Sep 2019)
JAVA 14 (17th March 2020)
JAVA 15 (15th Sep 2020)
JAVA 16 (16th March 2021)
Java Platforms
According to Oracle, there are four platforms of the Java programming language
Java Platform, Standard Edition (Java SE)
Java Platform, Enterprise Edition (Java EE)
Java Platform, Micro Edition (Java ME)
JavaFX/OpenJFX
Features of JAVA
The following are Java's main features:
• Object Oriented − In Java, everything is an object, creating objects that contain data
and methods.
• Architecture-neutral − Traditionally, we would have to recompile a program for
every system that it was going to run on because all systems have a different idea of
what their machine code should look like. Java compiler generates an architecture-
neutral object file format called as bytecode, which makes the compiled code
executable on many machines but with the presence of Java runtime system.
• 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 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.
• Portable − Bytecode can be run by any system in which Java is installed. This is
because when java is installed, Java virtual machine is also installed that is specific to
that system. It is this machine's responsibility to convert the bytecode into the final
instructions of that particular machine.
By making it the system's responsibility to do this final conversion, Java has created a write
once, run anywhere language where anyone can hand you a Java program and you can run it
on your machine
“Write once, run everywhere”
JDK
• It stands for Java Development Kit, is a software development environment used for
developing Java applications and applets.
• It compiles and executes new and already built applications.
• It is a collection of development tools as well as Java Runtime Environment (JRE), an
interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation
generator (Javadoc) and other tools needed in Java development.
JRE
• It stands for Java Runtime Environment. The Java Runtime Environment provides the
minimum requirements for executing a Java application.
• It consists of the Java Virtual Machine (JVM), interpreter, JIT, core classes,
and supporting files.
JVM
• It stands for Virtual Machine (JVM)
• It is responsible for executing bytecode where interpreter provides machine code for
the current machine and has JIT as well.
JIT
• It stands for Just-in-time Compiler, is the part of the Java Virtual Machine (JVM) that is
used to speed up the execution time.
• JIT interprets parts of the bytecode that have similar functionality at the same time and
hence reduces the amount of time needed for full interpretation.
Installation in progress
We need to set Java path so that whenever java program will be compiled or executed it can
locate JDK easily. Path can be set in two ways i.e. temporarily and permanently.
To set java path temporarily, open command prompt and run command “set path=C:\Program
Files\Java\jdk-21\bin”. Once the command prompt is closed, java path needs to be set again.
To set java path permanently, this path “C:\Program Files\Java\jdk-21 \bin” should be added
as per following steps.
Go to Control Panel > System and Security > System, and then click Advanced system settings
to view System properties window. In system properties window press advanced tab and
click on Environment variables to add new path. Finally click ok and you are all set.
To check version information of JDK, JRE and JVM, open command prompt and run command
“java -version”.
ii. Go to search bar in taskbar, write cmd and Open Command Prompt then write the
following commands;
a. cd desktop //for going to desktop
b. javac HelloWorld.java
c. java HelloWorld
The Java programming language compiler (javac) takes your source file and translates the
code into instructions known as bytecodes. “Java ClassName” will enable Java virtual
machine to run your application/code.
Local Variables
Local Variables are declared inside the body of a method.
Scope: Variables declared inside a method have method level scope and cannot be accessed
outside the method.
Class/Static Variables
It is declared with the keyword ‘static’, outside the method within a class. Static variables are
created when the program starts and destroyed when the program stops. There would only
be one copy of each static variable per class, regardless of how many objects are created.
Scope: Visibility is like instance variables. However, most static variables are declared public
since they must be available for users of the class.
class Variab {
int InsVarExam = 29; //instance variable
static int IsStatVar = 15; //static variable
void method() {
int IsLocalVar = 90; //local variable
}
}
In such cases, you have to explicitly specify the type cast operator. This process is known
as type casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is
enforced by the compiler, it makes the programmer aware that the conversion he is about to
do may cause some loss in data and prevents accidental losses.
class Demo {
public static void main(String args[]) {
byte x;
int a = 270;
double b = 128.128;
System.out.println("int converted to byte");
x = (byte) a;
System.out.println("a and x " + a + " " + x);
System.out.println("double converted to int");
a = (int) b;
System.out.println("b and a " + b + " " + a);
System.out.println("\ndouble converted to byte");
x = (byte)b;
System.out.println("b and x " + b + " " + x);
}
}
Output:
int converted to byte
a and x 270 14
double converted to int
b and a 128.128 128
System.out.printn(“Hello World”)
System.out.printn(Hello World)
System.out.println”Hello World”;
println(“Hello World);
To generate one final error message, remove one of the brackets from the end of your
program. Now what message do you receive?
Question: 2 (Input)
Write a program that takes user input and displays it.