Unit-2
Basics of Java
Contents
• Features of Java
• JVM, JDK, JRE and Byte Code
• Structure of Java Program
• Datatypes
• Operators
• Control Statements
• C v/s Java
Features of Java
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Features of Java
1. Simple:
• Java is very easy to learn.
• Its syntax is simple, clean and easy to understand.
2. Object-Oriented:
• Java is an object-oriented programming language.
• Everything in Java is an object.
• Object-oriented means we organize our program as a combination of different types of objects that
incorporate both data and behavior.
• Basic concepts of OOPs are: Class, Object, Inheritance, Polymorphism, Abstraction,
Encapsulation.
Features of Java
3. Portable:
• Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't
require any implementation.
4. Platform independent:
• Java code can be executed on multiple platforms, for example, Windows, Linux, Mac/OS, etc.
• Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-
independent because it can be run on multiple platforms, i.e., Write Once and Run Anywhere.
5. Secured:
• Java is best known for its security as it doesn’t use pointers.
• Java programs run in an environment that is independent of the OS environment which makes java
programs more secure.
Features of Java
6. Robust:
• Java language is robust which means reliable.
• The main features of java that make it robust are garbage collection, exception handling, and
memory allocation.
7. Architecture neutral:
• Java is architecture neutral because there are no implementation dependent features, for example,
the size of primitive types is fixed.
8. Interpreted:
• Java programs are compiled to generate bytecode files then JVM interprets the bytecode file during
execution.
• Along with this JVM also uses a JIT compiler (it increases the speed of execution).
Features of Java
9. High Performance:
• Java offers high performance using the JIT (Just In Time) compiler.
• The JIT enhances the performance of interpreting byte code by caching interpretations.
10. Multithreaded:
• A thread is like a separate program, executing concurrently.
• We can write Java programs that deal with 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.
Features of Java
11. Distributed:
• We can create distributed applications using the java programming language.
• The java programs can be easily distributed on one or more systems that are connected to each
other through an internet connection.
12. Dynamic:
• Java is a dynamic language.
• It supports the dynamic loading of classes.
• It means classes are loaded on demand.
• It also supports functions from its native languages, i.e., C and C++.
Working of JVM, JDK, JRE
JVM
• JVM (Java Virtual Machine) is an abstract machine.
• It is called a virtual machine because it doesn't physically exist.
• It is a specification that provides a runtime environment in which Java bytecode can be executed.
• It can also run those programs which are written in other languages and compiled to Java
bytecode.
• JVMs are available for many hardware and software platforms.
• JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from
each other.
• Main tasks of JVM: Loads code, Verifies code, Executes code and Provides runtime environment.
JDK
• Java Development Kit is a software development kit that provides the environment to develop and
execute(run) the Java program.
• JDK contains JRE + development tools.
1. Development Tools: used to provide an environment to develop your java programs.
(compilers, JavaDoc, Java Debugger, etc).
2. JRE: to execute your java program.
JRE
• JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java
Virtual Machine (JVM), and other components that are required to run Java applications.
• JRE is the superset of JVM.
• It is used to provide the runtime environment.
• It physically exists.
• It contains a set of libraries + other files that JVM uses at runtime
Byte Code
• Byte Code is an intermediate code generated by the compiler after the compilation JAVA Program.
• This intermediate code makes Java a platform-independent language.
• Compiler converts the source code or the Java program into the Byte Code(or machine code), and
secondly, the Interpreter executes the byte code on the system.
• The Interpreter can also be called JVM(Java Virtual Machine).
• The byte code is the common piece between the compiler(which creates it) and the Interpreter
(which runs it).
Why Java is Platform Independent?
Why Java is Platform Independent?
• Bytecode helps us to achieve platform independency.
• The set of instructions for the JVM may differ from system to system but all (Windows, Linux,
MAC etc.) can interpret the bytecode.
• Bytecode is essentially the machine level language which runs on the Java Virtual Machine.
• Whenever a class is loaded, it gets a stream of bytecode per method of the class.
• Whenever that method is called during the execution of a program, the bytecode for that method
gets invoked.
• Javac not only compiles the program but also generates the bytecode for the program.
• Thus, we have realized that the bytecode implementation makes Java a platform-independent
language.
• This helps to make Java a portable language.
Structure of Java Program
Structure of Java Program
1) Documentation Section:
• The documentation section is an important section but optional for a Java program.
• It includes basic information about a Java program i.e. the author's name, date of creation,
version, program name, company name, and description of the program.
• It improves the readability of the program.
• It will be written using comments, which are basically ignored by Java Compiler.
• The comments may be single-line, multi-line, and documentation comments.
• Single-line Comment: Starts with //.
• Multi-line Comment: Starts with a /* and ends with */.
• Documentation Comment: Starts with /** and ends with */.
Structure of Java Program
2) Package Declaration:
• The package declaration is optional.
• It is placed just after the documentation section.
• In this section, we declare the package name in which the class is placed by using ‘package’
keyword.
• Note that there can be only one package statement in a Java program.
• It must be defined before any class and interface declaration.
• Example:
package p1; //where p1 is the package name
package MyPack.p1; //where MyPack is the root directory and p1 is the package name
Structure of Java Program
3) Import Statements:
• The package contains the many predefined classes and interfaces.
• If we want to use any class of a particular package, we need to import that class by using ‘import’
keyword.
• The import statement represents the class stored in the other package.
• It is written before the class declaration and after the package statement.
• We can use the import statement in two ways, either import a specific class or import all classes of
a particular package.
• In a Java program, we can use multiple import statements.
• Example:
import java.util.Scanner; //it imports the Scanner class only
import java.util.*; //it imports all the class of the java.util package
Structure of Java Program
4) Interface Section:
• It is an optional section.
• We use the ‘interface’ keyword to create an interface.
• An interface is a slightly different from the class. It contains only constants and method
declarations.
• Another difference is that it cannot be instantiated.
• We can use interface in classes by using the ‘implements’ keyword.
• An interface can also be used with other interfaces by using the extends keyword.
• Example:
interface A {
//code
}
Structure of Java Program
5) Class Definition:
• It is vital part of a Java program. Without the class, we cannot create any Java program.
• The class is a blueprint of a Java program.
• Every Java program has at least one class that contains the main() method.
• A Java program may conation more than one class definition.
• We use the ‘class’ keyword to define the class.
• It contains information about user-defined methods, variables, and constants.
• Example:
class Student {
String sname;
void show() { … }
}
Structure of Java Program
6) Main Method Class:
• In this section, we define the main() method.
• It is essential for all Java programs as the execution of all Java programs starts from the main()
method.
• It is an entry point of the class.
• It must be inside the class.
• Inside the main method, we can create objects and call the methods.
• Example:
public static void main(String args[]) {
//code
}
Datatypes
Primitive Datatypes
• Primitive types are predefined in Java.
• They have fixed size and default value. A primitive type has always a value.
Non-Primitive Datatypes
• They don’t have fixed size and default values.
• The non-primitive data types include Classes, Interfaces, and Arrays.
• Non-primitive data types are called reference types because they refer to objects.
• Non-primitive types are created by the programmer and is not defined by Java (except for String).
• These can be used to call methods to perform certain operations.
• Non-primitive types can be null.
Control Statements
• In Java, there are 6 such structures that are used to determine the normal flow of control in a
program:
1. Block
2. While loop
3. Do...while loop
4. For loop
5. If statement
6. Switch statement
• Each of these can contain one or more other statements inside itself.
Control Statements
➢ Block:
• It is the simplest type of structured statement.
• Its purpose is simply to group a sequence of statements into a single statement.
• The format of a block is:
{
//statements;
}
• The block statement by itself really doesn’t affect the flow of control in a program. The five
remaining control structures do.
Control Statements
➢ Loops:
1) While Loop: A while loop will repeat a statement over and over, but only so long as a specified
condition remains true.
• It is also called Entry Control Loop.
• Once the condition is true, the statements in the loop body are executed. When the condition
becomes false, the loop terminates.
• Hence, it will be iterated n times, if condition becomes true for n times.
• Syntax:
while (boolean-expression) {
//statements;
}
Control Statements
➢ Loops:
2) Do…while Loop: The Java do while loop executes a part of the programs at least once and the
further execution depends upon the given boolean condition.
• It checks for condition after executing the statements.
• Therefore it is called Exit Control Loop.
• This loop gets executed at least once, hence it will be iterated n+1 times, if condition becomes true
for n times.
• Syntax:
do {
//statements;
} while (boolean-expression)
Control Statements
➢ Loops:
3) For Loop: The Java for loop is a control flow statement that iterates a part of the programs
multiple times.
• Syntax:
for (initialization; conditions; increment/decrement) {
//statements;
}
• Example: Display the numbers 1, 2, . . . , 10
for (int n = 1; n <= 10; n++) {
System.out.println(n);
}
Control Statements
➢ Branching Statements:
1) If Statement: It is an example of a “branching” or “decision” statement.
• The Java if statement is used to test the condition. It checks boolean condition: true or false.
• There are 4 types of if statement: if statement, if-else statement, if-else-if ladder, nested if
statement.
• Syntax:
if (boolean-expression) {
//statements;
}
else {
//statements;
}
Control Statements
➢ Branching Statements:
2) Switch Statement: A switch statement allows you to test the value of an expression and,
depending on that value, to jump directly to some location within the switch statement.
• Syntax:
switch (expression) {
case constant-1: statements-1
break;
case constant-2: statements-2
break;
case constant-N: statements-N
break;
default: // optional default case statements
}
C v/s Java
Sr. C Java
No.
1. C was developed by Dennis M. Ritchie . Java was developed by James Gosling.
2. C is a Procedural Programming Language. Java is Object-Oriented language.
3. C is a compiled language. Java is an interpreted language.
4. C is a low-level language. Java is a high-level language.
5. It does not support OOP concepts like Inheritance, It supports OOP concepts like Inheritance,
Encapsulation, Polymorphism and Abstraction. Encapsulation, Polymorphism and Abstraction.
6. C supports pointers. Java does not supports pointers.
7. It is not secure. It is secure.
8. Call by value and call by reference is supported in C. It only supports a call by value.
9. C is platform dependent. Java is a platform independent.
10. It follows a top-down approach. Java follows a bottom-up approach.
C v/s Java
Sr. C Java
No.
11. It has 32 keywords. It has 50 keywords.
12. C language supports go-to statements. Java does not supports go-to statements.
13. C language supports union and structures. Java does not supports union and structures.
14. Exception handling is not supported in C. Exception handling is supported in Java.
15. It does not maintain memory, internally. It maintains memory, internally.