javanotes_unit2
javanotes_unit2
8) Define string ?how to declare it? What are String methods in java
9) Define exception ? what are the types of exceptions ?
Java Arrays
An array is a collection of similar data values with a single name. An array can also be
defined as, a special type of variable that holds multiple values of the same data type at
a time.
In java, arrays are objects and they are created dynamically using new operator. Every
array in java is organized using index values. The index value of an array starts with '0'
and ends with 'size-1'. We use the index value to access individual elements of an
array.
In java, there are two types of arrays and they are as follows.
Creating an array
In the java programming language, an array must be created using new operator and
with a specific size. The size must be an integer value
class arrayex
{
public static void main(String args[])
{
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
Multidimensional Array
In java, we can create an array with multiple dimensions. We can create 2-dimensional,
3-dimensional, or any dimensional array.
In Java, multidimensional arrays are arrays of arrays. To create a multidimensional
array variable, specify each additional index using another set of square brackets. We
use the following syntax to create two-dimensional array.
Syntax
When we create a two-dimensional array, it created with a separate index for rows and
columns. The individual element is accessed using the respective row index followed by
the column index.
//matrix ex
class arrayex
{
public static void main(String args[])
{
a[0][0]=10;
a[0][1]=20;
a[1][0]=30;
a[1][1]=40;
System.out.println("matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(a[i][j]+ " ");
}
System.out.println();
}
}
}
o/p
10 20
30 40
Java Classes
The inheritance is the process of acquiring the properties of one class to another class.
(or)
The inheritance is the process of creating new class from an existing class.
Here existing class is called parent class or super class or base class.
The Parent class is the class which provides features to another class. The parent class
is also known as Base class or Superclass.
The Child class is the class which receives features from another class. The child class
is also known as the Derived Class or Subclass.
In the inheritance, the child class acquires the features from its parent class. But
the parent class never acquires the features from its child class.
Advantages of inheritance:
1. Reusability of code
2. To increase the features to the base class
3. To increase the reliability of the base class.
Syntax
Syntax:
class <ParentClassName>
In java, we use the keyword extends to create a child class. The following syntax used
to create a child class in java.
Syntax:
In a java programming language, a class extends only one class. Extending multiple
classes is not allowed in java.
Multiple Inheritance
Multi-Level Inheritance
Hierarchical Inheritance
Hybrid Inheritance
class single
{
public static void main(String args[])
{
test k=new test();
k.getdata1();
k.getdata2();
k.display1();
k.display2();
}
}
o/p: rno=101
name=ram
m1=80
m2=70
1) Multiple inheritance:
2) Multilevel inheritance:
One class is derived from using another derived class is called multi
level inheritance ,
class student
{
int rno;
String name;
void getdata1()
{
rno=101;
name="ram";
}
void display1()
{
System.out.println("rno= "+rno);
System.out.println("name :"+name);
}
}
class test extends student
class multilevel
{
public static void main(String args[])
{
result k=new result();
k.getdata1();
k.getdata2();
k.display1();
k.display2();
k.display3();
}
}
o/p
rn0=101
name=ram
m1=80
m2=70
tot= 150
3) Hybrid inheritance:
Combination of multiple and multilevel is called hybrid inheritance.
It is also solved with interface .
1) this :
this is one type of special keyword in java . it can be used for two
purposes.
a) this keyword for variable
b) this() constructor
Example:
class add
{
int x,y,s;
add(int x,int y)
{
this.x=x;
this.y=y;
}
void display()
{
s=x+y;
System.out.println("sum= "+s);
}
}
class thiskey
{
public static void main(String args[])
{
add k=new add(10,20);
k.display();
}
}
class overload
{
overload(int x)
{
System.out.println("integer value x="+x);
}
overload(double y)
{
this(10);
System.out.println("double value y="+y);
}
overload(char z)
{
this(20.5);
System.out.println("character value z="+z);
}
}
class thiscon
{
public static void main(String args[])
{
}
}
super is also one of the special keyword in java. It can be used in 3 ways:
when super class variable name and sub class variables are same using
the super keyword we can access super class variables.
class base
{
int x=10;
}
class derived extends base
{
int x=20;
void display()
{
System.out.println("super class x value ="+super.x);
The super class and sub class have the same method name , same
arguments and same return type , then when we call super class method
,in that case the sub class method is executed instead of super class
method. . this type of concept is called method overridden .
To overcome overridden concept we can use super keyword to call the
super class method.
Syntax: super.methodname();
void display()
{
super.display();
System.out.println("this is derived class display function");
}
}
class supermethod
{
public static void main(String args[])
{
derived k=new derived();
k.display();
}
}
super() constructor is used to call super class constructor from sub class
constructor dependind on the passing parameters , the appropriate constructor
is executed.
// super constructor
class base
{
base()
{
System.out.println("this is base class funtion");
}
}
class derived extends base
{
derived()
{
super();
System.out.println("this is derived class function");
}
}
class supercon
{
public static void main(String args[])
{
derived k=new derived();
}
}
o/p: this base class function
this is derived class function
abstract class:
5) Explain about abstract classes in java?
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated that is it does not have definition part. Definition part
can be done in sub class.
o Abstract methods must be overridden in sub class.
// abstract class
void display1()
{
System.out.println("this is non abstarct method");
}
{
void display2()
{
System.out.println("this is abstract methdo ");
}
}
class abstractex
{
Interface in Java
An interface is basically a kind of class. Like classes interface is also contains variables
and methods.
class contains mixed data (variables and final constants) and mixed methods (normal
methods and abstract methods).
Syntax:
interface interface-name
// final variables
// abstract methods
When ever we want to add interface features in class we must use implements
keyword.
Ex:
-----------
There are mainly three reasons to use interface. They are given below.
//interface example
class student
{
int rno;
String name;
void getdata1()
{
rno=101;
name="ram";
}
void display1()
{
System.out.println("rno= "+rno);
System.out.println("name :"+name);
}
}
class test extends student
{
int m1,m2;
void getdata2()
{
m1=80;
m2=70;
}
void display2()
{
System.out.println("m1="+m1);
System.out.println("m2="+m2);
}
}
interface sports
{
final int spwt=5;
abstract public void display3();
class interfaceex
{
public static void main(String args[])
{
result k=new result();
k.getdata1();
k.getdata2();
k.display1();
k.display2();
k.display3();
}
}
2. user-defined package.
Types of packages:
1) java.lang:
Contains language support classes(e.g classed which defines primitive data types,
math operations). This package is automatically imported.
2) java.io:
3) java.util:
Contains utility classes which implement data structures like Linked List, Dictionary
and support ; for Date / Time operations.
4) java.applet:
5) java.awt:
Contain classes for implementing the components for graphical user interfaces (like
button , ;menus etc).
6) java.net:
2)User-defined packages
These are the packages that are defined by the user. That‟s why it is called
user defined package. Whenever we want access classes we must use import
statement as:
import packagename.classname;
Or
import package-name.*;
For creating user defined package the following steps are required :
S.Rajeshwari , Lecturer in Computers 20
Step1 : create a directory, which has the same name as package.
C:\>jdk1.7>bin>cd pack
C:>jdk1.7>bi>pack>
Step 2: open a new file and create a new package. Directory name and
package name must be same..and class and methods must be public.
1)first.java
package pack;
public class first
{
public void display()
{
System.out.println("this is first class display function");
}
}
Save file as first.java
2)second.java
package pack;
public class second
{
public void display()
{
System.out.println("this is second class display function");
}
}
Save file as second.java
Whwnevr we want to add these classes in another program , we must import classes
using the import statement.
3)imp.java
Import pack.*;
k1.display();
k2.display();
1. Private: The access level of a private modifier is only within the class. It cannot
be accessed from outside the class.
2. 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.
3. 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.
4. 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.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
8)Define string ?how to declare it? What are String methods in java
name[0]=‟j‟;
name[1]=‟a‟;
name[2]=‟v‟;
name[3]=‟a‟;
for(i=0;i<name.length;i++)
Java String literal is created by using String class and it is enclosed with in double
quotes. For Example:
System.out.println(s);
3) using StringBuffer class:
StringBuffer is also same as Sting class. But the difference between String and
StringBuffer is :
String class contains fixed size, but StringBuffer class contains flexible size .
Ex: StringBuffer s=new StringBuffer(“java”);
Example:
//string example
class stringexample
{
public static void main(String args[])
{
String s1=new String("Java Programming");
String s2=new String ("Language");
String s3=new String("JAVA PROGRAMMING");
System.out.println("no. of characters in s1 string is "+s1.length());
System.out.println("upper string is "+s1.toUpperCase());
System.out.println("upper string is "+s1.toLowerCase());
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that normal flow of the application can be maintained.
1) Checked Exception
These type of errors are occurred at the time of compile program. Ex: data
type mismatch error, syntax errors, semicolon missing etc.
These type of errors are occurs at the time of running the program. e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at
runtime.
In java language exception handling mechanism can be used to handle run time errors.
Keyword Description
try The "try" keyword is used to specify a block where we should place exception
code. The try block must be followed by either catch or finally. It means, we
can't use try block alone.
catch The "catch" block is used to handle the exception. It must be preceded by try
block which means we can't use catch block alone. It can be followed by
finally block later.
finally The "finally" block is used to execute the important code of the program. It is
executed whether an exception is handled or not.
try
{
x=a/(b-c);
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
y=a/(b+c);
System.out.println("y= "+y);
}
}
}
Built-in Exception:
1. Arithmetic exception : It is thrown when an exceptional condition has
occurred in an arithmetic operation.