1. PROGRAMMING IN JAVA
A. SIVASANKARI
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE
SHANMUGA INDUSTRIES ARTS AND SCIENCE COLLEGE,
TIRUVANNAMALAI. 606601.
Email: sivasankaridkm@gmail.com
2. PROGRAMMING IN JAVA
UNIT - 1
HISTORY OF JAVA
FEATURES OF JAVA
CLASSES AND OBJECTS
BASIC DATATYPES
VARIABLE AND ITS TYPES
STRING CLASS
BASIC OPERATORS
CONTROL STATEMENTS
CONSTRUCTORS
JAVA PACKAGE
METHOD OVERLOADING
A. SIVASANKARI - SIASC-TVM
3. PROGRAMMING IN JAVA
UNIT -1 A
HISTORY OF JAVA
FEATURES OF JAVA
CLASSES AND
OBJECTS
BASIC DATATYPES
VARIABLE AND ITS
TYPES
STRING CLASS
A. SIVASANKARI - SIASC-TVM
4. INTRODUCTION TO JAVA
• 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.
• Java is developed by James Gosling at Sun Micro system in the year 1991.
• Initially it was named “OAK”. Oak was renamed as "Java"
• It is a platform independent language
• Java is case sensitive.
• Example: System.in
• Now Java is being used in Windows applications, Web applications,
enterprise applications, mobile applications, cards, and also used to create
a software on electronic device.
• Example: Microwave Oven, Remote Control.
• Java programs run in variety of CPU and different environments. Java was
originally designed for interactive television, but it was too advanced
technology for the digital cable television industry at the time.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
5. JAVA VERSIONS
1. JDK Alpha and Beta (1995)
2. JDK 1.0 (23rd Jan 1996)
3. JDK 1.1 (19th Feb 1997)
4. J2SE 1.2 (8th Dec 1998)
5. J2SE 1.3 (8th May 2000)
6. J2SE 1.4 (6th Feb 2002)
7. J2SE 5.0 (30th Sep 2004)
8. Java SE 6 (11th Dec 2006)
9. Java SE 7 (28th July 2011)
10. Java SE 8 (18th Mar 2014)
11. Java SE 9 (21st Sep 2017)
12. Java SE 10 (20th Mar 2018)
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
6. IMPORTANCE OF JAVA
• Java is forefront of program.
• Java expends the universe of object. We can access and create Object
University.
• Client and server process also access in java.
• It is divided into 3 performance
• Passive
• Dynamic
• Active
• Java divided into applet and applications
APPLICATIONS:
• It is a program that runs on our computer under the operating system of
that computer.
• Example: C, C++, Java
APPLETS:
• It is an application designed to be transmitted over the internet and
executed by a java compatible with browser.
• It is an intelligence program. Applications having Animations is called
applets.
• Example: sound, Image, Video clips.
SECURITY:
• To avoid the viral infection we use security.
• Example: Firewall method, K7, Trojan Horse, etc.
PORTABILITY:
• Many type of computers and operating system are in use throughout the
world and many are connected to the internet.
• It is elegant and efficient.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
7. THE BYTE CODE
• Java compiler is not executable code. It is executed by a byte code.
• Byte code is highly optimized set of instructions designed to be executable by the Java Run Time
system is called Java Virtual Machine(JVM).
• JVM is an interpreter for byte code.
• Translating a java program into byte code helps makes it much easier to run s program in a wide
variety of environments.
• It is much faster.
• Java2 release Java In time Compiler(JIT) for byte code.
• JIT is part of JVM. It combines byte code into executable code in real time on a piece by piece.
Difference between JDK, JRE, and JVM
1.A specification where working of Java Virtual Machine is specified. But implementation provider is
independent to choose the algorithm. Its implementation has been provided by Oracle and other
companies.
2.An implementation Its implementation is known as JRE (Java Runtime Environment).
3.Runtime Instance Whenever you write java command on the command prompt to run the java class, an
instance of JVM is created.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
8. FEATURES OF JAVA
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
9. SIMPLE
• Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun,
Java language is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator overloading, etc.
OBJECT-ORIENTED
• Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
SECURED
• Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Classloader: Class loader in Java is a part of the Java Runtime Environment(JRE) which is used to
load Java classes into the Java Virtual Machine dynamically. It adds security by separating the
package for the classes of the local file system from those that are imported from network sources.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
10. ROBUST
• Robust simply means strong. Java is robust because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• There is automatic garbage collection in java which runs on the Java Virtual Machine to get rid of objects
which are not being used by a Java application anymore.
• There are exception handling and the type checking mechanism in Java. All these points make Java
robust.
HIGH-PERFORMANCE
• Java is faster than other traditional interpreted programming languages because Java bytecode is "close"
to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted
language that is why it is slower than compiled languages, e.g., C, C++, etc.
DISTRIBUTED
• Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are
used for creating distributed applications. This feature of Java makes us able to access files by calling the
methods from any machine on the internet
MULTI-THREADED
• 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. The main advantage of multi-threading is that it doesn't
occupy memory for each thread. It shares a common memory area. Threads are important for multi-media,
Web applications, etc.
DYNAMIC
• Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on
demand. It also supports functions from its native languages, i.e., C and C++.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
11. CLASSES AND OBJECTS
Object − Objects have states and behaviours. Example: A dog has states - color, name, breed as
well as behaviours – wagging the tail, barking, eating. An object is an instance of a class.
Class − A class can be defined as a template/blueprint that describes the behaviour/state that the
object of its type support.
• SYNTAX OF CLASS:
• Class classname
• {
• Type instance variable 1;
• .
• .
• Type instance variable N;
• Type methodname1(parameter list)
• {
• Body of the method
• }
• Type methodnameN(parameter list)
• {
• Body of the method
• }
• }
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
12. SAMPLE PROGRAM
• Class Simple
• {
• public static void main(String args[])
• {
• System.out.println(“Welcome to JAVA”);
• }
• }
COMPILATION
• C:> bin> javac Simple.java
RUN
• C:> bin> java Simple
OUTPUT
• Welcome to JAVA
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
13. CREATING AN OBJECT
There are three steps when creating an object from a class −
• Declaration − A variable declaration with a variable name with an object type.
• Instantiation − The 'new' keyword is used to create the object.
• Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes
the new object.
DECLARING OBJECTS: TWO TYPES
Syntax of Object
1. Classname objectname=new classname()
2. Classname objectname;
Objectname=new classname();
Example of Object Declaration
1. Box mybox=new Box());
2. Box mybox;
mybox=new box();
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
14. • SAMPLE PROGRAM:
• Class box
• {
• Double width;
• Double height;
• Double depth;
• }
• Class boxdemo
• {
• public static void main(String args[])
• box mybox=new box();
• Double volume;
• Mybox.width=10;
• Mybox.height=20;
• Mybox.depth=15;
• Volume=mybox.width*mybox.height*mybox.depth;
• System.out.println(“The value is :”+ volume);
• }}
OUTPUT: Volume=3000
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
15. BASIC DATATYPES
• Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in the memory.
• Based on the data type of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, we can store integers, decimals, or characters in
these variables.
There are two data types available in Java
• Primitive Data Types
• Reference/Object Data Types
PRIMITIVE DATA TYPES
Data type long int short byte double float char Boolean
Width
64 32 16 8 64 32 255 True/False
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
16. • BYTE
• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers, since a byte
is four times smaller than an integer.
• Example: byte a = 100, byte b = -50
• SHORT
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2 times smaller
than an integer
• Default value is 0..
• LONG
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed
• Default value is 0L
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
17. • FLOAT
• Float data type is a single-precision 32-bit IEEE 754
floating point
• Float is mainly used to save memory in large arrays
of floating point numbers
• Default value is 0.0f
• BOOLEAN
• boolean data type represents one bit of information
• There are only two possible values: true and false
• CHAR
• char data type is a single 16-bit Unicode character
• Minimum value is 'u0000' (or 0)
• Maximum value is 'uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letterA = 'A'
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
18. LEXICAL ISSUES:
• White space
• Identifier
• Literals
• Commends
• Separators
• Java Keywords
JAVA LITERALS
• A literal is a source code representation of a fixed value. They are represented
directly in the code without any computation.
• Literals can be assigned to any primitive type variable. For example −
• byte a = 68;char a = 'A';
• byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base
16) or octal(base 8) number systems as well.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
20. VARIABLE AND ITS TYPES
A variable provides us with named storage that our
programs can manipulate. Each variable in Java has a specific
type, which determines the size and layout of the variable's
memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the
variable.
Example
• int a, b, c; // Declares three int s, a, b, and c.
• int a = 10, b = 10; // Example of initialization
• byte B = 22; // initializes a byte type variable
• B.double pi = 3.14159; // declares and assigns a value of PI.
• char a = 'a'; // the char variable a is initialized with value
'a‘
TYPES OF VARIABLE
• Local variables
• Instance variables
• Class/Static variables
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
21. LOCAL VARIABLES
• Local variables are declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or block is entered and the
variable will be destroyed once it exits the method, constructor, or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared method, constructor, or block.
INSTANCE VARIABLES
• Instance variables are declared in a class, but outside a method, constructor or any
block.
• When a space is allocated for an object in the heap, a slot for each instance variable
value is created.
• Instance variables are created when an object is created with the use of the keyword
'new' and destroyed when the object is destroyed.
• Instance variables hold values that must be referenced by more than one method,
constructor or block, or essential parts of an object's state that must be present
throughout the class.
• Instance variables can be accessed directly by calling the variable name inside the
class. However, within static methods (when instance variables are given
accessibility), they should be called using the fully qualified
name. ObjectReference.VariableName.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
22. STATIC VARIABLES OR CONSTANT VARIABLE
• Class variables also known as static variables are declared with the static
keyword in a class, but outside a method, constructor or a block.
• There would only be one copy of each class variable per class, regardless
of how many objects are created from it.
• Static variables are rarely used other than being declared as constants.
Constants are variables that are declared as public/private, final, and static.
Constant variables never change from their initial value.
• Static variables are stored in the static memory. It is rare to use static
variables other than declared final and used as either public or private
constants.
• Static variables are created when the program starts and destroyed when the
program stops.
• Static variables can be accessed by calling with the class
name ClassName.VariableName.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
23. SAMPLE PROGRAM
• import java.io.*;
• public class Employee
• {
• // salary variable is a private static variable
• private static double salary;
• // DEPARTMENT is a constant public static final String DEPARTMENT = "Development ";
• public static void main(String args[])
• {
• salary = 1000;
• System.out.println(DEPARTMENT + "average salary:" + salary);
• }
• }
OUTPUT
• Development average salary:1000
• Note − If the variables are accessed from an outside class, the constant should be accessed as
Employee.DEPARTMENT
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
24. MODIFIER TYPES
• Modifiers are keywords that you add to those definitions to change their
meanings. Java language has a wide variety of modifiers, including the
following −
1. Java Access Modifiers
2. Non Access Modifiers
ACCESS CONTROL MODIFIERS
• Java provides a number of access modifiers to set access levels for classes,
variables, methods and constructors. The four access levels are −
• Visible to the package, the default. No modifiers are needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
NON-ACCESS MODIFIERS
• Java provides a number of non-access modifiers to achieve many other
functionality.
• The static modifier for creating class methods and variables.
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
25. PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
• The final modifier for finalizing the implementations of classes, methods,
and variables.
• The abstract modifier for creating abstract classes and methods.
• The synchronized and volatile modifiers, which are used for threads.
26. DYNAMIC INITIALIZATION
• Class sample
• {
• Public static void main(String args[])
• {
• int a=,b=4; //dynamic intialization
• int c=math.sqrt(a*a + b*b);
• System.out.println(“The value of c is “+c);
• }
• }
• Output:
• The value of c is: 5
TYPE CONVERSIONS AND CASTING
• One data type automatically converted into another.
• Example:
• int a,b;
• float c;
• c=a+b;
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
27. STRING CLASS
• Strings, which are widely used in Java programming, are a sequence of characters. In Java
programming language, strings are treated as objects.
• The Java platform provides the String class to create and manipulate strings.
• Creating Strings
• The most direct way to create a string is to write
• String greeting = "Hello world!";
• Whenever it encounters a string literal in your code, the compiler creates a String object with
its value in this case, "Hello world!'.
• EXAMPLE
• public class StringDemo
• {
• public static void main(String args[])
• {
• char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
• String helloString = new String(helloArray);
• System.out.println( helloString );
• }}
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM