Monday, October 10, 2011

Conditional Operator Example in java.


package ConditionalOperatorExample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConditionalOperatorExample
{
public static void main(String[] arun) throws IOException
{
String s1,s2,s3;
int i1,i2,i3,Max,Min;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter first number; ");
s1 = br.readLine();

System.out.println("Enter second number: ");
s2 = br.readLine();

System.out.println("Enter third number: ");
s3 = br.readLine();

i1 = Integer.valueOf(s1).intValue();
i2 = Integer.valueOf(s2).intValue();
i3 = Integer.valueOf(s3).intValue();

Max = (i1>i2)?((i1>i3)?i1:i3):((i2>i3)?i2:i3);
System.out.println("Maximum number is "+Max);

Min = (i1 System.out.println("Minimum number is "+Min);
}
}


Output =>
Enter first number;
25
Enter second number:
3
Enter third number:
64
Maximum number is 64
Minimum number is 3

Wednesday, September 28, 2011

A static method can't make reference of a non-static variable.


/*
* A static method can't make reference of a non-static variable.
* But a non-static method can make reference of a non-static as well as static variable.
*/
package SCJP;

public class StaticMethodExample
{
static int static_variable = 99;
int non_static_variable = 98;

public static void static_method()
{
System.out.println("Access static variable from static method = "+static_variable);
/*System.out.println("Access non-static variable from static method = "+non_static_variable); can't do this coz of non-static variable.*/
}
public void non_static_method()
{
System.out.println("Access static variable from non-static method = "+static_variable);
System.out.println("Access non-static variable from non-static method = "+non_static_variable);
}


public static void main(String[] arun)
{
new StaticMethodExample().static_method();
new StaticMethodExample().non_static_method();
}
}


Output =>
Access static variable from static method = 99
Access static variable from non-static method = 99
Access non-static variable from non-static method = 98

Inner Class calling example


package SCJP;

public class innerClass
{
public static void main(String[] args)
{
A a = new A();
a.displayA();

A.B b1 = a.new B();
b1.displayB();

A.B b2 = new A().new B();
b2.displayB();
}
}
class A
{
public void displayA()
{
System.out.println("Hi! I am in class A");
}
class B
{
public void displayB()
{
System.out.println("Hi! I am in class B");
}
}
}
Output =>
Hi! I am in class A
Hi! I am in class B
Hi! I am in class B
***********************************************
[NOTE: Bellow class has only difference with above one is in above one class "A" is outside the class"innerClass" where as for the bellow one class "A" is inside the class "innerClassToAccessOuterClass"]
package SCJP;

public class innerClassToAccessOuterClass
{
public static void main(String[] arun)
{
A a = new innerClassAccessOuterClass().new A();
a.diaplayA();
}
class A
{
void diaplayA()
{
System.out.println("Hi! I am in class A.");
}
}
}

Output =>
Hi! I am in class A.

String and StringBuffer R&D.


R&D with String and StringBuffer.
**************************************************************
package SCJP;

public class StringExample1
{
public static void main(String[] args)
{
boolean s1 = "Arun" == "Arun";
boolean s2 = new String("Arun") == "Arun";
boolean s3 = new String("Arun") == new String("Arun");

System.out.println("S1 is "+s1);
System.out.println("S2 is "+s2);
System.out.println("S3 is "+s3);

System.out.println("(s2&&s3||s1) is "+(s2&&s3||s1));
}
}

Output =>
S1 is true
S2 is false
S3 is false
(s2&&s3||s1) is true
*****************************************************************
package SCJP;

public class StringExample2
{
public static void main(String arun[])
{
boolean s1 = "Arun" == "Arun";
boolean s2 = new String("Arun").equals(new String("Arun"));
boolean s3 = "Arun".toString() == "Arun";

System.out.println("S1 is "+s1);
System.out.println("S2 is "+s2);
System.out.println("S3 is "+s3);

System.out.println("(s1&&s2&&s3) is "+(s1&&s2&&s3));
}
}

Output =>
S1 is true
S2 is true
S3 is true
(s1&&s2&&s3) is true
******************************************************************
package SCJP;

public class StringExample3
{
public static void main(String[] args)
{
StringBuffer sb = "";//compile time error.
}
}
*******************************************************************
package SCJP;

public class StringExample4
{
public static void main(String[] args)
{
String s = null;
System.out.println(null+s);
}
}

Output =>
nullnull
*********************************************************************

Tuesday, September 27, 2011

SCJP-Link

Exception handling (Create my own exception)


package MyException;

public class MyException extends Exception {
public MyException(String str)
{
super(str);
}
}


package MyException;

public class mainClass
{
public static void main(String args[])
{
try
{
int n = 105;
if(n<199)
throw new MyException(n+" is less than 199");
}catch (MyException e)
{
System.out.println(e.getMessage());
}
}
}


Output =>
105 is less than 199

Monday, September 26, 2011

StringBuffer and StringBuilder are mutable (Example)


//Bellow program shows that StringBuffer/StringBuilder is mutable
package StringBuffered;
public class StringObjectIsImmutable
{
public static void main(String[] arun)
{
//StringBuffered is mutable and synchronized.
StringBuffer name = new StringBuffer("Arun");
System.out.println(name);
name.append(" Kumar");
// in the above line like String object no new StringBuffered Object (name) is created. That's the reason executable time for StringBuffer/String Builder is faster than String.
System.out.println(name);
/*//StringBuffered is mutable and not synchronized
StringBuilder name = new StringBuilder("Arun");
System.out.println(name);
name.append(" Kumar");
// in the above line like String object no new StringBuffered Object (name) is created. That's the reason executable time for StringBuffer/String Builder is faster than String.
System.out.println(name);*/
}
}
Output =>
Arun
Arun Kumar