You are on page 1of 11

Java

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. ---: To get the input from the user :--import java.io.*; public class getUserInput { public static void main(String args[]) throws IOException { String str=null; // Create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter lines of text."); str = br.readLine(); System.out.println("The entered text is :"+ str str); } }

Use throws IOException or Use try..Catch(IOException e)

---: Program to list all even numbers between two numbers :--import java.io.*; class getUserInput{ public static void main(String[] args) { try{ BufferedReader br1 = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter number : "); int num = Integer.parseInt(br1.readLine()); System.out.println("Even Numbers:"); for (int i=1;i <=num ; i++){ if(i%2==0 ){ System.out.print(i+","); } } } catch(Exception e){ e.printStackTrace(); } } }

Integer.parseInt() is used because all the values inputted by the user , Java compiler will take it as the String, If we use parseInt then the string is converted into the Integer value

import java.io.*; class getUserInput{ public static void main(String[] args) { for (int i=1;i <=Integer.parseInt(args[0]) ; i++) if(i%2==0 ) System.out.print(i+" "); } } Integer.parseInt(args[0]) , while run we have to give as java getUserInput 13 Like this we can give n number of arguments

java getUserInput 10 20 30 40 50 here args[0]=10 args[1]=20 args[2]=30 args[3]=40 args[4]=50

Constructor:
If no user defined constructor is provided for a class, compiler initializes member variables to its default values. numeric data types are set to 0 char data types are set to null character() reference variables are set to null In order to create a Constructor observe the following rules 1. It has the same name as the class 2. It should not return a value not even void

class Demo{ int value1=1; int value2=2; Demo(){ System.out.println("Inside 1st Constructor"); } Demo(int a){

value1 = a; System.out.println("Inside 2nd Constructor"); } Demo(int a,int b){ value1 = a; value2 = b; System.out.println("Inside 3rd Constructor"); } public void display(){ System.out.println("Value1 === "+value1); System.out.println("Value2 === "+value2); } public static void main(String args[]){ Demo d1 = new Demo(); Demo d2 = new Demo(30); Demo d3 = new Demo(30,40); d1.display(); d2.display(); d3.display(); } }

---: Creating Only Object for the Child Process The parent constructor is called 1st and then Child Constructor is called :--class Demo{ int value1; int value2; Demo(){ value1 = 1;

value2 = 2; System.out.println("Inside 1st Parent Constructor"); } Demo(int a){ value1 = a; System.out.println("Inside 2nd Parent Constructor"); } public void display(){ System.out.println("Value1 === "+value1); System.out.println("Value2 === "+value2); } public static void main(String args[]){ DemoChild d1 = new DemoChild(); d1.display(); } } class DemoChild extends Demo{ int value3; int value4; DemoChild(){ //super(5); value3 = 3; value4 = 4; System.out.println("Inside the Constructor of Child"); } public void display(){ System.out.println("Parent Value1 === "+value1); System.out.println("Parent Value2 === "+value2); System.out.println("Child Value3 === "+value3); System.out.println("Child Value4 === "+value4); } }

A scenario where a base class is extended by a child .Whenever an object of the child class is created , the constructor of the parent class is invoked first. This is called Constructor

chaining.

super() can be used to call the parent constructor explicitly In our example, super(5) is called then only value1=5 only initialized. Value2==???

this keyword
this is a reference to the current object, whose method is being called upon. You can use this keyword to avoid naming conflicts in the method/constructor of your instance/object .

class Demo{ int a; int b; public void setData(int a ,int b){

a = a; b = b; } public void showData(){ System.out.println("Value of A ="+a); System.out.println("Value of B ="+b); } public static void main(String args[]){ Demo obj = new Demo(); obj.setData(2,3); obj.showData(); }}

class Demo{ int a; int b; public void setData(int a ,int b){ this.a = a; this.b = b; } public void showData(){ System.out.println("Value of A ="+a); System.out.println("Value of B ="+b); } public static void main(String args[]){ Demo obj = new Demo(); obj.setData(2,3); obj.showData(); }}

Overloading

OverloadDemo.java class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } Overload.java class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(11); ob.test(11, 2); result = ob.test(11.2); System.out.println("Result of ob.test(11.2): " + result); } }

Inheritance
Base.java class Base { int a = 11; void show() { System.out.println(a); } } Super.java class Super extends Base { int a = 22; void show() { super.show(); System.out.println(a); } public static void main(String[] args) { new Super().show(); } }

Interface
interface IAnimal { public void speak(); } public class Cat implements IAnimal { public void speak() { System.out.println("Cat speaks meaown!!!"); } public static void main(String args[]) { Cat c = new Cat(); c.speak(); } } public class Dog implements IAnimal { public void speak() { System.out.println("Dog eats Cat!!!"); } public static void main(String args[]) { Dog d = new Dog(); d.speak();

} }

You might also like