Java

=============================================================
Java Features
=============================================================

 Simple
 Object-Oriented
 Distributed
 Robust
 Secure
 System Independence
 Portability
 Interpreted
 High Performance
 Multithreaded
 Scalability
 Dynamic

Simple:
----------

When java is developed, they wanted it to be simple because it has to work on  electronic devices, where

less memory is available.

 The difficult concepts of C and C++ omitted in java. eg: Pointers which difficult for both learners and

programmers.

The Syntaxes of C and C++.

Object-Oriented:
-----------------------

Java is an object oriented programming language. It uses objects and classes.  It provides reusablility.

Distributed:
----------------

Information is distributed on various computers on a network.  Using java we can write programs, which

capture information and distribute it to the clients.  Because java can handle the protocols like TCP/IP

and UDP.

Robust:
----------

Robust means strong.  Java programs are strong and they don't crash easily like a C or C++ program.  It

has excellent feature i.e. Exception Handling.  An exception is an error that occurs at runtime.

Another feature is memory management.We need not allocate or deallocate memory in Java.  Everything

will be taken care of by JVM only. In C and C++ we have to allocate to dellocate memory.

Secure:
----------

Security problems like evedropping, tampering, impersonation and virus threats can be eliminated or

minimized by using java on internet.

System Independence:

Java's byte code is not machine dependent.  It can be run on any machine with any processor and any

operating system.

Portability:
----------------

If a program yields the same result on every machine, then that program is called portable.  Java

programs are portable.  This is the result of Java's Sysem independence nature.

Interpreted:
-----------------

Java programs are compiled to generate the byte code.  if we take any other language, only an interpreter

or a compiler is used to execute the programs.  But in java, we use both compiler and interpreter for the

execution.

High Performance:
-------------------------

The problem with intepreter inside the JVM is that is slow.  Because of this, java programs used to run

slow.  To overcome this problem, along with the interpreter, JavaSoft people have introduced JIT(Just In

Time) compiler, which enhances the speed of execution.  So in JVM, both interpreter and JIT compiler

work together to run the program.

Multithread:
------------------

A thread represents an individual process to execute a group of statements.  JVM uses several threads to

execute different blocks of code.  Creating multiple threads is called multithreaded.

Scalability:
--------------

Java platform can be implemented on a wide range of computers with varying levels of resources - from

embedded divices to mainframe computers.  This is possible because java is compact and platform

independent.

Dynamic:
---------------

Before the development of java, only static text is used to be displayed in the browser.  But java provides

dynamically interacting programs on Internet.







Operators in Java
================









Example 1:
---------------
class A
{
 public static void main(String args[])
 {
 System.out.println("THis is First Program..");
 }
}
Example 2:
---------------
class A5
{
 public static void main(String args[])
 {
 int a=10;
 System.out.println("a...."+a);
 }
}


Example 3:
---------------
class A
{
 public static void main(String args[])
 {
 int x=10;
 int y=20;
 int z=x+y;
 System.out.println("The Sum is "+z);
 }
}

Example 4:
---------------
/*Using Command Line Arguments */

class A
{
 public static void main(String args[])
 {
 int x,y,z;
  x=Integer.parseInt(args[0]);
  y=Integer.parseInt(args[1]);
 z=x+y;
 System.out.println("Your First Number is "+ x);
 System.out.println("Your Second Number is "+ y);
 System.out.println("The Sum is "+z);

 }
}

javac A.java
java A 10 20

Example 5:
---------------
//Using Scanner Class

import java.util.*;
class A2
{
 public static void main(String args[])
 {
  Scanner s= new Scanner(System.in);
  int x,y,z;
  System.out.print("Enter a Number to x: ");
  x=s.nextInt();
  System.out.print("Enter a Number to y: ");
  y=s.nextInt();
  z=x+y;
  System.out.println("The Sum of " + x + " and " + y+" is "+z);
 }
}


Example 6:
---------------
//Using BufferedReader Class

import java.io.*;
class A3
{
 public static void main(String args[]) throws Exception
 {
  BufferedReader B=new BufferedReader(new InputStreamReader(System.in));
  int num;
  System.out.print("Enter a number.... ");
  num = Integer.parseInt(B.readLine());
  System.out.println("U entered....."+num);
  System.out.printf("U entered.....%d",num);
 }
}
Example 7:
---------------
//To display System Date

import java.util.Date;

class A4
{
 public static void main(String args[])
 {
 Date today=new Date();
 System.out.println("Today's Date is " +today);
 }
}

Example 8:
---------------

class A6
{
 public static void main(String args[])
 {
  int empNumber;
  float salary;
  char gender='M';
  double shareBalence=456790.087;
  boolean ownVehicle=false;

  empNumber=101;
  salary = 6789.50f;

  System.out.println("Emp Number :"+empNumber);
  System.out.println("Salary :"+salary);
  System.out.println("Gender :"+gender);
  System.out.println("ShareBalence :"+shareBalence);
  System.out.println("owns vehicle :"+ownVehicle);
 }
}


Example 9:
---------------
//If Condition

class A1
{
 public static void main(String[] args)
 {
 int x=10,y=20;

 if(x>y)
 {
  System.out.println("x is Greater");
 }
 else
 {
  System.out.println("y is Greater");
 }
 }
}

Example 10:
---------------

class A2
{
 public static void main(String[] args)
 {
 int x,y,z;

 x=Integer.parseInt(args[0]);
 y=Integer.parseInt(args[1]);
 z=Integer.parseInt(args[2]);

 if(x>y && x>z)
 {
  System.out.println(x + " is greater..");
 }
 else if(y>z)
 {
  System.out.println(y + " is greater..");
 }
 else
 {
  System.out.println(z + " is greater..");
 }
 }
}

javac A2.java
java A2 10 20 30

Example 11:
---------------
import java.util.*;

class A3
{
 public static void main(String[] args)
 {
 Scanner S=new Scanner(System.in);

 int x,y,z;
 System.out.print("Enter Value to x: ");
 x=S.nextInt();
 System.out.print("Enter Value to y: ");
 y=S.nextInt();
 System.out.print("Enter Value to z: ");
 z=S.nextInt();

 if(x>y && x>z)
 {
  System.out.println(x + " is greater..");
 }
 else if(y>z)
 {
  System.out.println(y + " is greater..");
 }
 else
 {
  System.out.println(z + " is greater..");
 }
 }
}

Example 12:
---------------
class A4
{
 public static void main(String[] args)
 {
 int x=1;

 while(x<=10)
 {
 System.out.println(x);
 x++;
 }
 }
}
Example 13:
---------------
class A5
{
 public static void main(String args[])
 {
 int i=10;

 do
 {
  System.out.println(i);
  i--;
 }
 while(i>=1);
 }
}

Example 14:
---------------
class A6
{
 public static void main(String[] ar)
 {
 for(int i=2;i<=20;i=i+2)
 {
  System.out.println(i);
 }
 }
}
Example 15:
---------------
class A10
{
 public static void main( String args[])
 {
 char color='a';

 switch(color)
 {
  case 'r':
  System.out.println("Red Color..");
  break;
  case 'g':
  System.out.println("Green Color..");
  break;
  case 'b':
  System.out.println("Blue Color..");
  break;
  default:
  System.out.println("Invalid Color..");
 }
 }
}


Example 16:
---------------

//Using Enhanced for loop

class A7
{
 public static void main(String args[])
 {
 int arr[]={10,20,30,50,60,89,34,56,67,67,45};

 for(int i : arr)
 {
 System.out.println(i);
 }
 }
}

Example 17:
---------------
class A8
{
 public static void main(String arg[])
 {
 String arr[]={"Sunitha","Scott","Martin","Smith"};

 for(String str : arr)
 {
  System.out.println(str);
 }
 }
}

Example 18:
---------------
class A9
{
 public static void main(String args[])
 {
 char ch[]={'A','P','T','E','C','H'};

 for(char c : ch)
 {
  System.out.println(c);
 }


 }
}
Arrays
=================
Purpose of Array:
----------------------
 Consider a program that stores the names of 100 students. To store the names, the programmer

would create 100 varibles of type string.  In such situations, the programmer can create an array for

storing the 100 names in single variable.

Difinition of an Array:
----------------------------
  Arrays are group of values of same datatype.  These values are stored in adjacent memory

locations, making it easier to access and manipulate them.  Each value is referred to as an element.  These

elements are accessed using index numbers that determine the position of the eleement in the array list.

syntax:
-----------
Datatype arrvar[]
 Or
Datatype[] arrvar;

1.. Array variables are allocated memory on the stack.
2.In the second step we create the memory for the array elements with the new keyword using the below

syntax:

Arrvar=new datatype[width];

Ex:- a=new int[10];

In java, we don’t have pointers as the memory allocated to array will not refer to large extent.

The array elements are themselves allocated memory on the heap.

Unlike in c++, arrays in java are internally treated as objects.

Reading past the boundaries of an array generates an exception called
ArrayIndexOutOfBoundsException

Because arrays are internally treated as objects they have given  built in property called length which

returns the number of elements in the array

The array elements are implicitly assigned default values zeroes in case of numbers, false in case of

Booleans, null in case of characters and other objects.

Example 19:
---------------
//Arrays
class A11
{
 public static void main(String args[])
 {
 int[] a={10,20,30,40,50};

 for(int i=0;i<5;i++)
 {
  System.out.println(a[i]);
 }
 }
}

Example 20:
---------------
//Arrays
import java.util.*;

class A12
{
 public static void main(String args[])
 {
 Scanner S=new Scanner(System.in);

 int arr[]=new int[5];
 System.out.println("Enter five values to an Array: ");

 for(int i=0;i<5;i++)
 {
  arr[i]=S.nextInt();
 }
 System.out.println("Printing an Array\n----------------");
 for(int i=0;i<5;i++)
 {
  System.out.println(arr[i]);
 }
 }
}


Example 21:
---------------
//Arrays
import java.io.*;

class  A13
{
 public static void main(String arg[]) throws Exception
 {
 BufferedReader B=new BufferedReader(new InputStreamReader(System.in));

 int a[]=new int[5];
 int sum=0;
 System.out.println("Enter Five Values to an Array :  ");

 for(int i=0;i<5;i++)
 {
  a[i]=Integer.parseInt(B.readLine());
 }
 for(int i=0;i<5;i++)
 {
  sum=sum+a[i];
 }
 System.out.println("The Sum of an Array is "+sum);
 }
}

Example 22:
---------------
//Arrays
import java.io.*;
class A14
{
 public static void main(String a[]) throws IOException
 {
 BufferedReader B=new BufferedReader(new InputStreamReader(System.in));
 String str[]=new String[5];
 System.out.print("Enter any Name: ");
 for(int i=0;i<5;i++)
 {
  str[i]=B.readLine();
 }
 System.out.println("\nThe names are \n");
 for(int i=0;i<5;i++)
 {
  System.out.println(str[i]);
 }
     }
}




Types of Arrays:
---------------------
 1. Single-Dimensional Arrays
 2. Multi-Dimensional Arrays


1. Single-Dimensional Arrays
----------------------------------------
 The elements of a single-dimensional array are stored in a single row in the allocated memory.
Example 23:
---------------
//Arrays
class A15
{
 public static void main(String args[])
 {
 char ch[]={'A','P','T','E','C','H'};

 for(int i=0;i<6;i++)
 {
  System.out.print(ch[i]);
 
 }
}
}

2. Multi-Dimensional Arrays
-----------------------------------------
 Consider a scenario where we need to store the roll numbers of 50 students and their marks in

three exams.  Using a single-dimensional array, we require two separate arrays for storing rollnumbers

and marks.  However, using a multi-dimensional array, we just need one array to store both, roll numbers

as well as marks.

 A multi-dimensional array allows you to store combination of values of a single type in two or

more dimensions.  The dimensions of the array are represented as rows and columns similar to the rows

and columns of a Microsoft Excel Sheet.

There are two types of multi-dimenstional arrays.  Those are
  1. Rectangular Arrays
  2. Jagged Arrays.

 1. Rectangular Array:
------------------------------
 A rectangular array is multi-dimensional array where all the specified dimensions have

constant values.  A ractangular array will always have the same number of columns for  each row.

Example 25:
---------------
// Rectangular Array
class A17
{
 public static void main(String args[])
 {
 int arr[][]=new int[4][5];
 int i=0;

 for(int row=0;row<4;row++)
 {
  for(int col=0;col<5;col++)
      {
        arr[row][col]=i;
           i++;
      }
  }
  for(int row=0;row<4;row++)
  {
   for(int col=0;col<5;col++)
     {
                      System.out.print(arr[row][col]+ "\t");
  }
   System.out.println();
 }
}
}

Jagged Array:
--------------------
 A jagged array is a multidimensional array where one of the specified dimensions can have

varying sizes.  Jagged arrays can have unequal number of columns for each row.

Example 24:
------------------
//Jagged Arrray
class A16
{
 public static void main(String args[])
 {
 String[][] companies=new String[3][];
 companies[0]=new String[]{"Intel","AMD"};
 companies[1]=new String[]{"IBM","Microsoft","Sun"};
 companies[2]=new String[]{"HP","Canon","Lexmark","Epson"};
 for(int row=0;row<companies.length;row++)
 {
  System.out.print("List of companies in group "+(row+1) + ":\t");
  for(int col=0;col<companies[row].length;col++)
  {
   System.out.print(companies[row][col] + " ");
  }
 System.out.println();
 }
}
}


No comments:

Post a Comment

PHP Notes

The Characteristics of PHP:- ----------------------------------- 1.PHP is a high level programming language. 2.It is a server-side scrip...