1. What is Java?
Java is one of widely used and popular programming language. The flexibility of writing a java
code on one machine and running it on several other machine makes it a popular choice in
programming world.
Java is the #1 programming language and development platform with millions of developers
running more than 51 billion Java Virtual Machines worldwide.
Features of Java
Following are some of the useful and advanced features of java:
1. Simple
Java is a very simple programming language, it is easy to learn, read and write in Java. The
syntax of Java is clean and easy to understand.
2. Platform Independent
Java is a platform independent language. Compiler (javac) converts source code (.java file) to
the byte code(.class file). JVM executes the bytecode produced by compiler. This byte code
can run on any platform such as Windows, Linux, Mac OS etc. Which means a program that is
compiled on windows can run on Linux and vice-versa.
Each operating system has different JVM, however the output they produce after execution
of bytecode is same across all operating systems. That is why we call java as platform
independent language.
3. Secure
Java is more secure than C/C++ as does not allow developers to create pointers, thus it
becomes impossible to access a variable from outside if it’s not been initialized.
4. Object-Oriented Programming language
Java is Object-Oriented Programming language.
5. Robust
Robust means reliable. Java programming language is developed in a way that puts a lot of
emphasis on early checking for possible errors.
6. Distributed
Using java programming language we can create distributed applications.
7. Multithreading
Java supports multithreading. Multithreading is a Java feature that allows concurrent
execution of two or more parts of a program for maximum utilisation of CPU.
2. 8. Portable
Java code that is written on one machine can run on another machine
9. Dynamic
Java is a dynamic programming language.
10. Performance
Java is significantly faster than other traditional interpreted programming languages.
Compiled java code which is known as byte code is like a machine code, that allows a faster
execution.
JDK (Java Development Kit)
The JDK comes with a complete Java Runtime Environment (JRE), that is different from the
regular JRE thats the reason it is usually called a private runtime so we can say that it includes
all the features that JRE has. It has all the java development tools such as compiler, JVM,
JRE, debugger etc. We need jdk in order to write and run java program.
JRE (Java Runtime Environment):JRE is the environment within which the java virtual machine
runs. JRE contains Java virtual Machine(JVM), class libraries, and other files excluding
development tools such as compiler and debugger.
JVM (Java Virtual Machine):
Java is a high level programming language. A program written in high level language cannot
be run on any machine directly. First, it needs to be translated into that particular machine
3. language. The javac compiler does this thing, it takes java program (.java file containing source
code) and translates it into machine code (referred as byte code or .class file).
Java Virtual Machine (JVM) is a virtual machine that resides in the real machine (your
computer) and the machine language for JVM is byte code. JVM executes the byte code
generated by compiler and produce output. JVM is the one that makes java platform
independent.
Compiler/Debugger: The javac compiler takes java program (.java file containing source code)
and translates it into machine code (referred as byte code or .class file).
Explain the following line:
public static void main (String [] args) {}
A java file can have any number of classes but it can have only one public class and the file
name should be same as public class name.
public: This makes the main method public that means that we can call the method from
outside the class.
static: We do not need to create object for static methods to run. They can run itself.
void: It does not return anything.
main: It is the method name. This is the entry point method from which the JVM can run your
program.
(String[] args): Used for command line arguments that are passed as strings. We will
cover that in a separate post.
OOP:
Object-oriented programming System(OOPs) is a programming paradigm based on the
concept of “objects” that contain data and methods. The primary purpose of object-oriented
programming is to increase the flexibility and maintainability of programs.
What is an Object
Object: is a bundle of data and its behaviour(often known as methods).
Objects have two characteristics: They have states and behaviors.
Examples of states and behaviors
4. Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.
What is a Class in OOPs Concepts
A class can be considered as a blueprint using which you can create as many objects as you
like. For example, here we have a class Website that has two data members (also known as
fields, instance variables and object states). This is just a blueprint, it does not represent any
website, however using this we can create Website objects (or instances) that represents the
websites.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code. Single line comments
starts with two forward slashes (//). Any text in front of // is not executed by Java.
//This is single line comment
2) Java Multi Line Comment
The multi-line comment is used to comment multiple lines of code. It can be used to explain a
complex code snippet . Multi-line comments are placed between /* and */.
/*
This
is
multi line
comment
*/
3) Java Documentation Comment
Documentation comments are usually used to write large programs for a project or software
application as it helps to create documentation API.
5. /**
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags
*
*/
6. Chap 3
Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and
double.
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
byte:
This can hold whole number between -128 and 127. Mostly used to save memory .Default
size of this data type: 1 byte.
Example:
byte num;
num = 113;
short:
This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
short num = 45678;
int: Used when short is not large enough to hold the number, it has a wider range: -
2,147,483,648 to 2,147,483,647
Default size: 4 byte
int num;
num = 150;
long:
Used when int is not large enough to hold the value, it has wider range than int data type,
ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Example:
long num = -12332252626L;
7. double: Sufficient for holding 15 decimal digits
size: 8 bytes
double num = -42937737.9d;
float: Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
float num = 19.98f;
boolean: holds either true of false.
boolean b = false;
char: holds characters.
size: 2 bytes
char ch = 'Z';
Sum of two numbers
public class JavaExample {
public static void main(String[] args) {
int num1 = 5, num2 = 15,sum;
sum = num1+num2;
System.out.println("Sum of "+num1+" and "+num2+" is: "+sum);
}
}
Output:
Sum of 5 and 15 is: 20
Program to read two integer and print product/Multiply
of them
import java.util.Scanner;
public class multiplynum {
public static void main(String[] args) {
8. Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt();
scan.close();
int product = num1*num2;
System.out.println("Output: "+product);
}
}
Output:
Enter first number: 15
Enter second number: 6
Output: 90
Program to read the number entered by user
import java.util.Scanner;
public class scaninput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
int num = scan.nextInt();
scan.close();
System.out.println("The number entered by user: "+num);
}
}
Output:
Enter any number: 101
The number entered by user: 101
9. Java program to calculate area of Triangle
import java.util.Scanner;
class AreaTriangleDemo {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
//comment : Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}
Output:
Enter the width of the Triangle: 2
Enter the height of the Triangle: 2
Area of Triangle is: 2.0
10. Variables in Java
A variable is a name which is associated with a value that can be changed. For example when
I write int i=10; here variable name is i which is associated with value 10, int is a data type
that represents that this variable can hold integer values.
To declare a variable we follow this syntax:
data_type variable_name = value;
There are three types of variables in Java.
1) Local variable (These variables are declared inside method of the class. Their scope is
limited to the method which means that You can’t change their values and access them
outside of the method.) 2) Static (or class) variable (If I create three objects of a class and
access this static variable, it would be common for all, the changes made to the variable using
one of the object would reflect when you access it through other objects.) 3) Instance
variable (Each instance(objects) of class has its own copy of instance variable.)
Variables naming convention in java
1) Variables naming cannot contain white spaces,
2) Variable name can begin with special characters such as $ and _
3) The variable name should begin with a lower case letter,
4) Variable names are case sensitive in Java.
Dynamic initialization of object refers to initializing the objects at run
time i.e. the initial value of an object is to be provided during run time.
Dynamic initialization can be achieved using constructors and passing
parameters values to the constructors.
Example Code of Dynamic initialization
#include <iostream>
using namespace std;
class simple_interest {
float principle , time, rate ,interest;
public:
simple_interest (float a, float b, float c) {
principle = a;
time =b;
rate = c;
}
void display ( ) {
interest =(principle* rate* time)/100;
cout<<"interest ="<<interest ;
}
};
int main() {
11. float p,r,t;
cout<<"principle amount, time and rate"<<endl;
cout<<"2000 7.5 2"<<endl;
simple_interest s1(2000,7.5,2);//dynamic initialization
s1.display();
return 1;
}
Output
Enter principle amount ,rate and time
2000 7.5 2
Interest =300