Java Programming Course Notes
Captured in Hadoop and Big Data Course under Data Scientist Program.
All credits to Edupristine.com and the presenter Kalyan Nelluri
Before starting with Java you need to setup the Java Environment.
Object Oriented Programming
Define Class
<attribute Declaration>
The package name instancesstaticvariable is declared indicating that the following class is the part of this package. A package is collection of classes and interfaces
Captured in Hadoop and Big Data Course under Data Scientist Program.
All credits to Edupristine.com and the presenter Kalyan Nelluri
Before starting with Java you need to setup the Java Environment.
- Download the Java Software from Oracle.com.
- To write the Java Codes you will need a tool we will user the Eclipse Editor.
Features of Java:
- Object Oriented
- Portable
- Multi Threaded
- Secure
- Platform Independent
- Programs are bunch of objects
- Object Oriented Principles
- Class
- Object
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
- If you write program on Windows you can still use it on
- Unix or MacOS
- Can be executed on any platform (Hardware or Software)
- Threads are programs which can run in parallel.
- For Example in a game.
- Render the Graphics
- Play the sound
- Capture the user inputs from Keyboard/Joystick
- All the threads share common memory
- We can develop highly efficient applications
- Java has no explicit pointers
- Byte Code Verifier (Output of compile is byte code)
- Byte Code Verifier ensure no illegal access
- Java Virtual Machine executes our java program.
- We can use SSL and Cryptographic Techniques.
- SSL is used for encrypting data
- Size of integer in Borland C 4 bytes
- But in unix it is 8 bytes.
- But in Java irrespective of platform Java can execute on all platforms.
Development Process
- MyProgram.java
- Javac compiler (Same for all platforms)
- MyProgram.class (Bytecode - Assembly Language)
- JavaVM (Different for each platform)
- ByteCode executed (Machine Language)
Phases of Java Program
Task
|
Tool to use
|
Output
|
Write a program
|
Text editor/eclipse
|
.java file
|
Compile the program
|
Java Compiler (javac)
|
.class file byte code
|
Run the program
|
Java Interpreter (java)
|
Program Output
|
Define Class
- Every Module in Java is a class file
<attribute Declaration>
<constructor Declaration>
<method Declaration>
}
- Class is a template or blue print from which we can create objects
- Class is not allocated any memory
- Objects when created are allocated memory
- Class contains attributes, constructors and methods
- Attributes are variables
Variables
- Instance Variables
- Belong to object instance
- Value of variable of an object instance is different from the values of other object of same class
- Defined outside the method inside the class
- Static Variable
- Variables that are associated with class
- Same value for all object instances in same class
package instancestaticvariable;
public class Student {
int rollno;
String name;
static String college="IIPM";
Student(int r, String n){
rollno=r;
name=n;
}
void display(){
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
The package name instancesstaticvariable is declared indicating that the following class is the part of this package. A package is collection of classes and interfaces
There is a class named Student defined which has three variables rollno, name and college.
A constructor method Student is defined which accepts two values as parameter and stores them in the variable rollno and name. Constructor is a method which is called when a new object is created automatically. The constructor method is having the same name as of class. In our example the parameters r and n are used to accept the values and store then in the variables rollno and name.
The college variable is declared as static because the value of college will be same for all the instances of the student objects. By declaring the variable as static it will create one variable which will be same for all the objects and separate memory will not be allocated for this variable for each object. Suppose we will create two objects of class student then there will be one static variable created for Class and two set of variables will be created for the two objects s1 and s2 as shown below.
![]() |
| Static Variables and Instance Variables |
So the use of static variable we can write memory efficient programs.
Static variables can be accessed by statement Student.college for example.
If we add a method in Student class
void College(String c){
Student.college=c;
}
Also if we update the main() function as below:
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
s1.setCollege("MyCollege");
s1.display();
s2.display();
}
If the value of static variable is updated then the same will be updated for all the objects for that class and the output of the above code will be:
111 Karan IIPM
222 Aryan IIPM
111 Karan MyCollege
222 Aryan MyCollege
Methods
- Declare methods
<modifier> <reurn type> <name> (<parameter>*)
{
<statement>*
}
Method or Functions are group of statements that will perform a particular task.
Methods are reusable across all the objects.
Method has a name
Method will access some parameters
Method will return some value
Method can be declared with modifiers
Method have one or more set of statements
In the example below:
public String getName(){
return name;
}
Method name is getName
Does not take any parameters or arguments
Returns the String value
Has the access modifier as public
Statement in the method returns the name
Getter Methods
Methods which help in getting the value of attributes of a class
public int getInt(){
return id;
}
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
Setter Methods (Mutator Methods)
Methods which help in setting the value of attributes of a class
public void setInt(int id){
thid.id=id;
}
public void setName(String name){
this.name=name;
}
public void setSalary(double salary){
this.salary=salary;
}
Static Methods
- Method is associated with class and not with object, though it can be accessed with object of class as well. e,g, main method of java.
- If static method is accessed with object, compiler does give warning that static methods should be accessed in static way
- Static method can access static data members and can change the value of it.
- Q. Why java main method is declared as static?
A. If it were not declared as static, JVM has to create an object of that class to main method and this would result in extra memory allocation. - Q. Can we execute a class without main?
A. Was possible in earlier JDK versions using static block. No longer supported from JDK 7.
- The static methods can not use non static data member or call non-static method directly.
- "this" and "super" cannot be used in static context.
int a=40;
public static void main(String args[]){
System.out.println(a);
{
}
- main method is static
- variable a is non-static
- you cannot access a non-static variable in a static method.
- The above code will cause a compilation error.
- Inside the static method you can only access static data or static functions.
- To print the value of a we can create an object and print it.
class A{
int a=40;
public static void main(String args[]){
A obj = new A();
System.out.println(obj.a);
{
}
- Or we can make the variable a as static and the above program will work.
class A{
static int a=40;
public static void main(String args[]){
System.out.println(a);
{
}
- Advantage of static method is that we can call that method without creating an object.
- Static blocks are executed before the main method.
