Wednesday, 25 July 2018
Tuesday, 24 July 2018
Summary table of Java Modifiers and Access Specifiers.
- The only applicable modifier for the local variable is final.
- The only applicable modifiers for constructor are public, private, default, protected.
- The modifiers which are applicable only to the method is native.
- The modifiers which are applicable only for variables volatile and transient.
- The modifiers which are applicable for classes but not for an interface is final.
- The modifier which is applicable for classes but not for enum is final, abstract.
Tuesday, 10 July 2018
//Array of ArrayList Demonstration in java
import java.util.*;
public class ArrayOfArrayList
{
public static void main(String args[])
{
ArrayList listArray[] = new ArrayList[2];
ArrayList firstList = new ArrayList();
firstList.add("Java");
firstList.add("Technology");
ArrayList secondList = new ArrayList();
secondList.add("Govind");
secondList.add("Ballabh");
secondList.add("Khan");
listArray[0] = firstList;
listArray[1] = secondList;
System.out.println(listArray[0]);
System.out.println(listArray[1]);
}
}
// Java program to illustrate creating an array of objects
class Student
{
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of array are objects of a class Student.
public class StudentDemo
{
public static void main (String[] args)
{
// declares an Array of Student Object.
Student[] arr;
// allocating memory for 5 objects of type Student.
arr = new Student[5];
// initialize the first elements of the array
arr[0] = new Student(1,"govind");
// initialize the second elements of the array
arr[1] = new Student(2,"ajit");
// so on...
arr[2] = new Student(3,"mani");
arr[3] = new Student(4,"dinesh");
arr[4] = new Student(5,"sanjay");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}
class Student
{
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of array are objects of a class Student.
public class StudentDemo
{
public static void main (String[] args)
{
// declares an Array of Student Object.
Student[] arr;
// allocating memory for 5 objects of type Student.
arr = new Student[5];
// initialize the first elements of the array
arr[0] = new Student(1,"govind");
// initialize the second elements of the array
arr[1] = new Student(2,"ajit");
// so on...
arr[2] = new Student(3,"mani");
arr[3] = new Student(4,"dinesh");
arr[4] = new Student(5,"sanjay");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}
One Dimensional Array in java
public class OneDimensionalArray
{
public static void main(String args[])
{
int marks[] = new int[5];
System.out.println("Elements are " + marks.length);
// Now assign values
marks[0] = 68;
marks[1] = 70;
marks[2] = 76;
marks[3] = 85;
marks[4] = 94;
System.out.println("Value of 1st element: " + marks[0]);
System.out.println("Value of 2nd element: " + marks[1]);
System.out.println("Value of 3rd element: " + marks[2]);
System.out.println("Value of 4th element: " + marks[3]);
System.out.println("Value of 5th element: " + marks[4]);
System.out.println("Printing through for loop ");
for(int i = 0; i < marks.length; i++)
{
System.out.println(marks[i] );
}
System.out.println("Printing through for each loop");
for(int k : marks)
{
System.out.println(k);
}
}
}
Monday, 9 July 2018
//String array in java
public class Demo
{
public static void main(String args[ ])
{
String fname = "Govind";
String mname = "Ballabh";
String lname = "Khan";
public class Demo
{
public static void main(String args[ ])
{
String fname = "Govind";
String mname = "Ballabh";
String lname = "Khan";
System.out.println("========================Names===============================");
String names[] = {fname, mname, lname};
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
System.out.println("========================Cities===============================");
// we can create like this also
String cities[ ] = {"patna", "Bhopal", "Delhi"};
for(int i = 0; i < cities.length; i++)
{
System.out.println(cities[i]);
}
// we can create like this also
String cities[ ] = {"patna", "Bhopal", "Delhi"};
for(int i = 0; i < cities.length; i++)
{
System.out.println(cities[i]);
}
System.out.println("========================Language===============================");
// we can do like this also
String languages[ ] = new String[6];
languages[0] = "C";
languages[1] = "C++";
languages[2] = "Java";
languages[4] = ".Net";
languages[5] = ".Php";
for(int i = 0; i <languages.length; i++)
{
System.out.println(languages[i]);
}
}
}
// we can do like this also
String languages[ ] = new String[6];
languages[0] = "C";
languages[1] = "C++";
languages[2] = "Java";
languages[4] = ".Net";
languages[5] = ".Php";
for(int i = 0; i <languages.length; i++)
{
System.out.println(languages[i]);
}
}
}
Subscribe to:
Posts (Atom)