Anda di halaman 1dari 26

Java Programming language

The Java programming language is a high-level language that can be characterized by all of the following buzzwords: Simple High performance Architecture neutral Multithreaded Object oriented Robust Portable Dynamic Distributed Secure

Java Programming language


In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes the machine language of the Java Virtual Machine (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.

Java Programming language

It java feature that it act as a platform independent. Due to the conversion of source code into byte code, these byte code can be deployed in all the operating system. The java platform has two components 1) Java Virtual Machine 2) Java API

Java Programming language


Java is a object oriented language: in real life every thing is an object (dog, car, TV, Computer etc). A program is a bunch of objects telling each other what to do by sending messages. Each object has its own memory made up of other object. Each object has type because it is an instance of class. The class is the template given to the object. It describes a set of objects that have identical character (data elements) and behaviors (functionality). So class is a data type defined by user . Which contains the primitive data types and operation on them by using functions. Using this data type we can create an en number of objects. So class acts as a blue print for objects. For example: Computer acts a whole object which has other small part of objects combined together to get a main object. They will create a blue print of Monitor , Hard disk , Keyboard , Mouse and from that hundreds of computers will be manufactured.

Class objExample{ int a, b; public void add(){ int a=10; int b=20; int c = a+b; System.out.println(c); } public void sub(){ int a=20; int b=10; int c = a-b; System.out.println(c); } public static void main(String args[]){ objExample a1 = new objExample(); a1.add(); a1.sub(); objExample b1 = new objExample(); b1.add(); b1.sub(); }

Java Programming language


What is a class ? It acts as a blue print for objects. It defines a new type of data. class emp{ String name; String id; double salary; } in the above example emp acts as a templet for the container of the employee details. We can use this name to declare object of type emp. To create actual object we require the below statement. emp manager = new emp(); after this statement executes manager acts as a instance of emp. Thus it will have physical means memory is created for the object at the runtime. Each new object has its own copy of class with instance variable. to access the instance variable of the class use . operator it also used to access the methods of the class also. manager.name = xyz; manager.id = 123ee; manager.salary = 12345.90;

Java Programming language As we have created the object we can give a reference to object also. emp e1 = new emp(); emp e2 = e1; the above statement states that e1 and e2 are similar object which referred to same memory location.
e1 e2 Name Id salary

Java Programming language


public class xyz{ String name; String id; double salary; public void display(){ System.out.println(name); System.out.println(id); System.out.println(salary); } public static void main(String args[]){ xyz e1 = new xyz(); e1.name ="Shankar"; e1.id = "345tt"; e1.salary = 12345.90; e1.display(); xyz e2 = e1; e2.display(); } }

Java Programming language


Constructors: Each time when an instance is created it is very difficult to initialize that object. So java facilitate the object to initialize themselves when they are created. This is automatically performed through the use of constructor. It gives the characteristics to object when it has created. We can put the conditions to the object when it has created by using the constructors. it has the same name as the class in which it resides and it is similar to a method. Either we can define it or it will automatically done by default constructor. They have no return value not even void. Bcz the implicit return type of class constructor is the class type itself. xyz x1 =new xyz(); it is a constructor. Parameterized Constructor : This will help us to create a object with value by passing the arguments to parameterized constructor.

public class emp{ String name; String id; double salary; emp(String n,String i,double s){ name = n; id = i; salary = s; } public void display(){ System.out.println(name); System.out.println(id); System.out.println(salary); } public static void main(String args[]){ emp e1 = new emp("shailaja","123rr",1000.00); emp e2 = new emp("rani","2345tt",23456.00); e1.display(); e2.display(); } }

Java Programming language


As the object oriented program we have to divided the function into different modules. So the methods are there, as the constructor like same way we have to define the methods in java except that each method has a return value at list void should be there. The general format is type name (parameters){ //body of the method return; } type specify the type of return value it may be primitive data types or object. name is the name of the method. And in the bracket we will pass the parameters as there in the constructor but here we will pass the value to variables declared in the method. return is the key word which will return the total value of the function which is specified in the method. int add(int a,int b){ int c = a=b; return c; } type = int , name = add , parameters = (int a,int b) return = int.

Java Programming language


Polymorphism : It is the process when one object changes it behaviors according to the way it has used. This can be achieved in java by overloading and overriding Overloading : it is possible to define two or more methods with in the same class with same name. 1) different return type 2) different parameters when an overloaded method is invoked , java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. It will takes place only with in the single class.

class xyz{ int a,b,c; public void add(){ c = a+b; System.out.println(c); } public void add(int d , int e,int f){ c = d + e + f; System.out.println (c); } public int add(int d,int e){ return c = d + e; } public static void main(String args[]){ xyz g = new xyz(); g.a=10; g.b=20; g.add(); g.add(100,200); System.out.println( g.add(300,400)); }

Java Programming language

Java Programming language


As the methods are overloaded you can overload the constructor also. class box{ double width; double height; double depth; box(){ width = -1; height = -1; depth = -1; } box(double w, double h , double d){ width = w; height = h; depth = d; } public static void main(String args[]){ box b1 = new box(); box b1 = new box(10,20,30); } }

Java Programming language


static keyword : it can be given to variable, method and class. When you say something is static, it means that data or method is not tied to any particular object instance of that class. it remains the as a whole for any instance of object created by the class. class emp{ static int a; public static void main(String args[]){ emp e1 = new emp(); emp e2 = new emp(); } } as two objects are created there will be two copy of memory bcz each object is independent but the static int a variable will have only one storage area for the two objects. we can access the int a with two ways e1.a = 10; emp.a=20; Similar logic will be add to methods also class emp1{ static void add(){ } }

Java Programming language


Inheritance : It is another property of oops concept which will inherit the methods, variables of super class to sub class extends is the key word to inherit the classes. class abc{ } class bca extends abc{ } Here abc is super class and bca is sub class which will inherit the charter from super class and it has all the rights to add additional methods and variables also. abc extends bca In real life inheritance is possible with genetics fathers characters are inherited by son even though he has its own characters.

Java Programming language


class abc{ int c; public void add(int e,int d){ c = e+d; System.out.println(c); } } class bca extends abc{ double d; public void add1(double f , double e){ d = f+e; System.out.println(d); } public static void main(String args[]){ bca b1 = new bca(); b1.add1(10.20 ,20.30); b1.add(10,20); } }

Java Programming language


Method overriding : is possible by inheritance. if the super class has the same method name , return type , parameters then the sub class method over rides the super class method. class emp{ String name; String id; public void display(){ System.out.println(name); System.out.println(id); } }

Java Programming language


class manager extends emp{ double salary ; public void display(){ System.out.println(name); System.out.println(id); System.out.println(salary); } public static void main(String args[]){ manager m1 = new manager(); m1.name = xyz; m1.id = 234xx; m1.salary = 100000.00; m1.display(); } }

Java Programming language


Overriding of the constructor is not possible because super class has different name than sub class. But we can call the super class constructor to sub class by using the keyword super. overriding is possible only when the methods in super class and sub class will satisfy the following conditions 1) method should have same return type , same number of parameters. 2) Access specify of the super class should have more power than sub class. super has two general forms. The first calls the super class constructor. The second is used to access a member of the super class that has been hidden by a member of a subclass. Super() must always be the first statement executed inside a subclass constructor. super keyword is used with the variables and methods of the super class to sub class.

Java Programming language


class box{ double width; double height; double depth; box (double w , double h , double d){ width = w; height = h; depth =d; } public void display(){ System.out.println(width); System.out.println(height); System.out.println(depth); } }

Java Programming language


public class box1 extends box{ double weight; box1(double w,double h,double d,double m){ super(w,h,d); weight = m; } public double volume(){ return width * height * depth*weight; } public static void main(String args[]){ box1 b1 = new box1(10,20,30,40); b1.display(); System.out.println(b1.volume); } }

Java Programming language


super keyword for the method and variables class a{ int i; void show(){ System.out.println(i); } }

Java Programming language


class b extends a{ int i; b(int a,int b){ super.i = a; i = b; } void show(){ super.show(); System.out.println(i); } public static void main(String args[]){ b ob = new b(2,4); ob.show(); } }

Java Programming language


final Keyword : this keyword can be used to methods , variables, class methods if the final is used at the method then it stops the overriding. class it will stop the inheritance. variable it will behave like constant variable.

Java Programming language


Command-Line Arguments : if the values has to be passed at run time then it will be called as command-line arguments to main(). class example{ public static void main (String args[]){ for (int i=0; I < args.length ;i++){ System.out.println(args[i]); } } }

Anda mungkin juga menyukai