public class HelloWorld { public static void main(String args[]) { String s1="sree"; String s2 ="sree"; // s1 & s2 point to same memory String s3= new String("sree"); //s3 creates new string in memory if(s1==s2) // == compares the memory locations System.out.println("same"); if(s1==s3) // == compares the memory locations System.out.println("same"); else System.out.println("not"); if(s1.equals(s3)) // equals method compares content in the strings System.out.println("same"); else System.out.println("not"); } }
-----------------------------------------------------------------------------------------------------------
1)
package com.saavn;
import java.util.Scanner;
public class Book {
int bookPrice;
String bookName;
void bookTitle()
{
System.out.print("enter book titile: ");
Scanner scanner=new Scanner(System.in);
bookName=scanner.next();
System.out.print("enter the price of the book: ");
Scanner scan=new Scanner(System.in);
bookPrice=scan.nextInt();
}
void display()
{
System.out.println("Name of the book is "+bookName +" and price is : "+bookPrice);
}
public static void main(String[] args) {
Book a=new Book();
a.bookTitle();
a.display();
}
}
===========================================================
public class Calculations {
public static void main(String args[])
{
int a,b;
char ch;
System.out.println("1. Addition 2.subtraction 3.multiplication ");
System.out.print("enter your choice: ");
Scanner scan = new Scanner(System.in);
ch= scan.next().charAt(0);
//System.out.println("char is :"+ch);
System.out.println("enter a & b: ");
a=scan.nextInt();
b=scan.nextInt();
//System.out.println("a & b is : "+a +" ,"+b);
switch(ch)
{
case '1':
System.out.println("Addition of "+a +"& "+b +"is :"+(a+b));
break;
case '2':
System.out.println("Subtraction of "+a +"& "+b +"is :"+(a-b));
break;
case '3':
System.out.println("Multiplication of "+a +"& "+b +"is :"+(a*b));
break;
default:
System.out.println("Invalid choice");
}
}
}
==============================================================
public class Array {
public static void main(String[] args) {
System.out.print("enter length of array: ");
Scanner scan = new Scanner(System.in);
int x= scan.nextInt();
int[] arr = new int[x];
for(int i=0;i<x;i++)
{
System.out.print("enter arr[ "+i+"] :");
int y=scan.nextInt();
arr[i]=y;
}
System.out.print("The array is : {");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+",");
}
System.out.print("}");
}
}
package com.google;
class Priv
{
int a ;
public int b;
private int c;
void setc(int x)
{
c=x;
}
int getc(){
return c;
}
}
class Test {
public static void main(String[] args) {
Priv obj=new Priv();
obj.a=2;
obj.b=3;
obj.setc(4);
System.out.println("a & b : "+obj.a+" ,"+obj.b);
System.out.println("c : "+obj.getc());
}
}
class sample
}
===========================================================
package com.google;
class A{
int i,j;
void showij()
{
System.out.println("i & j : "+i +" "+j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k : "+k);
}
void sum()
{
System.out.println("i + j +k : "+(i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[])
{
A supobj=new A();
B subobj=new B();
supobj.i=1;
supobj.j=2;
System.out.print("super obj contents: ");
supobj.showij();
subobj.i=3;
subobj.j=4;
subobj.k=5;
System.out.println("sub obj contents: ");
subobj.showij();
subobj.showk();
subobj.sum();
supobj.showij();
}
}
===========================================================
class A
void set(int x,int y)
}
}
class B extends A
void sum()
}
class Access {
package mypack;
class A
{
int a,b;
void set(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("a & b is: "+a + " "+b);
}
}
class B extends A
{
void sum()
{
System.out.println("sum of a& b is :"+(a+b));
}
}
class Inherit {
public static void main(String args[])
{
A aobj= new A();
aobj.set(1,3);
aobj.show();
B bobj=new B();
bobj.set(5,4);
bobj.sum();
bobj.show();
}
}
===========================================================
public class Calculations {
public static void main(String args[])
{
int a,b;
char ch;
System.out.println("1. Addition 2.subtraction 3.multiplication ");
System.out.print("enter your choice: ");
Scanner scan = new Scanner(System.in);
ch= scan.next().charAt(0);
//System.out.println("char is :"+ch);
System.out.println("enter a & b: ");
a=scan.nextInt();
b=scan.nextInt();
//System.out.println("a & b is : "+a +" ,"+b);
switch(ch)
{
case '1':
System.out.println("Addition of "+a +"& "+b +"is :"+(a+b));
break;
case '2':
System.out.println("Subtraction of "+a +"& "+b +"is :"+(a-b));
break;
case '3':
System.out.println("Multiplication of "+a +"& "+b +"is :"+(a*b));
break;
default:
System.out.println("Invalid choice");
}
}
}
==============================================================
public class Array {
public static void main(String[] args) {
System.out.print("enter length of array: ");
Scanner scan = new Scanner(System.in);
int x= scan.nextInt();
int[] arr = new int[x];
for(int i=0;i<x;i++)
{
System.out.print("enter arr[ "+i+"] :");
int y=scan.nextInt();
arr[i]=y;
}
System.out.print("The array is : {");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+",");
}
System.out.print("}");
}
}
==================================================================
package com.saavn;
import java.util.Scanner;
class Squ
{
int squaring(int i)
{
return i*i;
}
}
class Square {
public static void main(String args[])
{
int x;
System.out.println("enter x value");
Scanner scan=new Scanner(System.in);
x=scan.nextInt();
Squ s1=new Squ();
System.out.println("the square of no is : " + s1.squaring(x));
}
}
======================================================================
Call by Ref
package com.google;
class Change
{
int a,b;
Change(int a,int b)
{
this.a=a;
this.b=b;
}
void method(Change obj1)
{
obj1.a=1;
obj1.b=2;
}
void display()
{
System.out.println("a & b are : "+a +" , "+b);
}
}
class CallByRef {
public static void main(String[] args) {
Change obj=new Change(3,5);
obj.display();
obj.method(obj);
obj.display();
}
}
====================================================================
class Priv
{
int a ;
public int b;
private int c;
void setc(int x)
{
c=x;
}
int getc(){
return c;
}
}
class Test {
public static void main(String[] args) {
Priv obj=new Priv();
obj.a=2;
obj.b=3;
obj.setc(4);
System.out.println("a & b : "+obj.a+" ,"+obj.b);
System.out.println("c : "+obj.getc());
}
}
===========================================================
package com.google;
class sample
{
static int m=4;
}
class TestStatic {
static int a=3;
static int b;
static
{
b=4;
}
static void method(int x)
{
System.out.println("x : "+x);
}
public static void main(String args[])
{
System.out.println("a& b : "+a+" ,"+b);
method(6);
System.out.println("m : "+sample.m);
/*Outside of the class in which they are defined, static methods and variables can be used
independently of any object. To do so, you need only specify the name of their class followed
by the dot operator.*/
}
}
===========================================================
package com.sree;
class Sample
{
static int a=3;
static void display()
{
System.out.println("a : "+a);
}
}
class StaticDemo{
public static void main(String args[])
{
System.out.println("a in sample class: "+Sample.a);
System.out.println("static method :");
Sample.display();
}
}
class Sample
{
static int a=3;
static void display()
{
System.out.println("a : "+a);
}
}
class StaticDemo{
public static void main(String args[])
{
System.out.println("a in sample class: "+Sample.a);
System.out.println("static method :");
Sample.display();
}
}
===========================================================
Passing Command line Arguments in Eclipse
=====================================================================
package com.google;
class Command {
public static void main(String args[])
{
System.out.println("in main");
for(int i=0;i<args.length;i++)
System.out.println("arg["+i+"] : "+args[i]);
}
}
- Click on Run -> Run Configurations
- Click on Arguments tab
- In Program Arguments section , Enter your arguments.
- Click Apply
=====================================================================
package com.google;
class Command {
public static void main(String args[])
{
System.out.println("in main");
for(int i=0;i<args.length;i++)
System.out.println("arg["+i+"] : "+args[i]);
}
}
===========================================================
package com.google;
class VarArgs {
static void Test(int arr[])
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
int a[]={1};
int b[]={2,3};
int c[]={};
Test(a);
Test(b);
Test(c);
}
}
class VarArgs {
static void Test(int arr[])
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
int a[]={1};
int b[]={2,3};
int c[]={};
Test(a);
Test(b);
Test(c);
}
}
------------------------------------------------------------------------------------------------------------------------------------
package com.google;
class VarArgs {
static void Test(int ... arr)
{
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
Test(1);
Test(2,3);
Test();
}
}
===========================================================
package com.google;
class VarArgs {
static void Test(String msg,int ... arr)
{
System.out.print(msg +" :");
System.out.print("no of argments: "+arr.length+" contents are: ");
for(int x: arr)
{
System.out.print(x+ " ");
}
System.out.println();
}
public static void main(String args[])
{
Test("one",1);
Test("two three",2,3);
Test("no varags");
}
}
===========================================================
Inheritance
class A{
int i,j;
void showij()
{
System.out.println("i & j : "+i +" "+j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k : "+k);
}
void sum()
{
System.out.println("i + j +k : "+(i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[])
{
A supobj=new A();
B subobj=new B();
supobj.i=1;
supobj.j=2;
System.out.print("super obj contents: ");
supobj.showij();
subobj.i=3;
subobj.j=4;
subobj.k=5;
System.out.println("sub obj contents: ");
subobj.showij();
subobj.showk();
subobj.sum();
supobj.showij();
}
}
===========================================================
package com.google;
class A
{
int i,j;
void set(int x,int y)
{
i=x;
j=y;
}
void show ()
{
System.out.println(" i& j:"+i+" "+j);
}
}
class B extends A
{
int tot;
void sum()
{
System.out.println("sum is :"+(i+j));
}
}
class Access {
public static void main(String args[])
{
A aobj=new A();
aobj.set(5,6);
aobj.show();
B bobj=new B();
bobj.set(1,2);
bobj.sum();
aobj.show();
}
}
===========================================================
class A
{
int a,b;
void set(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("a & b is: "+a + " "+b);
}
}
class B extends A
{
void sum()
{
System.out.println("sum of a& b is :"+(a+b));
}
}
class Inherit {
public static void main(String args[])
{
A aobj= new A();
aobj.set(1,3);
aobj.show();
B bobj=new B();
bobj.set(5,4);
bobj.sum();
bobj.show();
}
}
===========================================================
package sri;
import sri.Subcls;
import sri.Supercls;
class Supercls {
int x=5;
void show()
{
System.out.println("in super cls");
}
}
class Subcls extends Supercls{
int x=9;
void show()
{
System.out.println("in sub cls");
}
void method()
{
Subcls sub=new Subcls();
sub.show();
super.show();
System.out.println("in super cls:"+super.x);
System.out.println("in sub cls:"+sub.x);
}
public static void main(String args[])
{ System.out.println("in main");
Subcls obj =new Subcls();
obj.method();
}
}
import sri.Subcls;
import sri.Supercls;
class Supercls {
int x=5;
void show()
{
System.out.println("in super cls");
}
}
class Subcls extends Supercls{
int x=9;
void show()
{
System.out.println("in sub cls");
}
void method()
{
Subcls sub=new Subcls();
sub.show();
super.show();
System.out.println("in super cls:"+super.x);
System.out.println("in sub cls:"+sub.x);
}
public static void main(String args[])
{ System.out.println("in main");
Subcls obj =new Subcls();
obj.method();
}
}
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
This keyword
- this keyword can only be the first statement in Constructor.
- A constructor can have either this or super keyword but not both.
. subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private
.You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass
========================================================
No comments:
Post a Comment