SlideShare a Scribd company logo
UNIT-1 Introduction
 Introduction
 Feature of Java
 Java Virtual Machine
 Byte Code
 JDK
 JRE
 Comments
 Java coding convention
 Array and String:
 Single and Multidimensional Array,
 Command line argument
 String
 String Buffer
 String Pool, for-each loop
 var-length arguments. Wrapper Class
 Arrays class
• Class Object and Method:
• Class, Object
• Constructor
• Method overloading,
• Constructor overloading
• passing, and returning Object as function arguments
• passing and returning array of object in function
• new keyword, this keyword
• static variable, static method
• garbage collection
• finalize ()
• access modifier (default, public, private,
protected),Singleton class
• Diagram: Class Diagram, Object Diagram, Has-A
Relation
 Java is popular high-level, class-based object oriented programming language originally
developed by Sun Microsystems and released in 1995.
 Currently Java is owned by Oracle and more than 3 billion devices run Java. Java runs on a
variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
 Java is used to develop Mobile apps, Web apps, Desktop apps, Games and much more
• Java is Open Source which means its available free of cost.
• Java is simple and so easy to learn
• Java is much in demand and ensures high salary
• Java has a large vibrant community
• Java has powerful development tools
• Java is platform independent
 Object Oriented − In Java, everything is an Object. Java can be easily extended since it
is based on the Object model.
 Platform Independent − Unlike many other programming languages including C and
C++, when Java is compiled, it is not compiled into platform specific machine, rather into
platform independent byte code. This byte code is distributed over the web and
interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
 Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP
Java, it would be easy to master.
 Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
 Portable − Being architecture-neutral and having no implementation dependent aspects of the
specification makes Java portable. Compiler in Java is written in ANSI C with a clean
portability boundary, which is a POSIX subset.
 Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on
compile time error checking and runtime checking.
 Multithreaded − With Java's multithreaded feature it is possible to write programs that can
perform many tasks simultaneously. This design feature allows the developers to construct
interactive applications that can run smoothly.
 Interpreted − Java byte code is translated on the fly to native machine instructions and is
not stored anywhere. The development process is more rapid and analytical since the linking
is an incremental and light-weight process.
 High Performance − With the use of Just-In-Time compilers, Java enables high
performance.
 Distributed − Java is designed for the distributed environment of the internet.
 Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to
adapt to an evolving environment. Java programs can carry extensive amount of run-time
information that can be used to verify and resolve accesses to objects on run-time.
Java Runtime …
 The bytecode is similar to machine instructions but is architecture neutral and can run on
any platform that has a Java Virtual Machine (JVM).
 Rather than a physical machine, the virtual machine is a program that interprets Java
bytecode.
 This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware
platforms and operating systems.
 Java source code is compiled into Java bytecode and Java bytecode is interpreted by the
JVM
JAVA PROGRAM JAVA COMPILER VIRTUAL MACHINE
PROCESS OF COMPILATION
PROCESS OF CONVERTING BYTE CODE INTO MACHINE CODE
BYTE CODE JAVA
INTERPRETER MACHINE CODE
Java is platform independent due to this reason.
Single-line Comments
Sytem.out.println("Hello World"); // This is a comment
Java Multi-line Comments
/* The code below will print the words Hello World to the screen, and it is
amazing */
Sytem.out.println("Hello World");
 Normally, an array is a collection of similar type of elements which has contiguous
memory location.
 Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java array.
 Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
 Unlike C/C++, we can get the length of the array using the length member. In C/C++, we
need to use the size of operator.
• Advantages
• Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
• Random access: We can get any data located at an index position.
• Disadvantages
• Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
 Types of Array in java
• Single Dimensional Array
• Multidimensional Array
Syntax: dataType[] arr; (or) dataType arr[];
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
//Java Program to print the array elements using for-each loop
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}
 The java command-line argument is an argument i.e. passed at the time of running the java
program.
 The arguments passed from the console can be received in the java program and it can be
used as an input.
 So, it provides a convenient way to check the behavior of the program for the different
values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
 class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}
•compile by > javac CommandLineExample.java
•run by > java CommandLineExample Testing
•Output: Your first argument is: Testing
 Java uses System.out to refer to the standard output device and System.in to the
standard input device.
 By default, the output device is the display monitor and the input device is the
keyboard.
 To perform console output, you simply use the println method to display a primitive
value or a string to the console.
 Console input is not directly supported in Java, but you can use the Scanner class to
create an object to read input from System.in.
Scanner input = new Scanner(System.in);
double radius = input.nextDouble();
 In Java, string is basically an object that represents sequence of char values. An
array of characters works same as Java string.
 For example:
 char[] ch={‘M’,’b’,’I’,’t’};
 String s=new String(ch); is same as:
 String s=“Mbit“
 Java String class provides a lot of methods to perform operations on strings such
as compare(), concat(), equals(), split(), length(), replace(), compareTo(),
substring() etc
 Java String literal is created by using double quotes. For Example:
 String s="welcome";
 Each time you create a string literal, the JVM checks the "string constant pool"
first. If the string already exists in the pool, a reference to the pooled instance is
returned. If the string doesn't exist in the pool, a new string instance is created and
placed in the pool. For example:
 String s1="Welcome";
 String s2="Welcome";//It doesn't create a new instance
Continued …
public class StringExample{
public static void main(String args[]){
String s1="java";//creating string by Java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating Java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}} • java
• strings
• example
 Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer
class in Java is the same as String class except it is mutable i.e. it can be changed.
 A String that can be modified or changed is known as mutable String. StringBuffer and
StringBuilder classes are used for creating mutable strings.
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
• Hello Java
public class StringPoolExample
{
public static void main(String[] args)
{
String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");
String s4 = new String("Java").intern();
System.out.println((s1 == s2)+", String are equal."); // true
System.out.println((s1 == s3)+", String are not equal."); // false
System.out.println((s1 == s4)+", String are equal."); // true
}
}
The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. It provides an alternative approach to
traverse the array or collection in Java. It is mainly used to traverse the array or collection elements.
Advantages
It makes the code more readable.
It eliminates the possibility of programming errors.
//An example of Java for-each loop
class ForEachExample1{
public static void main(String args[]){
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
Variable Argument (Varargs)
The varargs allows the method to accept zero or muliple arguments
Advantage of Varargs:
We don't have to provide overloaded methods so less code.
Syntax of varargs:
return_type method_name(data_type... variableName){}
class VarargsExample1{
static void display(String... values){
System.out.println("display method invoked ");
}
public static void main(String args[]){
display();//zero argument
display("my","name","is","varargs");//four arguments
}
}
 The wrapper class in Java provides the mechanism to convert primitive into object and
object into primitive.
 Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and
objects into primitives automatically. The automatic conversion of primitive into an object is
known as autoboxing and vice-versa unboxing
 Java is an object-oriented programming language, so we need to deal with objects many
times like in Collections, Serialization, Synchronization, etc. Let us see the different
scenarios, where we need to use the wrapper classes.
 Change the value in Method: Java supports only call by value. So, if we pass a primitive
value, it will not change the original value. But, if we convert the primitive value in an
object, it will change the original value.
 Serialization: We need to convert the objects into streams to perform the serialization.
If we have a primitive value, we can convert it in objects through the wrapper classes.
 Synchronization: Java synchronization works with objects in Multithreading.
 java.util package: The java.util package provides the utility classes to deal with
objects.
 Collection Framework: Java collection framework works with objects only. All
classes of the collection framework (ArrayList, LinkedList, Vector, HashSet,
LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
 //Java program to convert primitive into objects
 //Autoboxing example of int to Integer
 public class WrapperExample1{
 public static void main(String args[]){
 //Converting int into Integer
 int a=20;
 Integer i=Integer.valueOf(a);//converting int into Integer explicitly
 Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
 System.out.println(a+" "+i+" "+j);
 }}
 20 20 20
//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}} 3 3 3
 The java.util.Arrays class contains a static factory that allows arrays to be viewed as
lists.Following are the important points about Arrays −
 This class contains various methods for manipulating arrays (such as sorting and searching).
 The methods in this class throw a NullPointerException if the specified array reference is null.
 public class Arrays
 extends Object
import java.util.Arrays;
public class first{
public static void main(String[] args)
{
int Arr[] = { 10, 20, 11, 21, 31 };
Arrays.sort(Arr);
int Key = 31;
System.out.println(Key + " found at index = “ + Arrays.binarySearch(Arr, Key));
}
}
• 31 found at index = 4
import java.util.Arrays;
public class Scaler {
public static void main(String[] args)
{
int Arr[] = { 10, 20, 11, 21, 31 };
int Arr1[] = { 10, 11, 21 };
System.out.println("Integer Arrays on comparison are : " + Arrays.equals(Arr, Arr1));
}
}
Integer Arrays on comparison are: false
import java.util.Arrays;
public class Scaler {
public static void main(String[] args)
{
int Arr[] = { 10, 20, 11, 21, 31 };
int Key = 23;
Arrays.fill(Arr, Key);
System.out.println("Integer Array on filling is: “ + Arrays.toString(Arr));
}
}
• Integer Array on filling is: [23, 23, 23, 23, 23]
import java.util.Arrays;
public class Scaler {
public static void main(String[] args)
{
int Arr[] = { 10, 20, 11, 21, 31 };
Arrays.sort(Arr);
System.out.println("Integer Array is: " + Arrays.toString(Arr));
}
}
• Integer Array is: [10, 11, 20, 21, 31]
 java.util.Arrays are a class that belongs to java.util package.
 Arrays class consists of static methods that make it easier to work with an array.
 The static methods of the Java Array class could be used to perform operations like
◦ filling the elements (fill(originalArray, fillValue))
◦ sorting the elements (sort(originalArray, fromIndex, endIndex))
◦ searching for the elements (binaySearch())
◦ converting the array elements to String (toString(original array))
◦ Many more…
 class <class_name>{
 field;
 method;
 Instance variable in Java
 A variable which is created inside the class but outside the method is
known as an instance variable.
 Method in Java
 In Java, a method is like a function which is used to expose the
behavior of an object.
 Advantage of Method
• Code Reusability
• Code Optimization
 An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen
Object and Class Example:
class Student{
int id
String name;
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
 In Java, a constructor is a block of codes similar to the method. It is
called when an instance of the class is created. At the time of calling
constructor, memory for the object is allocated in the memory.
 It is a special type of method which is used to initialize the object.
 Every time an object is created using the new() keyword, at least one
constructor is called.
 It calls a default constructor if there is no constructor available in the
class. In such case, Java compiler provides a default constructor by
default.
 Constructor name must be the same as its class name
 A Constructor must have no explicit return type
 A Java constructor cannot be abstract, static, final, and synchronized.
 Example of default constructor
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
The default constructor is used to provide the default values to the object like 0,
null, etc., depending on the type
//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}
• 0 null
• 0 null
Example of parameterized constructor /Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
• O/P : 111 Karan
222 Aryan
 If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading. Or
 Same Method names with different arguments , may or may not be same return type in class
itself.
 Method Overloading is static / Early Binding Polymorphism.
 Overloading occurs in Same class.
 Advantage of method overloading
 Method overloading increases the readability of the program.
 Different ways to overload the method
 There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
O/P: 22
33
 In Java, we can overload constructors like methods. The constructor overloading can be
defined as the concept of having more than one constructor with different parameters so that
every constructor can perform a different task.
public class Student {
//instance variables of the class
int id;
String name;
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student();
System.out.println("nDefault Constructor values: n");
System.out.println("Student Id : "+s.id + "nStudent Name : "+s.name);
System.out.println("nParameterized Constructor values: n");
Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "nStudent Name : "+student.name);
}
}
• O/P
• this a default constructor
• Default Constructor values:
• Student Id : 0
• Student Name : null
• Parameterized Constructor values:
• Student Id : 10
• Student Name : David
import java.util.Arrays;
public class ReturnArrayExample1
{
public static void main(String args[])
{
int[] a=numbers(); //obtain the array
for (int i = 0; i < a.length; i++) //for loop to print the array
System.out.print( a[i]+ " ");
}
public static int[] numbers()
{
int[] arr={5,6,7,8,9}; //initializing array
return arr;
}
}
public class ReturnArrayExample3
{
public static double[] returnArray( )
{
double[] arr = new double [3]; // Creating an array of 3 elements
arr[0]=6.9;
arr [1]=2.5;
arr [2]=11.5;
return( x ); // Return the reference of the array
}
public static void main(String[] args)
{
double[] a; //variable to store returned array
a = returnArray(); //called method
for (int i = 0; i < a.length; i++) //for loop to print the array
System.out.println( a[i]+ " ");
}
}
 The Java new keyword is used to create an instance of the class. In other words, it
instantiates a class by allocating memory for a new object and returning a reference to that
memory. We can also use the new keyword to create the array object.
 NewExample obj=new NewExample();
• It is used to create the object.
• It allocates the memory at runtime.
• All objects occupy memory in the heap area.
• It invokes the object constructor.
public class NewExample1 {
void display()
{
System.out.println("Invoking Method");
}
public static void main(String[] args) {
NewExample1 obj=new NewExample1();
obj.display();
}
 The this keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and parameters, this keyword resolves the problem of ambiguity.
 “this” can be used in both super and sub classes.
 “this” keyword is used to invoke the current class properties.
 This() is used to invoke default constructor of same class.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
111 ankit 5000.0
112 sumit 6000.0
 If you declare any variable as static, it is known as a static variable.
• The static variable can be used to refer to the common property of all objects (which is not
unique for each object), for example, the company name of employees, college name of
students, etc.
• The static variable gets memory only once in the class area at the time of class loading.
• It makes your program memory efficient (i.e., it saves memory).
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
• 111 Karan ITS
• 222 Aryan ITS
 If you apply static keyword with any method, it is known as static method.
 A static method can be invoked without the need for creating an instance of a class.
 A static method can access static data member and can change the value of it.
Java programing language unit 1 introduction
 In java, garbage means unreferenced objects.
 Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is
a way to destroy the unused objects.
 To do so, we were using free() function in C language and delete() in C++. But, in java it is performed
automatically. So, java provides better memory management.
 Advantage of Garbage Collection
• It makes java memory efficient because garbage collector removes the unreferenced objects from heap
memory.
• It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
OPEN DATE
START DATE
1-10-2005
10-10-2005
Released
Automatically
The finalize() method is
invoked each time before the
object is garbage collected.
This method can be used to
perform cleanup processing.
This method is defined in
Object class as:
protected void finalize(){}
 There are four types of Java access modifiers:
 Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
 Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the default.
 Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
 Public: The access level of a public modifier is everywhere. It can be accessed from within the
class, outside the class, within the package and outside the package.
Java programing language unit 1 introduction

More Related Content

Similar to Java programing language unit 1 introduction (20)

JAVA Module 1______________________.pptx
JAVA Module 1______________________.pptxJAVA Module 1______________________.pptx
JAVA Module 1______________________.pptx
Radhika Venkatesh
 
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
JAVA AND OOPS CONCEPTS.pptx helpful for engineeringJAVA AND OOPS CONCEPTS.pptx helpful for engineering
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
PriyanshuGupta101797
 
OOPS JAVA.pdf
OOPS JAVA.pdfOOPS JAVA.pdf
OOPS JAVA.pdf
DeepanshuMidha5140
 
Introduction To Java.
Introduction To Java.Introduction To Java.
Introduction To Java.
Tushar Chauhan
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
EduclentMegasoftel
 
java slides
java slidesjava slides
java slides
RizwanTariq18
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
JitendraYadav351971
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
MAYANKKUMAR492040
 
Introduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptxIntroduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptx
SANDHYAP32
 
INTRODUCTION TO JAVA
INTRODUCTION TO JAVAINTRODUCTION TO JAVA
INTRODUCTION TO JAVA
Pintu Dasaundhi (Rahul)
 
UNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptxUNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptx
amanuel236786
 
Srgoc java
Srgoc javaSrgoc java
Srgoc java
Gaurav Singh
 
Jacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.pptJacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
Java basic
Java basicJava basic
Java basic
Arati Gadgil
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Chapter One Basics ofJava Programmming.pptx
Chapter One Basics ofJava Programmming.pptxChapter One Basics ofJava Programmming.pptx
Chapter One Basics ofJava Programmming.pptx
Prashant416351
 
Java
JavaJava
Java
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH, SINDH, PAKISTAN
 
java ppt for basic intro about java and its
java ppt for basic intro about java and itsjava ppt for basic intro about java and its
java ppt for basic intro about java and its
kssandhu875
 
Java - A parent language and powerdul for mobile apps.
Java -  A parent language and powerdul for mobile apps.Java -  A parent language and powerdul for mobile apps.
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 
JAVA Module 1______________________.pptx
JAVA Module 1______________________.pptxJAVA Module 1______________________.pptx
JAVA Module 1______________________.pptx
Radhika Venkatesh
 
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
JAVA AND OOPS CONCEPTS.pptx helpful for engineeringJAVA AND OOPS CONCEPTS.pptx helpful for engineering
JAVA AND OOPS CONCEPTS.pptx helpful for engineering
PriyanshuGupta101797
 
Introduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptxIntroduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptx
SANDHYAP32
 
UNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptxUNIT 1 : object oriented programming.pptx
UNIT 1 : object oriented programming.pptx
amanuel236786
 
Jacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.pptJacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Chapter One Basics ofJava Programmming.pptx
Chapter One Basics ofJava Programmming.pptxChapter One Basics ofJava Programmming.pptx
Chapter One Basics ofJava Programmming.pptx
Prashant416351
 
java ppt for basic intro about java and its
java ppt for basic intro about java and itsjava ppt for basic intro about java and its
java ppt for basic intro about java and its
kssandhu875
 
Java - A parent language and powerdul for mobile apps.
Java -  A parent language and powerdul for mobile apps.Java -  A parent language and powerdul for mobile apps.
Java - A parent language and powerdul for mobile apps.
dhimananshu130803
 

More from chnrketan (6)

NON-PREEMPTIVE_SCHEDULING operating system
NON-PREEMPTIVE_SCHEDULING operating systemNON-PREEMPTIVE_SCHEDULING operating system
NON-PREEMPTIVE_SCHEDULING operating system
chnrketan
 
priority interrupt computer organization
priority interrupt computer organizationpriority interrupt computer organization
priority interrupt computer organization
chnrketan
 
Seminar_ketan metaverse technology and a
Seminar_ketan metaverse technology and aSeminar_ketan metaverse technology and a
Seminar_ketan metaverse technology and a
chnrketan
 
Database managementsystemes_Unit-7.pptxe
Database managementsystemes_Unit-7.pptxeDatabase managementsystemes_Unit-7.pptxe
Database managementsystemes_Unit-7.pptxe
chnrketan
 
Framing in DLL computer networks and layers
Framing in DLL computer networks and layersFraming in DLL computer networks and layers
Framing in DLL computer networks and layers
chnrketan
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
chnrketan
 
NON-PREEMPTIVE_SCHEDULING operating system
NON-PREEMPTIVE_SCHEDULING operating systemNON-PREEMPTIVE_SCHEDULING operating system
NON-PREEMPTIVE_SCHEDULING operating system
chnrketan
 
priority interrupt computer organization
priority interrupt computer organizationpriority interrupt computer organization
priority interrupt computer organization
chnrketan
 
Seminar_ketan metaverse technology and a
Seminar_ketan metaverse technology and aSeminar_ketan metaverse technology and a
Seminar_ketan metaverse technology and a
chnrketan
 
Database managementsystemes_Unit-7.pptxe
Database managementsystemes_Unit-7.pptxeDatabase managementsystemes_Unit-7.pptxe
Database managementsystemes_Unit-7.pptxe
chnrketan
 
Framing in DLL computer networks and layers
Framing in DLL computer networks and layersFraming in DLL computer networks and layers
Framing in DLL computer networks and layers
chnrketan
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
chnrketan
 
Ad

Recently uploaded (20)

A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdfIntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdf
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Ad

Java programing language unit 1 introduction

  • 2.  Introduction  Feature of Java  Java Virtual Machine  Byte Code  JDK  JRE  Comments  Java coding convention  Array and String:  Single and Multidimensional Array,  Command line argument  String  String Buffer  String Pool, for-each loop  var-length arguments. Wrapper Class  Arrays class • Class Object and Method: • Class, Object • Constructor • Method overloading, • Constructor overloading • passing, and returning Object as function arguments • passing and returning array of object in function • new keyword, this keyword • static variable, static method • garbage collection • finalize () • access modifier (default, public, private, protected),Singleton class • Diagram: Class Diagram, Object Diagram, Has-A Relation
  • 3.  Java is popular high-level, class-based object oriented programming language originally developed by Sun Microsystems and released in 1995.  Currently Java is owned by Oracle and more than 3 billion devices run Java. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.  Java is used to develop Mobile apps, Web apps, Desktop apps, Games and much more
  • 4. • Java is Open Source which means its available free of cost. • Java is simple and so easy to learn • Java is much in demand and ensures high salary • Java has a large vibrant community • Java has powerful development tools • Java is platform independent
  • 5.  Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.  Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.  Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
  • 6.  Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.  Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.  Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.  Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
  • 7.  Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.  High Performance − With the use of Just-In-Time compilers, Java enables high performance.  Distributed − Java is designed for the distributed environment of the internet.  Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.
  • 9.  The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM).  Rather than a physical machine, the virtual machine is a program that interprets Java bytecode.  This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems.  Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM
  • 10. JAVA PROGRAM JAVA COMPILER VIRTUAL MACHINE PROCESS OF COMPILATION PROCESS OF CONVERTING BYTE CODE INTO MACHINE CODE BYTE CODE JAVA INTERPRETER MACHINE CODE
  • 11. Java is platform independent due to this reason.
  • 12. Single-line Comments Sytem.out.println("Hello World"); // This is a comment Java Multi-line Comments /* The code below will print the words Hello World to the screen, and it is amazing */ Sytem.out.println("Hello World");
  • 13.  Normally, an array is a collection of similar type of elements which has contiguous memory location.  Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.  Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.  Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the size of operator.
  • 14. • Advantages • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. • Random access: We can get any data located at an index position. • Disadvantages • Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
  • 15.  Types of Array in java • Single Dimensional Array • Multidimensional Array Syntax: dataType[] arr; (or) dataType arr[]; class Testarray{ public static void main(String args[]){ int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //traversing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }}
  • 16. //Java Program to print the array elements using for-each loop class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; //printing array using for-each loop for(int i:arr) System.out.println(i); }}
  • 17.  The java command-line argument is an argument i.e. passed at the time of running the java program.  The arguments passed from the console can be received in the java program and it can be used as an input.  So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.  class CommandLineExample{ public static void main(String args[]){ System.out.println("Your first argument is: "+args[0]); } }
  • 18. •compile by > javac CommandLineExample.java •run by > java CommandLineExample Testing •Output: Your first argument is: Testing
  • 19.  Java uses System.out to refer to the standard output device and System.in to the standard input device.  By default, the output device is the display monitor and the input device is the keyboard.  To perform console output, you simply use the println method to display a primitive value or a string to the console.  Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in. Scanner input = new Scanner(System.in); double radius = input.nextDouble();
  • 20.  In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string.  For example:  char[] ch={‘M’,’b’,’I’,’t’};  String s=new String(ch); is same as:  String s=“Mbit“  Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), substring() etc
  • 21.  Java String literal is created by using double quotes. For Example:  String s="welcome";  Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:  String s1="Welcome";  String s2="Welcome";//It doesn't create a new instance
  • 23. public class StringExample{ public static void main(String args[]){ String s1="java";//creating string by Java string literal char ch[]={'s','t','r','i','n','g','s'}; String s2=new String(ch);//converting char array to string String s3=new String("example");//creating Java string by new keyword System.out.println(s1); System.out.println(s2); System.out.println(s3); }} • java • strings • example
  • 24.  Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is the same as String class except it is mutable i.e. it can be changed.  A String that can be modified or changed is known as mutable String. StringBuffer and StringBuilder classes are used for creating mutable strings. class StringBufferExample{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } } • Hello Java
  • 25. public class StringPoolExample { public static void main(String[] args) { String s1 = "Java"; String s2 = "Java"; String s3 = new String("Java"); String s4 = new String("Java").intern(); System.out.println((s1 == s2)+", String are equal."); // true System.out.println((s1 == s3)+", String are not equal."); // false System.out.println((s1 == s4)+", String are equal."); // true } }
  • 26. The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. It provides an alternative approach to traverse the array or collection in Java. It is mainly used to traverse the array or collection elements. Advantages It makes the code more readable. It eliminates the possibility of programming errors. //An example of Java for-each loop class ForEachExample1{ public static void main(String args[]){ //declaring an array int arr[]={12,13,14,44}; //traversing the array with for-each loop for(int i:arr){ System.out.println(i); } } }
  • 27. Variable Argument (Varargs) The varargs allows the method to accept zero or muliple arguments Advantage of Varargs: We don't have to provide overloaded methods so less code. Syntax of varargs: return_type method_name(data_type... variableName){} class VarargsExample1{ static void display(String... values){ System.out.println("display method invoked "); } public static void main(String args[]){ display();//zero argument display("my","name","is","varargs");//four arguments } }
  • 28.  The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.  Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing  Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.  Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
  • 29.  Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.  Synchronization: Java synchronization works with objects in Multithreading.  java.util package: The java.util package provides the utility classes to deal with objects.  Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.
  • 30. Primitive Type Wrapper class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double
  • 31.  //Java program to convert primitive into objects  //Autoboxing example of int to Integer  public class WrapperExample1{  public static void main(String args[]){  //Converting int into Integer  int a=20;  Integer i=Integer.valueOf(a);//converting int into Integer explicitly  Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  System.out.println(a+" "+i+" "+j);  }}  20 20 20
  • 32. //Java program to convert object into primitives //Unboxing example of Integer to int public class WrapperExample2{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(3); int i=a.intValue();//converting Integer to int explicitly int j=a;//unboxing, now compiler will write a.intValue() internally System.out.println(a+" "+i+" "+j); }} 3 3 3
  • 33.  The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists.Following are the important points about Arrays −  This class contains various methods for manipulating arrays (such as sorting and searching).  The methods in this class throw a NullPointerException if the specified array reference is null.  public class Arrays  extends Object
  • 34. import java.util.Arrays; public class first{ public static void main(String[] args) { int Arr[] = { 10, 20, 11, 21, 31 }; Arrays.sort(Arr); int Key = 31; System.out.println(Key + " found at index = “ + Arrays.binarySearch(Arr, Key)); } } • 31 found at index = 4
  • 35. import java.util.Arrays; public class Scaler { public static void main(String[] args) { int Arr[] = { 10, 20, 11, 21, 31 }; int Arr1[] = { 10, 11, 21 }; System.out.println("Integer Arrays on comparison are : " + Arrays.equals(Arr, Arr1)); } } Integer Arrays on comparison are: false
  • 36. import java.util.Arrays; public class Scaler { public static void main(String[] args) { int Arr[] = { 10, 20, 11, 21, 31 }; int Key = 23; Arrays.fill(Arr, Key); System.out.println("Integer Array on filling is: “ + Arrays.toString(Arr)); } } • Integer Array on filling is: [23, 23, 23, 23, 23]
  • 37. import java.util.Arrays; public class Scaler { public static void main(String[] args) { int Arr[] = { 10, 20, 11, 21, 31 }; Arrays.sort(Arr); System.out.println("Integer Array is: " + Arrays.toString(Arr)); } } • Integer Array is: [10, 11, 20, 21, 31]
  • 38.  java.util.Arrays are a class that belongs to java.util package.  Arrays class consists of static methods that make it easier to work with an array.  The static methods of the Java Array class could be used to perform operations like ◦ filling the elements (fill(originalArray, fillValue)) ◦ sorting the elements (sort(originalArray, fromIndex, endIndex)) ◦ searching for the elements (binaySearch()) ◦ converting the array elements to String (toString(original array)) ◦ Many more…
  • 39.  class <class_name>{  field;  method;  Instance variable in Java  A variable which is created inside the class but outside the method is known as an instance variable.  Method in Java  In Java, a method is like a function which is used to expose the behavior of an object.  Advantage of Method • Code Reusability • Code Optimization
  • 40.  An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen Object and Class Example: class Student{ int id String name; public static void main(String args[]){ Student s1=new Student(); System.out.println(s1.id); System.out.println(s1.name); } }
  • 41.  In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.  It is a special type of method which is used to initialize the object.  Every time an object is created using the new() keyword, at least one constructor is called.  It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.  Constructor name must be the same as its class name  A Constructor must have no explicit return type  A Java constructor cannot be abstract, static, final, and synchronized.
  • 42.  Example of default constructor //Java Program to create and call a default constructor class Bike1{ //creating a default constructor Bike1(){System.out.println("Bike is created");} //main method public static void main(String args[]){ //calling a default constructor Bike1 b=new Bike1(); } }
  • 43. The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type //Let us see another example of default constructor //which displays the default values class Student3{ int id; String name; //method to display the value of id and name void display(){System.out.println(id+" "+name); } public static void main(String args[]){ //creating objects Student3 s1=new Student3(); Student3 s2=new Student3(); //displaying values of the object s1.display(); s2.display(); } } • 0 null • 0 null
  • 44. Example of parameterized constructor /Java Program to demonstrate the use of the parameterized constructor. class Student4{ int id; String name; //creating a parameterized constructor Student4(int i,String n){ id = i; name = n; } //method to display the values void display(){System.out.println(id+" "+name); } public static void main(String args[]){ //creating objects and passing values Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); //calling method to display the values of object s1.display(); s2.display(); } } • O/P : 111 Karan 222 Aryan
  • 45.  If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. Or  Same Method names with different arguments , may or may not be same return type in class itself.  Method Overloading is static / Early Binding Polymorphism.  Overloading occurs in Same class.  Advantage of method overloading  Method overloading increases the readability of the program.  Different ways to overload the method  There are two ways to overload the method in java 1. By changing number of arguments 2. By changing the data type
  • 46. class Adder { static int add(int a,int b) { return a+b; } static int add(int a,int b,int c) { return a+b+c; } } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }} O/P: 22 33
  • 47.  In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
  • 48. public class Student { //instance variables of the class int id; String name; Student(){ System.out.println("this a default constructor"); } Student(int i, String n){ id = i; name = n; } public static void main(String[] args) { //object creation Student s = new Student(); System.out.println("nDefault Constructor values: n"); System.out.println("Student Id : "+s.id + "nStudent Name : "+s.name); System.out.println("nParameterized Constructor values: n"); Student student = new Student(10, "David"); System.out.println("Student Id : "+student.id + "nStudent Name : "+student.name); } } • O/P • this a default constructor • Default Constructor values: • Student Id : 0 • Student Name : null • Parameterized Constructor values: • Student Id : 10 • Student Name : David
  • 49. import java.util.Arrays; public class ReturnArrayExample1 { public static void main(String args[]) { int[] a=numbers(); //obtain the array for (int i = 0; i < a.length; i++) //for loop to print the array System.out.print( a[i]+ " "); } public static int[] numbers() { int[] arr={5,6,7,8,9}; //initializing array return arr; } }
  • 50. public class ReturnArrayExample3 { public static double[] returnArray( ) { double[] arr = new double [3]; // Creating an array of 3 elements arr[0]=6.9; arr [1]=2.5; arr [2]=11.5; return( x ); // Return the reference of the array } public static void main(String[] args) { double[] a; //variable to store returned array a = returnArray(); //called method for (int i = 0; i < a.length; i++) //for loop to print the array System.out.println( a[i]+ " "); } }
  • 51.  The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.  NewExample obj=new NewExample(); • It is used to create the object. • It allocates the memory at runtime. • All objects occupy memory in the heap area. • It invokes the object constructor.
  • 52. public class NewExample1 { void display() { System.out.println("Invoking Method"); } public static void main(String[] args) { NewExample1 obj=new NewExample1(); obj.display(); }
  • 53.  The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.  “this” can be used in both super and sub classes.  “this” keyword is used to invoke the current class properties.  This() is used to invoke default constructor of same class.
  • 54. class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ this.rollno=rollno; this.name=name; this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis2{ public static void main(String args[]){ Student s1=new Student(111,"ankit",5000f); Student s2=new Student(112,"sumit",6000f); s1.display(); s2.display(); }} 111 ankit 5000.0 112 sumit 6000.0
  • 55.  If you declare any variable as static, it is known as a static variable. • The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. • The static variable gets memory only once in the class area at the time of class loading. • It makes your program memory efficient (i.e., it saves memory).
  • 56. //Java Program to demonstrate the use of static variable class Student{ int rollno;//instance variable String name; static String college ="ITS";//static variable //constructor Student(int r, String n){ rollno = r; name = n; } //method to display the values void display (){System.out.println(rollno+" "+name+" "+college);} } //Test class to show the values of objects public class TestStaticVariable1{ public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); //we can change the college of all objects by the single line of code //Student.college="BBDIT"; s1.display(); s2.display(); } • 111 Karan ITS • 222 Aryan ITS
  • 57.  If you apply static keyword with any method, it is known as static method.  A static method can be invoked without the need for creating an instance of a class.  A static method can access static data member and can change the value of it.
  • 59.  In java, garbage means unreferenced objects.  Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.  To do so, we were using free() function in C language and delete() in C++. But, in java it is performed automatically. So, java provides better memory management.  Advantage of Garbage Collection • It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. • It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts. OPEN DATE START DATE 1-10-2005 10-10-2005 Released Automatically
  • 60. The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in Object class as: protected void finalize(){}
  • 61.  There are four types of Java access modifiers:  Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.  Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.  Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.  Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.