Lecture 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Campus programming

Session – 4-5
Branch-Batch-1
Semester-I
Faculty Name-prof. Hemant vyas

Technocrats Group of Institutions


Technocrats Group Campus, BHEL, Bhopal - 462021 MP, India
Phone : +91-755-2751679
e-mail: info@titbhopal.net | website: www.technocratsgroup.edu.in
Difference between oop and pop
Sr. Key OOP POP
No.

Definition OOP stands for Object Oriented POP stands for Procedural Oriented
1 Programing. Programming.

2 Approach OOP follows bottom up approach. POP follows top down approach.

Division A program is divided to objects and their A program is divided into funtions and
3 interactions. they interacts.

Inheritance Inheritance is supported. Inheritance is not supported.


4 supported

Access Access control is supported via access No access modifiers are supported.
5 control modifiers.

Data Hiding Encapsulation is used to hide data. No data hiding present. Data is globally
6 accessible.

7 Example C++, Java C, Pascal

www.technocratsgroup.edu.in java programming 2


Brief History of Java and Overview of
Language

www.technocratsgroup.edu.in java programming 3


History

A brief history of Java


"Java, whose original name was Oak, was developed as a part of the Green project at
Sun. It was started in December '90 by Patrick Naughton, Mike Sheridan and James
Gosling and was chartered to spend time trying to figure out what would be the "next
wave" of computing and how we might catch it. They came to the conclusion that at
least one of the waves was going to be the convergence of digitally controlled consumer
devices and computers. "
Applets and Applications
"The team returned to work up a Java technology-based clone of Mosaic they named
"WebRunner" (after the movie Blade Runner), later to become officially known as the
HotJavaTM browser. It was 1994. WebRunner was just a demo, but an impressive one: It
brought to life, for the first time, animated, moving objects and dynamic executable
content inside a Web browser. That had never been done. [At the TED conference.]"
www.technocratsgroup.edu.in java programming 4
Architecture of java

www.technocratsgroup.edu.in java programming 5


How Java Works
Java's platform independence is achieved by the use of the
Java Virtual Machine
A Java program consists of one or more files with a .java
extension
these are plain old text files
When a Java program is compiled the .java files are fed to a
compiler which produces a .class file for each .java file
The .class file contains Java bytecode.

www.technocratsgroup.edu.in java programming 6


How Java Works
Bytecode is like machine language, but it is intended for the
Java Virtual Machine not a specific chip such as a Pentium or
PowerPC chip.

www.technocratsgroup.edu.in java programming 7


Hello world in java
/**
* A simple program
*/

public class HelloWorld


{
public static void main(String[] args)
{
System.out.println("HELLO CS307!");
}
}
www.technocratsgroup.edu.in java programming 8
Variants of main method in java
Below are different variants of main() that are valid.

Default prototype: Below is the most common way to write main() in Java.

class Test
{
public static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method
Meaning of the main Syntax:

www.technocratsgroup.edu.in java programming 9


Variants of main method in java
public: JVM can execute the method from anywhere.
static: Main method can be called without object.
void: The main method doesn't return anything.
main(): Name configured in the JVM.
String[]: Accepts the command line arguments.
Order of Modifiers: We can swap positions of static and public in main().

www.technocratsgroup.edu.in java programming 10


Variants of main method in java
class Test
{
static public void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method
Variants of String array arguments: We can place square brackets at different positions and we can use varargs
(…) for string parameter.

www.technocratsgroup.edu.in java programming 11


Variants of main method in java
class Test
{
public static void main(String []args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 12


Variants of main method in java
class Test
{
public static void main(String args[])
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 13


Variants of main method in java
class Test
{
public static void main(String...args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 14


Variants of main method in java
Final Modifier String argument: We can make String args[] as final.

{
public static void main(final String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 15


Variants of main method in java
Final Modifier to static main method: We can make main() as final.

class Test
{
public final static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 16


Variants of main method in java
synchronized keyword to static main method: We can make main() synchronized.

class Test
{
public synchronized static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 17


Variants of main method in java
strictfp keyword to static main method: strictfp can be used to restrict floating point calculations.

class Test
{
public strictfp static void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 18


Variants of main method in java
Combinations of all above keyword to static main method:

class Test
{
final static public synchronized strictfp void main(String[] args)
{
System.out.println("Main Method");
}
}
Output:

Main Method

www.technocratsgroup.edu.in java programming 19


Variants of main method in java
Overloading Main method: We can overload main() with different types of parameters.

class Test
{
public static void main(String[] args)
{
System.out.println("Main Method String Array");
}
public static void main(int[] args)
{
System.out.println("Main Method int Array");
}
}
Output:

Main Method String Array

www.technocratsgroup.edu.in java programming 20


Variants of main method in java
Inheritance of Main method: JVM Executes the main() without any errors.

class A
{
public static void main(String[] args)
{
System.out.println("Main Method Parent");
}
}

class B extends A
{

}
Two class files, A.class and B.class are generated by compiler. When we execute any of the two .class, JVM
executes with no error.

www.technocratsgroup.edu.in java programming 21


Variants of main method in java
O/P: Java A
Main Method Parent
O/P: Java B
Main Method Parent
Method Hiding of main(), but not Overriding: Since main() is static, derived class main() hides the base class
main. (See Shadowing of static functions for details.)

www.technocratsgroup.edu.in java programming 22


Variants of main method in java
class A
{
public static void main(String[] args)
{
System.out.println("Main Method Parent");
}
}
class B extends A
{
public static void main(String[] args)
{
System.out.println("Main Method Child");
}

www.technocratsgroup.edu.in java programming 23


Variants of main method in java
}
Two classes, A.class and B.class are generated by Java Compiler javac. When we execute both the .class, JVM
executes with no error.

O/P: Java A
Main Method Parent
O/P: Java B
Main Method Child

www.technocratsgroup.edu.in java programming 24


End of Session

You might also like