Java - Notes
Introduction of Java
- Author : James Gosling
- Vendor : Sun Microsystem (Now merged in Oracle)
- Programming Type : Object Oriented and Open Source
- Initial Name : Oak
- Present Name : Java
- Project Name : Project Green
- Slogan : WORA (Write Once Run Anywhere)
- Symbol : Coffee Cup with Saucer
- Extension : .java, .class, .jar
- First Version : JDK 1.0
- Present Name : JDK 22
Java components
- JDK : Java Development Kit is a Software Pacakage. It contains Java
Compiler, Java Runtime Environment and Java Virtual Machine.
- Java Compiler : Java Compiler is a type of translator, which converts source code to
byte code.
- JRE : Java Runtime Environment create a layer between java application and operating
system. Java application is executed under supervision of JRE.
- JVM : Java Virtual Machine is an agent of java, which converts byte code to machine
code.
Execution Scenario of Java
- 1. Source Code (.java) Compiled in Byte Code (.class) through Java Compiler.
- 2. Byte Code (.class) Executed through JVM.
- 3. JVM Converts the byte in Machine Code and we Get Output.
Keywords
Keywords are reserved words with predefined meanings in the compiler. There are 50 keywords in Java, such as:
- Data types: int, float, char, double, long, short, byte, Boolean
- Control structures: if, else, switch, break, default, do, while, for, continue
- Others: void, return, new, class, interface, abstract
Class, Package, Variables
- Class : A Class is the blueprint of objects. It is a container of variables and
methods.
- Package : A Package is a collection of classes, interfaces, and sub-packages.
- Variables : Variables are value containers that create space in memory.
public static void main(String[] args) Explained
- public: A keyword which works as an access modifier.
- static: Static is a keyword which works as modifier. If you create a method with static
modifier then that method can be call wtihout object.
A modifier indicating no need for an object
to call the method.
- void: Void is a keyword which works as return type. Indicates no return value.
- main(): The pre-defined method name, starting point of the program.
- String[] args: This array is used to take command line input.
System.out.println(" "); Explained
- System : Class
- out : Object
- println() : Method
Operators
Operators are symbols which are used to perform operation on operands.
- Arithmetic Operators :
Addtion (+) | Substraction (-) | Multiplication (*) | Division (/) | Modulo[Remainer Collector] (%)
- Relational Operators : These operators specifies relation b/w two entities.
Greater Than (>) | Less Than (<) | Greater Than or Equal to (>=) | Less Than or Equal to (<=)
|==equality [a==b] | Not equal to (!=)
- Assignment Operator : a=b //Copies value of b to a
- Increment Operator : Increment Operator increase value of a variable by 1.
a++ //a=a+1;
- Decrement Operator : Decrement Operator increase value of a variable by 1.
a-- //a=a-1;
- Logical Operators : Logical Operators are used to combine two or more conditions.
AND (&&) | OR (||) | NOT (!)
Decision Controls
Decision Controls are used for decision making.If you want to execute code
based on some condition then you can use decision control. In java there are following types of decision
control :
- if Statement : if is a keyword which works as decision control. We attach a
condition with if statement. If given condtiion is true then code will be executed and if given
condition is false then code will not be executed.
Syntax :
if (Condition) {
//code
}
- if-else Statement : if else is variation of if statement. We attact a condition
with if statement. if given condition is true then if block code will executed and if given
condition is false then else block code will be executed.
Syntax :
if (Condition) {
// if block code
}
else {
// else block code
}
- ladder if-else Statement :
- Switch Statement :
Loop Control
- while Statement :
- for Statement :
- do while loop :
- for each loop :
Array
Array is collection of simillar data types that means an array can store mutiple values of similar data
types.
Delcaration of Array :
datatype [] arrayname = new datatype[size];
E.g. : int [] arr = new int[10];
The above array can store ten numbers of integer types.
Initialization of Array :
int [] arr = {10, 20, 30, 40, 50};
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Memory Representation of Array |
arr[0] |
arr[1] |
arr[2] |
arr[3] |
arr[4] |
10 |
20 |
30 |
40 |
50 |
How to take input from user for an array?
int [] arr = int[5];
int i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter five numbers");
for(i=0; i<5; i++) {
arr[i] = sc.nextInt();
}
String in Java
Technically String is a sequence of characters. In Java String is a class. The object of String class stores
sequence of characters.
String name = "Hardik Srivastava";
Methods of String Class
- toUpperCase() : Converts String into uppercase.
- toLowerCase() : Converts String into lowercase.
- length() : Find length of String.
- equals() : Compare two Strings for equality. This method return boolean value.
- equalsignoreCase() : This method also compare two Strings for equality, but this
method avoid case sensitivity.
- split() : This method is used to split String into sub-strings and return array of
string.
String sen = "He is a good boy.";
String[] words = sen.spilt(" ");
words[0] = "He";
words[1] = "is";
words[2] = "a";
words[3] = "good";
words[4] = "boy.";
- replace() : This method is used to replace on string with another string in given
string.
- CharAt() : This method used to find characters at specified location.
Methods
Method is a named block of code which perform specific task.
Why Method?
If you have a block of code which is required different locations of program, then you can create a method of
that code and call it from desired location.
How to Create Method in Java?
modifier returntype methodname(parameters) {
// code
}
E.g.
public int sum(int a, b) {
return(a+b);
}
Types of Methods in Java :
- Static Method : Static Methods are those methods which are created by using 'static'
modifier. These methods are called class methods. There is no need of object to call static method. eg.
Arrays.sort();
- Non-Static Method : Non-Static methods are created without using Static keyword.
Non-Static methods can call by using object.
Note : You can create userdefined method inside class and outside of main method.
OOPS
OOPS stand for object oriented Programming System. It is a mechanism of software Development. The Object
Oriented Programming System has four pillars.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction : Abstraction is a mechanism to show only essential functionalities and
hide all other functionalities of an object.
- Encapsulation : Encapsulation is a mechanism to warp properties and functionalities in
a single unit, that single unit is called object
- Inheritance : Inheritance is a feature of object oriented programming. In Inheritance
you can create a new product by using existing product.
- Polymorphism : Thw term Polymorphism means one thing many forms.
Any Programming language which follows these four concept is known as object oriented programming language.
- Class : Class is a blueprint of object. Class is collection of variables and methods.
Class is created by using class keyword followed by classname. and the body of class is enclosed within
braces.
class clasname {
// variables and methods
}