Java M1-P1
Java M1-P1
Module-1
1
Books & References
3
OBJECTIVES
▪ Understand the OOP concepts and difference in C++ and Java languages
▪ Differentiate among
- JDK,
- J𝑅𝐸 𝑎𝑛𝑑
- 𝐽𝑉𝑀
▪ Understand the steps for creating, compiling and executing a Java program
4
Introduction
• Java is a high-level programming language originally developed by Sun Microsystems and released in
1995.
• Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
• Since Oak was already a registered company, so James Gosling and his team changed the Oak name to
Java. Java is a coffee type of an island of Indonesia.
5
Platform?
• Any hardware or software environment in which a program runs, is known as a
platform.
• Since Java has a runtime environment (JRE) and API, it is called a platform.
6
Step by step Execution of Java Program:
.
•Whenever, a program is written in JAVA, the javac compiles it.
•The result of the JAVA compiler is the .class file or the bytecode and not the
machine native code (unlike C compiler).
7
JAVA: Applications
According to Sun, 3 billion devices run Java.
• Mobile
• Embedded System
• Smart Card
• Robotics
• 8 Games, etc.
JAVA FEATURES
➢ Java is a fully Object Oriented Language.
Pure Object Oriented Language or Complete Object Oriented Language are Fully Object Oriented Language
which supports or have features which treats everything inside program as objects. It doesn’t support
primitive datatype(like int, char, float, bool, etc.). There are seven qualities to be satisfied for a programming
language to be pure Object Oriented. They are:
1.Encapsulation/Data Hiding
2.Inheritance
3.Polymorphism
4.Abstraction
5.All predefined types are objects
6.All user defined types are objects
7.All operations performed on objects must be only through methods exposed at the objects.
Java use primitive data type and static class.
Wrapper class provides the mechanism to convert primitive into object and object into primitive. In Java,
you can use Integer, Float etc. instead of int, float etc.
used as singleton classes , you cannot create object but used a factory classes which return their object
refernce.
JAVA FEATURES
➢ Case Sensitive
➢ Platform independent
• Java compiler converts the source code to bytecode, which is Intermediate Language.
• Bytecode can be executed on any platform (OS) using JVM( Java Virtual Machine).
• Byte codes are converted into machine code using interpreter.
➢ Distributed
• Programs can be designed to run on computer networks.
• Write once, run anywhere
➢ Secure
• JVM is an interpreter which is installed in each client machine that is updated with latest security
updates by internet .
• When this byte codes are executed , the JVM can take care of the security.
• The JVM(Java Virtual Machine) in java uses sandbox model which is the main security feature in java
that makes java secure.
• With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication
techniques are based on public-key encryption
JAVA FEATURES
➢Multithreaded
• A thread is like a separate program, executing concurrently.
• We can write Java programs that deal with many tasks at once by defining multiple
threads.
• it doesn't occupy memory for each thread. It shares a common memory area.
• Threads are important for multi-media, Web applications, etc.
➢Robust
• Robust simply means strong. Java uses strong memory management.
• There is a lack of pointers that avoids security problems.
• There is automatic garbage collection in java.
• There are exception handling and the type checking mechanism in Java.
JAVA FEATURES
➢Architecture-neutral
• Java is architecture neutral because there are no implementation dependent
features, for example, the size of primitive types is fixed.
• It occupies 4 bytes of memory for int data type in both 32 and 64-bit architectures
in Java.
➢Dynamic
• Java is a dynamic language. It supports dynamic loading of classes.
• Java supports dynamic compilation and automatic memory management.
C++/JAVA
Comparison Index C++ Java
Mainly used for C++ is mainly used for system Java is mainly used for application
programming. programming.
Goto C++ supports the goto statement. Java doesn't support the goto statement.
Multiple C++ supports multiple inheritance. Java doesn't support multiple inheritance
inheritance through class. It can be achieved by interfaces
in Java.
Operator C++ supports operator overloading. Java doesn't support operator overloading.
Overloading
Pointers C++ supports pointers. We can write Java supports pointer internally. However, you
pointer program in C++. can't write the pointer program in java. It
means java has restricted pointer support in
java.
C++/JAVA
Compiler and C++ uses compiler only. Java uses compiler and interpreter
Interpreter both.
Call by Value and C++ supports both call by value and call by Java supports call by value only.
Call by reference reference. There is no call by reference in java.
Structure and Union C++ supports structures and unions. Java doesn't support structures and
unions.
Thread Support C++ doesn't have built-in support for Java has built-in thread support.
threads. It relies on third-party libraries for
thread support.
Virtual Keyword C++ supports virtual keyword. Java has no virtual keyword.
• Java SE's API provides the core functionality of the Java programming language.
• It defines everything from the basic types and objects of the Java programming
language to high-level classes.
• provides an API and runtime environment for developing and running large-scale,
multi-tiered, scalable, reliable, and secure network applications.
15
JAVA Programming Language Platform
❑ JavaFX
• JavaFX is a platform for creating rich internet applications using a lightweight user-
interface API.
16
Types of Java Applications
There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application- Standalone applications are also known as desktop applications or window-
based applications. These are traditional software that we need to install on every machine. Examples of
standalone application are Media player, antivirus, etc. AWT and Swing are used in Java for creating
standalone applications.
2) Web Application- An application that runs on the server side and creates a dynamic page is called a
web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for
creating web applications in Java.
3) Enterprise Application- An application that is distributed in nature, such as banking applications, etc.
is called enterprise application. It has advantages of the high-level security, load balancing, and clustering.
In Java, EJB is used for creating enterprise applications.
4) Mobile Application- An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
17
Program 1
// prints Hello World
18
• Java main() method is always static, so that compiler can call it without
the creation of an object or before the creation of an object of the class.
• In any Java program, the main() method is the starting point from
where compiler starts program execution. So, the compiler needs to call
the main() method.
• If the main() is allowed to be non-static, then while calling
the main() method JVM has to instantiate its class.
• While instantiating it has to call the constructor of that class, There will
be ambiguity if the constructor of that class takes an argument.
• Static method of a class can be called by using the class name only
without creating an object of a class.
• The main() method in Java must be declared public, static and void. If
any of these are missing, the Java program will compile but a runtime
error will be thrown.
• Sttaic function can not be override also.
Steps to create a Java Program
For executing any java program:
java Simple
To execute:
21
Java Virtual Machine(JVM)
• 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. However, Java is platform independent.
22
Java Runtime Environment(JRE)
23
Java Development Kit(JDK)
24
Q) Can you save a java source file by other name than
the class name?
Yes, if the class is not public.
We can save your java source code file with any other name, not same as your main class
name but when you comiple it than byte code file name will be same as your main class name
26
class A
{
public static void main(String args[]) Save file as example.java
{
System.out.println("You are in Class A"); Compile: javac example.java
}
} Run: java A
class B O/P: You are in Class A
{ Java B
public static void main(String args[]) O/P: You are in Class B
{
System.out.println("You are in class B");
}
}
27
Java Variables
• A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
• There are three types of variables in java: local, instance and static.
28
Local Variable
Local Variable
• A variable defined within a block or method or constructor is called local variable.
•These variable are created when the block in entered or the function is called and
destroyed after exiting from the block or when the call returns from the function.
•The scope of these variables exists only within the block in which the variable is
declared. i.e. we can access these variable only within that block.
•It can’t be static, because in Java, static variable is a class variable but local variable is
only for specified function, so it will violate the purpose of static variable in Java.
29
public class Student
{
public void Age()
{
// local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}
• It is called instance variable because its value is instance specific and is not shared
among instances.
• As instance variables are declared in a class, these variables are created when an
object of the class is created and destroyed when the object is destroyed.
• Unlike local variables, we may use access specifiers for instance variables. If we do not
specify any access specifier then the default access specifier will be used.
31
• Initialization of Instance variable is not Mandatory. Its default value is 0.
32
3) Static variable
• A variable which is declared as static is called static variable.
• It cannot be local to any function or constructor. We can create a single copy of static
variable and share among all the instances of the class.
• Memory allocation for static variable happens only once when the class is loaded in
the memory.
35
Data Types in Java
• Data types specify the different sizes and values that can be stored in the variable.
Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
36
Primitive Data Types
Data Default Default size
Type Value In Java char data type is 2 bytes Unicode character.
boolean false 1 bit Unicode is a universal international standard
character encoding that is capable of representing
char '\u0000' 2 byte most of the world's written languages. Its value-
byte 0 1 byte range lies between '\u0000' (or 0) to '\uffff' (or
65,535).
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
37
Operators in Java
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
38
Some Assignments ☺
1. Write a program which prints the following information about at least 5 persons:
NAME MAIL-ID EMPLOYEE-CODE PHONE
Eg. Umesh umesh@cse p03161 25764728
Salil salil@cse p03160 25764728
Each entry should be on a separate line.
2. Write a program that prints the following line on the screen along with quotes.
“Can we print ‘\’ with System.out.println() statement?”
System.out.println("\"Can we print"+ "'\\'" + "with System.out.println() statement?\"");
End
Thank you…
[email protected]
REFRENCES
1. Java : The Complete Reference , Patrick Naughton, Herbert Schildt
42
• Cst spl1 (6feb)
10-12AM
3,6,7,12,13,16,19,21,24,25,26,27,28,29,32,33,37,38,42,43,44,45,47,50,52,55,58,59,61,62,65,66
7th Feb(8-9AM)
1,2,3,4,7,8,11,12,13,16,18,19,21,22,24,25,26,27,28,29,30,32,33,34,36,37,38,39,42,43,44,45,46,47,48,
50,51,52,53,54,55,56,58,59,60,62,64,65,66
AIDS(7feb): 10,11,15,16,27,31,32,36,42,43,44,45,46,51
AIML: 3,5,6,7,10,11,21,22,24,25,26,28,37,38,
8th feb
AIDS: 1,7,8,11,15,16,22,27,31,32,36,39,42,43,44,45,46,48,49,51
AIML: 2,3,5,7,8,10,11,20,21,22,24,25,26,29,34,35,37,38,42